I made a shared class which all classes in the program can see it, and if I change any variable in this shared class and all classes can see this change. I use a navigation controller to navigate between view controllers, and I put some code in viewDidLoad function, but when I navigate to the view controller this code called only once. What should I do to make this code run every time I navigate to this view controller?
You can put code in viewWillAppear or viewDidAppear
These are events that you can use:
– viewWillAppear:
– viewDidAppear:
– viewWillDisappear:
– viewDidDisappear:
– viewWillLayoutSubviews
– viewDidLayoutSubviews
Related
I know there are a lot of similar questions, but I can't find one that specifically addresses this.
Why is self.navigationController null when called in viewDidLoad, but correct when called from outside viewDidLoad?
This is my output from NSLog(#"%#",self.navigationController); The first is called in viewDidLoad, the second I add a button to the interface with a method that calls NSLog(#"%#",self.navigationController);:
NavApp[31524:11003] (null)
NavApp[31524:11003] <UINavigationController: 0x6e21190>
I know there must be some simple explanation for this behavior, I'm just curious as to what it is. Thanks!
A view controller's view is loaded when you first access the -view method/property on that controller. After the view has been loaded, the viewDidLoad method is called. This is pretty straight forward. You also have to remember that the view can be loaded/unloaded multiple times if memory warnings are received while the view is off-screen.
So viewDidLoad does not mean your view controller has been inserted into a navigation controller. In the process of pushing a view controller onto a navigation controller, its view will be accessed and loaded, but this will happen before the whole push is complete. So viewDidLoad is clearly getting called before the navigationController property has been updated.
You also have to consider that some other part of your code could be accessing the view controller's view before you even push the view controller onto the navigation controller.
So viewDidLoad is the wrong place to do what you are doing. You probably want to use a method like viewDidAppear: so you know that the view controller's view is part of the view hierarchy when it is invoked.
I was wondering how can you load a class in objective-c from another class? I have a push action button in one and when the users presses this I want it to start this other Class (calling its viewDidLoad method)
You need to be more precise.
What kind of classes are you manipulating ? UIViewControllers ?
If so, you can alloc/init (or alloc/initWithNibName if you are using Interface Builder) the second ViewController when your button is pushed. The viewDidLoad method will be called when the view of the second view controller will be loaded (not necessarily when the viewController is allocated, but when the view will be displayed for the first time).
If you need the execute the code in the viewDidLoad method every time the button is pushed, prefere using the viewWillAppear or viewDidAppear methods.
If you are talking about the classes which are subclass of UIViewController then you can load view of another view controller by pushing it into the navigation controller object. Also you can add the view of the second controller on the first controller view.
By my count, the only two instances when viewWillAppear is called is when you initialize your view controller, or when you pop off the view controller that's on top of it on the navigation stack (ie pushing the back button on the viewcontroller ahead of it). Are there any other instances when viewWillAppar is called? I don't believe it's called when the app becomes active. Interested to hear some responses on this.
viewwillappear method is called as and when the view controller's view is added to the window. ( if the view is already in the window and is hidden by another view, this method is called when the view is once again revealed). The method is a notification to the view controller that the view is about to become visible. You can override this method to make any customizations with presenting the view.
This will also be called anytime addSubView is called, with your view.
How to pop to rootview controll without using popToRootViewControll method because this method does not implement viewDidLoad method of RootViewController???
What do you mean by "because this method does not implement viewDidLoad method of RootViewController"?
The "viewDidLoad" is called only one time if you don't have memory warning. If you get an memory warning your viewDidLoad method is called again.
You need to implement your logic in "viewWillAppear".
And maybe you should read View Controller Programming Guide for iOS
UIViewController Class Reference here you find the viewWillAppear, and viewDidAppear methods
I think you need to take a different approach to solving this problem.
If you have code in the root view controller's viewDidLoad method that you want to run again, then that code shouldn't be in the viewDidLoad method in the first place. viewDidLoad only runs just after the view controller's view is loaded (usually just once in the lifetime of the view controller). You should probably put that code it in its own method in the view controller, so you can still call it from the viewDidLoad method.
To have the code run when you pop to the root view controller, you can call that new method from the root view controller's viewWillAppearAnimated method, which gets called each time the view controller's view is about to be displayed.
When pushing a viewcontroller using UINavigationController into the view:
What is necessary for it to trigger
viewDidAppear: and viewWillAppear: ?
What makes it fail to not trigger viewDidAppear: and viewWillAppear: ?
We are having a hard time relying on wether those methods gets triggered or not.
UINavigationController calls these methods directly on the controller that's being pushed when you call pushViewController:animated: Similarly, UITabBarController calls these methods directly when you switch tabs, and UIViewController calls them when you use presentModalViewController:animated:. They also get called when a view controller's view is added to a window. I've never seen these methods fail to get called in these specific contexts.
Now bear in mind that these methods only get called on the controller being pushed or presented in these specific contexts. These methods won't get called, for example, if you add your view controller's view as a subview of some view other than the UIWindow. Apple's documentation states that view controllers are intended only to be used with full screen views, which are typically presented using one of the methods described above. It's possible to ignore Apple's advice and associate a view controller with a subview of another view controller, but you'll have to delegate the viewWill/DidAppear/Disappear calls from the container view controller to the nested controller manually.
Check that you've got the function name exactly right, for example:
- (void)viewWillAppear:(BOOL)animated
For example, if you forget to declare the animated parameter, your function won't get called.
This may sound obvious, but I've made this mistake and since the original post doesn't have a code sample I thought I'd share!