I am using storyboards, and I have one UIViewController that is being opened modally.
When the UIViewController is being loaded, it never hits any of the init methods
init, initwithcoder, or initwithnib.
Why not?
I need to initialize some variables there!!
awakeFromNib was the answer, #Fogmiester got it, don't know how to make it his answer though!!
you can use viewDidLoad function and it also call once for each control object.
Thanks
Related
I am curious about this method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
I find this method in UIViewController.h, and it's a regular method.
I check it, it is not a delegate method (it is not a protocol).
Because UITableViewController extends from UIViewController, we can use this method in our custom class for any table views.
Strangely, this method behaves like a delegate, it fires without any calls.
It fires when the view is going to do a transition.
As far as I know, this behaviour only exist in delegation.
How can a UIViewController calls prepareForSegue method in it's child class?
prepareForSegue is a method that is called as and when a transition is going to occur between ViewControllers. So, whenever a segue is made, this method will be called mandatorily. Now if your main View has a subclassed View and you want to perform a Segue from it, you can do it using the following function.
[self performSegueWithIdentifier:#"segueIdentifier" sender:self];
In this case, this method will invoke a transition with the specified identifier which corresponds to a segue. And whenever this method is called, prepareForSegue is called immediately after this, after which transition occurs.
For information, it is better to connect segue between ViewControllers rather than creating segue directly from Controls in the view. As sometimes, some conditions need to be checked before performing a Segue.
Hope this helps.
Like many other methods of UIViewController, your subclass can override the default behavior of many UIViewController methods. A couple of examples include viewDidLoad and viewWillAppear.
i m new to ios app development.i m currently learning to work with navigation controller.Can anyone plz explain how to call -(UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation: have implemented it in my code ,but it is not executing at all.
thankz in advance.
You don't call it, it's a delegate method that will be called by the navigation controller. You need to make whatever class you have the implementation of that method in, the delegate of the navigation controller.
The first method you mention is just a method which is called by the framework (if necessary) to get the "preferred Interface Orientation". For the system use this method you have to set more than one accepted interface orientation in your project setting and plist file.
The second method ´navigationController´ is inherited from ´UIViewController´ and gives a reference to the UINavigationController in which the UIViewController is embedded (means if there is no NavigationController it's nil). So it's a getter.
Now I think some more clarification in what you want to do with these methods would be useful, I mean you must have a reason why you ask, nobody just asks "why is there that method?"^^
I have problem with calling methods from one UIViewController by another UIViewController.
Currently I have UIScroll view with two UIViewControllers.
I want to change something in second one and see results in first one.
I try to do this in this way:
Inside function of second UIViewController:
-(void)doSomething:(){
FirsOneViewController *firstVC = [FirsOneViewController alloc] init];
[firstVC changeUnits:0];
}
Function is called but I don't se any changes in first controller.
BR,
Paul
From your code I see you create a new instance of FirstViewController and so there is no reason why the current instance inside the scrollview would receive this message.
You need to send the changeUnits: message to the current FirstViewController, so you need a reference to it. To do this you may want to think about creating a protocol, so that you parent container (the scrollview) is notified by the SecondViewController and then notifies the FirstViewController. A simpler(and lazier) solution is make the SecondViewController have a strong reference to the FirstViewController (though this solution may bite you in the future).
As said in other answers you are creating a new instance of FirsOneViewController instead of referencing to the one you already have.
Here are three ways of doing what you are asking:
Delegation:
The FirstViewController should be the delegate of the SecondViewController (as the secondViewController is calling methods on the FirstViewController). You should tell the SecondViewController that the FirstViewController is its delegate in what ever class initialises the two viewControllers.
From what you have said so far this seems like your best option.
NSNotification:
This could be good option if you think more than one object will want to listen to the change in the SecondViewController. Just post an NSNotification in the SecondViewController and add an NSNotification listener in the FirstViewController
Singleton:
if there should only ever be one instance of the FirstViewController in existence then make it a singleton. By making a class initialiser method. so that you can create/get the current instance of the object from anywhere in your appellation.
Hope this helped.
My viewDidLoad in a view controller is called twice. Once by [UIViewController View] and a second time by [UINib instanciateWithOwner:Options]. Why is this happening? Can it be prevented?
Any code you put inside of viewDidLoad should be able to run multiple times with out any issues. If you have code that only needs to run once for your controller use -awakeFromNib. The reason is because the view of the view controller can be unloaded and loaded multiple times. The code inside of viewDidLoad should only modify the UI to reflect the current state.
Now that I got that out of the way, your particular issue looks to be a bug. See Ned's answer.
Is this the same problem?
Why is viewDidLoad called twice when the rootViewController property of UIWindow is set?
Looks like it might be a bug in XCode 4.
You might have to check the object building mechanism. If there is only one nib file with reference to the controller, then this method should not be called multiple times. (unless if the object is getting rebuilt).
I think you might have to make your code within ViewDidLoad idemPotent. It is always better to make sure, that framework call back methods make this assumption.
There are two possibilities, whereby this issue happened in my iOS device frequently.
Rule #1: Do not call any view related setup in [init] function, all view related setup must be done in viewDidLoad and viewWillAppear.
Rule #2: Check viewDidLoad and viewWillAppear, are they calling correct super function? For example viewDidLoad -> super viewDidLoad and so on.
Hope this helps.
In my case, I used self.view (once) in viewDidLoad while calling viewDidLoad in my unit tests. This resulted in two calls. However, when I replaced [testedViewController viewDidLoad] with [testedViewController view], the double call problem was gone.
Debugging this showed that viewDidLoad was called a second time by #IBInspectable. The root controller is a UITabbarController. #IBInspectable was setting the tab in the storyboard. Not sure if this is a UIKit bug but try checking for this. You should never need to check viewDidLoad for double calls if your project is setup correctly.
I face a strange situation. In my controller, viewDidLoad is calling before init. Is there any technical reason behind that?
The viewDidLoad method is being called when accessing self.view inside the init method (since self.view should not yet be loaded from the nib the process seems to be fasten so it doesn't return nil).
I know this is a bit old post, but I'll post my point of view anywhere because I think it could help somebody.
Well, I've been in this same situation. I thought that viewDidLoad was being called before init method in my view controller class. But what was really happening was not that: the flow starts on init method, but jumps to viewDidLoad when calling [super init*], so my log messages in viewDidLoad method were being displayed first that those in my custom initialization.
I think that's it. I hope this to save some time to someone.
[Sorry for my English]
NOTE for UITabBarController:
I don't know what kind of UIViewController caused this for you but I faced a similar case with UITabBarController.
I thought it might help another one facing it with UITabBarController.
As far as I know all viewControllers call init before viewDidLoad, except for the UITabBarController and its subclasses.
As Andrew claims here, UITabBarControllers call loadView inside [super init] method, which causes the call to viewDidLoad. So the viewDidLoad method will be called before init has finished its job.
If you have some thing to setup in viewDidLoad you should perhaps do it inside init method after the call to [super init].
When you initialise a UIViewController from code, you use -initWithNibName:bundle:, whereas when it is initialised from a XIB, the XIB loading code will call -initWithCoder:. One, and only one of these two methods will be called, and they will definitely be called before -viewDidLoad.
There's no conceivable way that -viewDidLoad could be called first, unless you are calling it yourself (which you should never really do).
No, the viewDidLoad message is always called after init.
Are you sure init is called at all? There are several init methods especially for UIViewController, maybe another one is called instead making you think differently.
If you need more information, please paste the code of viewDidLoad and all of your init methods, and tell us how it is loaded (i.e. with code) or from a nib.
If your ViewController is being loaded from your main nib file, then most likely it is initWithCoder that is being called to initialize the controller.