Access UI components Inside controllers init method - iphone

I'm trying to hide a button inside the controller's init method. But its not working. I have created a outlet to the image to access it from the controller. Dont know why the image is not get hidden.But I can do it inside the viewdidload method. Is it because the view still not loaded?

Yes the view will be loaded when viewdidload is called. so any changes to the existing UI should be called in view did load or view will appear

The UI components you're talking about are all subviews of the UIView that is owned by the UIViewController that you're creating. But that UIView and its subviews aren't actually created at the time the init method is called, so you can't change them there.
viewDidLoad is the appropriate place for any code that only needs to run once after the view has been created, but before it has been displayed.
viewWillAppear: and viewDidAppear: will run every time the view is about to or just did become visible again respectively.

Actually, the whole view hierarchy will be loaded when the loadView message is sent to the controller. This message is also sent automatically the first time you access the controller's view. That's why your message to the button gets discarded in the init method, because the object is not yet 'unfrozen' from the xib/storyboard. And yes, the appropriate place to do your view customization would be inside the viewDidLoad delegate method.
PS. Also... if you want the button to be hidden upon initialization, why don't you just check its hidden property in your xib/storyboard ?

Related

Why is self.navigationController null in viewDidLoad?

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.

Run class viewDidLoad method from push button

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.

When is viewWillAppear called?

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.

Can viewDidLoad be called mutiple times?

The documentation says "This method is called after the view controller has loaded its associated views into memory." My questions are:
1) if I initialize a view controller is viewDidLoad called, or does the view actually have to be added as a superview of the current view for it to be called
2) If I add the view controllers view, and viewDidLoad is called, then I remove the view controllers view, then re-add it again later, will viewDidLoad be called again?
viewDidLoad is called whenever the view is loaded. This happens when the vc.view property is accessed. It typically happens right before adding the view to a view hierarchy, but it can happen earlier if the property is accessed earlier.
If you remove your view from a view hierarchy, and then a memory warning happens and viewDidUnload is called, then viewDidLoad will be called again if the view property is accessed again. But this is the only way for it to be called a second time; if your view never unloads, then viewDidLoad will never be repeated.

How to pop to rootview controll without using popToRootViewControll method?

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.