I'm currently building a little sample iOS app, i developed my UIViewControllers and views programatically, i'm targeting iOS 7+ devices and i have a simple question :
Here how i show a new controller
MySuperController *superController = [[MySuperController alloc] init];
[self.navigationController showViewController:superController sender:self.navigationController];
First i wanted to know if it's the correct way to show another view controller ?
Second imagine i'm performing those instructions in a LoginViewController that will be displayed just once (typically when the user launches the app) how can i release this loginviewcontroller after creating and showing another view controller ?
i know this question has been already asked but all the solutions presented are old/inappropriate (my sample app is ARC enabled which is by default enabled i think)
I'm new to this environnement any help/indication is appreciated thank you
As Roy Nakum says in his comment, if you are using ARC, your code is fine. You create your view controller using a local strong variable, then present it. At that point the navigation controller takes ownership of it. Since your strong reference is a local variable, it does not keep ownership after your method returns.
However there is another problem with your code. This line will likely cause you problems:
MySuperController *superController = [[MySuperController alloc] init];
You should not use init to create a view controller. It won't have any contents. You should either use initWithNibName:bundle: (to load a view controller from a NIB) or instantiateViewControllerWithIdentifier (to load a view controller from a storyboard.)
It is possible to set up a view controller so its "plain" init method loads it's views, but it takes special handling in the init method, and it's not the normal way to do things.
That's a good way to present a ViewController. If you have ARC enable (default) don't worry about releasing, it will release automatically.
Related
I have been searching all over the web but I can't seem to find the answer to this.
Currently i am using presentViewController to start new ViewControllers, but on certain view controllers i do not dismiss it and call over it. I currently am not using any navigation controllers or anything like that.
I am just worried that if I call the same viewController again via presentViewController, that the same viewController would have 2 running instances.
Is it possible? Or does the iOS framework automatically reuse the idle viewController?
If so, how do i remove the idle view controllers?
Thank you! (I was holding back my question and tried to find it all over the web, so if you can point me in the right direction, it would be very helpful thanks!)
iOS will not reuse your view controller, you can easily check it yourself by printing your view controller in viewDidLoad, you will notice first that viewDidLoad is called every time, and next that all objects have different addresses.
Unless you create thousand of them, or the navigation of your app doesn’t let you come back to an “idle” view controller, I would not say this is an issue though.
I don’t see any clean way to remove a view controller from the memory without calling “dismiss”. You could try to:
- “refresh” your view with new data.
- use something like UIPageViewController if the workflow of your app allows this kind of behaviour.
- rework the navigation so you can dismiss the view before calling another one
Good luck
so I am building a simple chat app. I will have a login screen, register and later on a UITableViewController so show the friend list. I'm not sure if I should use a UINavigationController for this or just stick to UIViewControllers. Below are two images for the potential setups.:
and:
I'm just wondering is it generally better practice to use a navigation controller? I am also a little confused about what happens to a view controller when it is popped. Does [self.navigationController popViewControllerAnimated:YES]; keep the view controller in the memory to be accessed again later or is a new one created each time? The same question for [self dismissViewControllerAnimated:YES completion:nil], is this destroying the controller or storing it for use later? Thanks
I'll give you my opinion about when to use a navigation controller.
If your app need to present its content in a hierarchical fashion (Master/detail) is pretty common to use a UINavigationController.
If you need to present some content that is not strictly related with the presenting content you can present it using a new view controller on top of it.
Regarding the memory, as soon as you don't keep any reference to the controllers either presented or pushed once remove (popped / dismissed) you loose any reference to them so no space used in memory
I think you will stick to the UINavigationViewController. Simply because the login and registration are belonged to your user management. From the UX standpoints, it's better to use Modal if the scene you are going to present is somehow irrelevant to the current scene. Therefore, go with UINavigationViewController for your situation.
The second question is related to memory management. It's kind of like the reference counting. If you have something to reference the UIViewController, its reference counting won't drop to zero which mean the system won't clean this up. So, you still have way to get then. If you just simply pop then up or dismiss it without referencing it. The reference counting will become zero and the system will clean it.
Note: If you want to simply display a view controller which doesn't need hierarchical navigation or a navigation bar then you wouldn't need to use a UINavigationController.
But in this case I would still suggest to use UINavigationController for the simple fact that your use case (Login Flow) can be completely addressed using a navigation flow.
In terms of memory there is now difference. Feel free to use any of them in this concern. Although how these views are presented has difference.
I have 3 view controllers that I'm navigating between. When I open my app, I start at Controller1, which I can then use to navigate to Controller2 or Controller3. I can navigate to each of them fine individually, however, after I go to Controller3, return to Controller1, then try to navigate to Controller2, I get an EXC_BAD_ACCESS with code = 1. There is no exception or error message given at all, it just takes me to my AppDelegate file and gives me that error code.
I don't know what the issue is, but something that seems relevant is that I'm setting Controller3 as the navigation controller's delegate. I have a fourth navigation controller that also is set as a delegate as well, and causes the same behavior when I from 1->4->1->2, just like with 1->3->1->2. I have no issue going from 1->4->1->3 or 1->3->1->4, only when 2 is involved. I'm not sure if the issue is the delegates, and the fact that 2 isn't being set as one. Once again, I can navigate to it fine by itself, but not after navigating to one of the other 2 sub-view controllers.
If you set Controller3 or Controller4 as the navigation controller delegate then you need to clear it in the viewWillDisappear function of those classes otherwise you will end up with an invalid reference and that is what is causing your crash
I ran into this same kind of crash, so I will share what the issue was for me:
I had created a subclass of UIWebView.
In my storyboard, I dragged a UIWebView onto the canvas and changed it's class to be my subclass.
That was all working. The app had already passed through our QA team, was "accepted" by the business owner, and we were ready to push it to the app store.
Then I was told, "You can't use UIWebView. You must use WKWebView."
Fine, I changed my subclass to inherit from WKWebView, tweaked my internal class logic, and....splat. Trying to segue to that view controller would crash just as noted by the OP.
The problem was my storyboard: Because it was trying to instantiate my subclass, it was basically attempting to create a WKWebView which is NOT necessarily supported in Interface Builder (my friend says it might be ok in later versions, but I didn't verify).
TL;DR
The moral of the story is that if you have a subclassed object on your story board whose ancestor can't be dragged from the toolbox, then you will probably crash when you segue.
I'm working on a car app.
I have a tabBar linked to different view controllers (CarInfoViewController is one of them).
I'm passing data to one of the view controller using the following way
CarInfoViewController *myCarInfoController = [self.tabBarController.viewControllers objectAtIndex:2];
[myCarInfoController setSearchParam:vin];
I want to know what is the difference between getting the view controller directly from the TabBar using objectAtIndex vs. doing the following :
CarInfoViewController *myCarInfoController = [[CarInfoViewController alloc] init];
[myCarInfoController setSearchParam:vin];
Both ways are working! I'm curious to see what is the best practice in such case.
I doubt that both work fine.
Unless, of course, when you create the view controllers programmatically anyway and it is the very myCarInfoController object that you created and add it to the tab bar controller later.
With your first statement you receive a controller from your tab bar. That may well be a controller which was created in Interface Builder or Storyboard Editor respecteively. Of this view controller you know that it is the one in tab no. 3 (index no. 2)
Your second statement allocated a brand new instance of your CarInfoViewController. Assuming that there is one already that was created before (in IB or so) and resides as 3rd controller in your tab bar controller, then you have two intances of the same class which are not related to each other. if you know pass any data to myCarInfoController (of variant 2) then nothing at all will happen in/to that very instance that is acutally used by the tab bar controller.
Create a new single-view project (e.g., 'Test')
Within the main storyboard, create two view controllers with titles One and Two - make One the initial view controller
Place the label One within the content of view controller One and label Two within Two
Include the following within the viewDidLoad of TestViewController.m:
// instantiate the new view controller
UIStoryboard *storyboard = self.storyboard;
TestViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Two"];
// Change the view
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:viewController animated:YES completion:nil];
Save, build, and run.
I consistently get an error of the Storyboard does not contain view controller 'Two' variety. Using breakpoints, I've discovered that the problem is at the instantiation step. Yet the above code is taken directly from Apple's View Controller Programming Guide.
I've combed this site and discovered many people having problems with instantiating view controllers programatically. Any definite solution?
There's no bug here -- you're just writing inappropriate code. The code snippet you've given works fine if you put it in an action and trigger it with a button, a timer, etc. But you're attempting to present another view controller modally before the view controller running the code has even gotten around to displaying its own view.
-viewDidLoad is called when the view controller's view has been loaded from the storyboard or .xib file; it's an opportunity to do any initialization that had to be deferred until the view hierarchy comes into existence. However, the view isn't actually displayed at that point. You need to wait until you get a -viewDidAppear message to know that the view is on screen. So, you can imagine that it doesn't make a lot of sense to try to present some other view controller before the current one has even settled in.
I consistently get an error of the Storyboard does not contain view controller 'Two' variety.
In that case, you haven't properly assigned a storyboard identifier to the view controller. Select view controller "Two" in the storyboard editor and then look at the identity inspector. You need to set the identifier like this:
I've combed this site and discovered many people having problems with
instantiating view controllers programatically. Any definite solution?
Did you also look at the answers to their questions? That's how this site works -- we answer questions not just for the people that are asking them, but also to help others in the future who may have similar questions. Identifying a UIStoryboard is a good example of a question similar to yours with an answer that probably would have helped you.