Call method from initialized UIViewController in storyboarding - objective c - objective-c

I'm new to Storyboarding in objective c and I need to call method from UIVIewController. Before Storyboarding I was initializing UIViewController in AppDelegate or just assigning pointer there, and then simply call method from any class, accessing AppDelegate properties. How to do It in Storyboarding, If there are no UIViewController initializing by myself? My app is UITabBar application if it does matter.

You can get the root view controller of your storyboard from your app delegate. For example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabbarController *tbc = (UITabbarController *)self.window.rootViewController;
// Do any setup you want to here
// You aren't setting up the controller - just accessing the one that has been set up for you by the storyboard
return YES;
}

If you want to have this method available from any class you can keep doing the same. Place in in your AppDelegate and then just retrieve the shared instance on the controller you want to use it from. (all your application has access to your appdelegate) so just import the header of the app delegate, create a new instance of your specific appdelegatename class, and call the shared delegate. then you can use the method as if that class would have it.
Additionaly you can use something like this
#import "AppDelegate.h"
#define appDelegate (AppDelegate *) [[UIApplication sharedApplication] delegate]
IF you want to call a method from ANY of your viewcontrollers which are specified in the storyboard you can do it by referencing their identifier:
PreviewViewController *tmp = [[self storyboard] instantiateViewControllerWithIdentifier:#"PreviewViewController"];

Related

iOS: how to get access viewController's methods

I have a trouble.
There is viewConntroller and an object inside this viewController. This object tries to get information via NSURLConnection later it has to notify viewController to run method. If i try to create object of viewController inside I catch problem: new viewController makes the same thing that first one do, so new viewController makes NSURLConnection, creates third viewController and so on.
You can pass reference to this object. But first create new property with assign attribute.
//property in your class
#property(nonatomic, assign) id myViewController;
//set reference to your UIViewController
[myClassInstance setMyViewController: self]; //self is MyViewController instance
//call UIViewController's method from your class
MyViewController *controller = (MyViewController *)self.myViewController;
[controller myMethodInUIViewController];
How about passing on a reference of the viewController to the object you´re making inside that viewController?
So something along the lines of:
Object *obj = [[Object alloc]init];
obj.viewController = self;
Obviously, you'd have to create an attribute to hold the viewController on the object class.
Then, when you need to call methods on the viewController, you could use:
[self.viewController performSelector:#selector(methodName:)];

How to Call a Controller Class (delegate) method from View Class Event in Objective C/Cocoa

lets say I have an NSWindow Class, that has several events for mouse and trackpad movements
for example this code
IMHO, I think this should be good programming, it is similar to pointing an action of a button to its method in controller.
in MyWindow.m Class I have (which in IB I have set the window class to it)
#implementation MyWindow
- (void)swipeWithEvent:(NSEvent *)event {
NSLog(#"Track Pad Swipe Triggered");
/* I want to add something like this
Note that, Appdelegate should be existing delegate,
not a new instance, since I have variables that should be preserved*/
[AppDelegate setLabel];
}
#end
and in My AppDelegate.h I have
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSTextField *label;
}
-(void)setLabel;
#end
and in my AppDelegate.m I have
-(void)setLabel
{
[label setStringValue:#"swipe is triggered"];
}
I have tried #import #class, [[... alloc] init], delegate referencing in IB (I made an object class of MyWindow - thanks to the answer of my previous question )
that latter seems the closest one, it works if both of the classes are delegates, so I could successfully call the "setLabel" action from a button in a second controller (delegate class)'s IBAction method,
but this View Events seem not communicating with the delegate's action although their code is executing.
You are sending a message to the AppDelegate class, not the instance of your AppDelegate class which is accessible from the NSApplication singleton ([NSApplication sharedApplication])
[AppDelegate setLabel];
This is wrong, to get the delegate do this:
AppDelegate* appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
then send the message to that instance:
[appDelegate setLabel];

Objective-C – Message a view controller from application delegate

I want to download some data in the application delegate in - application:didFinishLaunchingWithOptions:
After I have downloaded some data I want to set this data to an NSArray property in a view controller. If I have a synthesized property of NSArray (nonatomic, retain) called data I would like to do [viewController setData:downloadedData];
How would I call the active viewController instance from the application delegate?
My application structure is a Tab Bar Controller as root controller.
You'll want to use NSNotificationCenter which will essentially broadcast a message to all objects that have subscribed to that particular message.
In your view controller subscribe to the notification:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(downloadedData:)
notificationName:#"DownloadedData"
object:data];
- downloadedData:(NSNotification *)notification {
self.data = notification.object;
}
And in your app delegate send the notification to the subscribers:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"DownloadedData"
object:data];
Add delegateComplete property in your app delegate class:
//define ivar
id delegateComplete;
//define property
#property (nonatomic, retain) id delegateComplete;
//synthesize
#synthesize delegateComplete;
In init method or viewDidLoad of your viewController do following:
MainClass *appDelegate = (MainClass *)[[UIApplication sharedApplication] delegate];
appDelegate.delegateComplete = self;
replace MainClass with your app class. After downloading is complete, do following in your app delegate:
[delegateComplete loadingCompletedWithData:data];
Dont forget to add this method in your viewController:
- (void)loadingCompletedWithData:(NSData *)data
What happens is that your view controller registers to your app delegate. When loading completes, if your view controller has registered, call loadingCompletedWithData. Proper way of doing this would be through a protocol.

objective C, iOS: not calling alloc or init on a subview

I am currently running the following code as part of a simple test iPad program. I've declared "viewController" as a property. In all other examples I've seen involving subviews, I've been required to alloc and init the viewController, but here it works fine. Any ideas?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Why does this work without allocating or initializing viewController?
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
This looks like the typical boilerplate code Apple supplies with the iOS templates. In these projects the viewController is defined in the MainWindow NIB. This NIB is loaded very early in the process of starting the app. The viewController is allocated in the NIB loading process, and initWithNibName:bundle: is then called. The NIB loading process then connects the initialized object to the view controller's IBOutlet of the app delegate.

objective C Basic question

i made a simple application using view based template.and i put only nslog inside view didload method in viewController file and also inside applicationDidFinishLaunch method (in appDelegate )to checked which class file called first.
after the run i got: viewController Run first and then appdelegate ..but i think appdelegate should first then other's called according to the need ... plz give me the proper reasion.
Noted that --i did not call viewController (didnot make object) in my appDelegate(inside application didFinishLaunch) . i am using ios4
If your View Controller is a property of the AppDelegate, similar to the code reference
#interface AppDelegate_Shared : NSObject <UIApplicationDelegate, UIAlertViewDelegate, OMFDataLoadDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UITabBarController *tabBarController;
}
then it is probably getting allocated by the AppDelegate when it is being allocated. According to the Apple documentation viewDidLoad is run after the view is loaded into memory, which can be a little confusing, since the language can make you believe it's when it's loaded onto the screen.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW25
Move your NSLog statement to viewDidAppear for the result you were expecting. Here's two sample snippets with the way you should expect the statements to load.
ViewController.m
- (void) viewDidLoad {
NSLog(#"1st - this occurs when appDelegate allocates this object");
}
- (void) viewDidAppear {
NSLog(#"3rd - this should appear after the applicationDidFinishLaunchingStatement");
}
AppDelegate_Shared.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"2. Starting AppDelegate_Shared");
[window addSubview:self.tabBarController.view];
[window makeKeyAndVisible];
NSLog(#"4. Leaving AppDelegate_Shared");
return YES;
}
If the initial view hasn't loaded then clearly the application has not finished launching.
The messages are sent in the right order.