Calling instance from another view - iphone

Can I call the following instance of first view from another second view
(void)applicationDidFinishLaunching:(UIApplication *)application

applicationDidFinishLaunching is called by the application framework when your application launches. You should never be calling it.
It is part of your application's delegate, not of the view.

Related

viewDidAppear isn't getting called after running from the background

Method viewDidAppear is not getting called after going from the background back into my iPhone Application.
Does anyone have a solution for this?
I need to call a method every time my MainViewController is shown (even after returning from the background) that will change a label to the new date.
viewDidAppear doesn't seem to be working properly.
Actually it is working properly.
Instead, consider installing a notification handler for UIApplicationDidBecomeActiveNotification or UIApplicationWillEnterForegroundNotification, whichever may be more appropriate for your situation. From there you could do an update of your GUI.
You will have the UIApplicationWillEnterForegroundNotification method in App delegate.Now you need to create the object of the class you want and call the viewwillAppear
this method will be called when the app is in background.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
now for the view will appear code like this
- (void)applicationWillEnterForeground:(UIApplication *)application
{
ViewController *vc = [ViewController alloc]init];//you can use your viewcontroller here
[vc viewDidAppear:YES];// this will call the method.
}
Try using viewWillAppear if you dont mind doing this process before the is actually shown.
Else you can use the method.
- (void)applicationWillEnterForeground:(UIApplication *)application{}

View Controller lifecycle

Hi I have a resetView method that resets my view too a default state and that method gets called on loadView and when the device is shaken , the two view controllers are in a tab bar controller so when I change tabs the viewWillAppear method is call and resets the view but when close the application and it go's into the background the next time it is relaunched the view wont reset I have add the reset view method call in to
-(void)viewWillAppear:(BOOL)animated;
-(void)viewDidAppear:(BOOL)animated ;
- (void)viewDidLoad;
-(void)awakeFromNib;
but none of them are being called when the application is woken from the background
Look at the application delegate methods. You should call any methods from when the app is started from there. If your view was the last view when you left the app, none of the methods you listed should be called. Read the documentation about when they are called.

What is the difference between view 'did load' method and 'didFinishLaunching' application

In the iPhone SDK, can anyone explain the difference between application DidFinishLaunching in delegate and ViewDidLoad method in ViewControler?
applicationDidFinishLaunching is called by the App Delgate when your application has finished launching. This method is useful for doing setup as soon as possible. Examples of this could include setting up GameCenter, and doing some first launch checking.
viewDidLoad is called by a UIViewController after the view is loaded, usually from the nib. However, in some cases, you may want to do setup before the view is loaded. In that case, use
viewWillLoad is called just before the view is loaded, usually from the nib. For the most part, it will not make much of a difference wether you use viewDidLoad or viewWillLoad. However, some setup may have to be done after the view is loaded and other setup you may want to do before the screen displays anything.
applicationDidFinishLaunching is for initial appwide setup, viewWillLoad is for setup before the view is displayed, and viewDidLoad is for setup right after the view is loaded.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions gets called when your app is finished launching; and viewDidLoad: gets called when an UIView controlled by UIViewController is loaded.
viewDidLoad is the method that is called once the view has been loaded. It is a place where you can insert code that does initial setup of the view once it has been loaded.
The applicationDidFinishLaunching: method of the NSApplication delegate will be called when the app has finished loading.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Method available only in application AppDelegate it calles only ones at the time of app is loaded you can do all the stuff related to your application prelaunch here.
-(void)viewDidLoad: called whenever a view is loaded.
it also call ones when the view is loaded
but it's has own copy for every viewController you can do any stuf related to that controller inside it.

Objective C: When to use methods in App Delegate and when to use methods in View Controller

I am a little confused on the following methods in both my View Controller and App delegate classes
Method in App delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Method in ViewController:
- (void)viewDidLoad
Under what situation do I need to add code in the app delegate or ViewController methods? I believe that for switching of views, we need to include it in the app delegate method, are there any rules of thumb that we need to abide by?
Thanks!
Zhen
As it is called at launch,application:didFinishLaunchingWithOptions: typically contains logic to initialise your application (e.g. setting up core data objects, registering for push notifications, etc.). The purpose of viewDidLoad on the other hand is to initialise your view controller before it is shown.
application:didFinishLaunchingWithOptions: should be used for setup that must occur when the application is launched, e.g.
Core Data stack
Restoring application state
Creating application navigation controllers or tab bars
viewDidLoad should be used for any configuration that only needs to be done for that specific view controller. In some cases the view may not get loaded, so there's no point doing that configuration in the app delegate.
e.g
Opening an HTTP connection when the view loads
Asking for location data for a view

Access method in viewcontroller from appdelegate

I want to use appdelegate for applicationDidBecomeActive and I want this function to use method which is placed in viewcontroller. How I am gonna do that?
The only thing that I find is to access from anywhere the appdelegate.
You need to give your applicationDelegate class a reference to the viewController that you want to call the method on. You could do this by creating an instance variable or a property of the applicationDelegate that points to your viewController that you want to be able to call the method on. If you create your viewController in the init method of your appDelegate, or in your applicationDidLoad: method, then you can simply assign this viewController to the instance variable/property that you've created.
You can access the root view controller of your app from within the app delegate through the window property, that is self.window.rootViewController. The view controller that you want to access must be a child of the root view controller or at least accessible through it.
I had the same issue of needing to call a method on my view controller from the app delegate. After some digging and reading this answer I tried to call a method (saveData was the method in question) in my view controller directly from the app delegate and it worked as required.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[self.window addSubview:rootViewController_iPad.view];
[self.window makeKeyAndVisible];
return YES;
}
-(void)applicationWillResignActive:(UIApplication *)application {
[rootViewController_iPad saveData];
}
Hope this helps.