A view in UITabBarController disappears after a memory warning - objective-c

I have a UINavigationController within one of the tabs of a UITabBarController.
I now present a new view controller (let’s call it Steve) over the whole app (using presentViewController:animated:completion:).
Then, I simulate low memory.
After dismissing Steve (using dismissViewControllerAnimated:completion:), I can now see that the UINavigationController’s view is gone; within the tab; only an empty white area is seen!
Why is this? I have tried calling the view methods on all imaginable controllers upon Steve’s dismissal, but the contents of the tab still stay empty (white).
The strange thing is this: If I click on another tab, and click back on the original tab, the contents (the navigation controller) shows just fine again. Is the tab bar controller doing something special to force the view to display?
UPDATE: I was able to “fix” my issue with this terrible code, just before dismissing Steve:
[[[[[self tabBarController] view] subviews] objectAtIndex:0]
addSubview:[[self navigationController] view]];
What this does is that it finds the subview of the tab bar controller which is not the tab bar (i.e. the top view), and then adds the navigation controller’s view to be its subview.
This is of course terrible, because it makes internal assumptions about the subview structure of the tab bar controller’s view.
If someone has any better solutions, please let me know about them.

When your app receives a memory warning, one of the first thing it will do is drop the view hierarchies of any view controllers that have loaded views but are not currently visible (like your UINavigationController). Most likely, whatever view controller is at the top of your navigation stack is having its views dropped but not reloading them when it reappears.
Always put your view construction code in -loadView or -viewDidLoad and not in -init. That way, the view controller will reconstruct your view if it's been dropped due to a memory warning.
(P.S.: The reason your hack works is the bit where you call [[self navigationController] view], which in turn calls -loadView on the top VC in the stack, forcing it to reconstruct its view.)

Related

Adding a view to a NavigationController that isn't pushed to the stack

I've added a UIToolbar to a NavigationController because I wanted to use the realtime blurring capabilities of the toolbar. I also wanted to customize the size - which means I can't use the toolbar built in to the NavigationController. I had to create my own and add it as a subview.
The problem is that I only want it on one particular view in my navigation stack. When I push subsequent views, the toolbar stays on the screen. I want it to be covered by the view pushed on the stack as the view slide-animates itself in to place.
How can I get it to do that without writing a custom animation?
[self presentViewController:MyController animated:YES completion:nil];
Presents a view like a modal. Exactly what I needed.

Increasing number of living Views

I've set up a really simple project using storyboards including two views as shown here: http://i.stack.imgur.com/iRx21.png. The navigation can be done by either selecting a cell in the custom table view or hitting the back button labelled with "<<". Everything works fine except the following:
when I switch between the views, every time an instantiation happens. The profiling shows an increasing number of view objects. I would like to keep only one of each view and instantiation should be happen only once. What am I doing wrong? (I'm using ARC.)
Thanks in advance!
You should not link your back button to the parent view controller. This is what causes the new instantiation.
The way to go is to embed the table view into UINavigationController (in IB, choose Editor -> Imbed In -> Navigation Controller. Then change your segue to a Push segue. You can of course hide the navigation bar etc. to make things look exactly as you like. Then, link the back button to the controller with an IBAction and in the handler do a simple
[self.navigationController popViewControllerAnimated:YES];
This would be the appropriate logic of what you are doing. Of course, you can also push the web view modally and then handle the button click with
[self dismissModalViewControllerAnimated:YES];

Tab Bar controller disappearing when moving to another view (iOS SDK, Using storyboards)

I am building an iPhone app using storyboards and I have a problem with the tab bar controller. On one of the views that is linked from the tab bar controller (view1), there is a button that leads to another view (view2). On View2, there is a button that leads back to View1. Very straight forward. But when I go from view1 to view2, the tab bar disappears, and even worse, when I go back to View1, the tab bar is still gone...
How can I fix that? (I have yet to put ANY code in the app, there is only the storyboard and the apple provided AppDelegate Class (and also a main file I suppose, but I am not intending on touching that).
Any Reply is Highly appreciated!
If you do a modal segue from a view that is a tab bar view, it will get rid of the tab bar for the modal view you are presenting.
Secondly, when you segue you are creating a new instance of the view controller. So I am guessing you are segueing from view1 to view2 and losing the tab bar, then you are segueing back to view1. At this point you have created view1, view2, and a second copy of view1 that does not have a tab bar.
I would suggest one of two things.
1.) If you want to keep the tabs at the bottom when you segue from view1 to view2, then click on view1, at the top of the screen select Editor/Embed In/ Navigation Controller. This will embed your view1 in a navigation controller. Then if you change your segue from Modal to Push it will keep your tab bars at the bottom. The navigation bar at the top also make it easy to go back from view 2 to view 1 the correct way (by popping the view) rather than creating a new segue. If you do not like the navigation bar, then you can change the "Top Bar" property to "None" in the inspector. You will then need to create some other way in view2 to get back to view1. (BY POPPING THE CONTROLLER, NOT BY SEGUEING)
2) If you don't want to set up a navigation controller you will have a little bit harder time keeping the tab bar stuff at the bottom of the view2 controller. In fact, I'm not sure you can do it at all with a modal segue, you'd probably have to write some type of custom segue. Either way, If you want to transition back to view1 and get to the correct controller (not a new version without the tabs) then you need to attach an action to whatever button you are using to segue and use the following code (I also attached the code for navigation controller push segues, in case you create a navigation controller and get rid of the navigation bar.)
For Modal Segue:
[self dismissModalViewControllerAnimated:YES];
For Push segue:
[self.navigationController popViewControllerAnimated:YES];
Your best bet is to use the navigation controller method, as you are assured to keep your tabs. You can then either use the navigation bar to return (the easy way, no code needed) or you can get rid of it and use a button and the code above.
Good luck!
I had the same problem, i know this is an old question but [self dismissModalViewControllerAnimated:YES]; is deprecated in iOS 6.
What i used is:
[self dismissViewControllerAnimated:YES completion:nil];

UIViewControllers problems

Hi there and thank you in advice for your help. I have a really strange problem while working with ViewControllers in Xcode4. First of all I have to say that I'm not using storyboards and I prefer to create any UI element programmatically. So I've set a UIButton and I want that, when pressed, it brings me to a new view controller. This is the code I'm using for a button:
-(void)settingsAndExportHandle:(UIButton *)buttonSender {
SettingsViewController* settingView = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
settingView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:settingView animated:YES];
}
This buttons is initialized and allocated in the viewDidLoad method of the RootViewController. I want to switch to the other view controller (in this case SettingsViewController) when I press the button.
The strange thing is that when I press the button, the animation that flips the controllers goes well, but when it finishes I obtain the EXACT same things that I had on the RootViewControllers (same custom views, same buttons, same all!). The question is: what I'm missing?? I have to say that I use ARC (automatic reference counting) so I can't release or dealloc the views and buttons I've created on my RootViewController.
Any help will be appreciated. Thank you all!
Pushing and and modally presenting view controllers does not deallocate the view controller that presented them. It simply adds the additional view controller to the stack. You'll need to implement a callback method so that when the user hits the button to flip back to root view controller, your settings view controller lets the root view controller know what's about to happen so you can call a method you've written to reset the interface back to whatever state you need it at. You may also be able to use viewWillAppear: but that's a little messy.
However, according to the Apple Human Interface Guidelines, the user expects that when they push a view controller or modally present it, the view controller they were on will save state and be exactly the way they left it when they came back. It's disconcerting and annoying when state is not preserved while in a navigation controller context. It is especially annoying when it's modally presented.
Think about this - A user is in a hypothetical Mail app. They start typing out an email and set a font size and a color. They tap on the add attachment button, which brings up a modal view controller that allows them to select a picture. They select the picture and the modal view is dismissed, and in your implementation, the mail composing interface would have reset and the email content would be gone or at the very least the selected font size and color would be back to the default. That's not a good experience.

How/whether to simulate a UINavigationController pushToViewController method

I’m designing the interface for an iOS app in which there are both hierarchical navigation modes and arbitrary view-swapping modes. The layout of any given view in one mode is distinct from that of the corresponding view in the other mode.
I want the user to be able to switch back and forth between these modes. Say an isolated subview has a corresponding detail view that is 2 levels down in the UINavigationController’s hierarchy. The user should be able to switch directly to that navigation detail view. From there, he should be able either to navigate up and down the UINavigationController hierarchy or to switch back to the isolated subview from which he came.
UINavigationController has a method popToViewController:animated: that will let you skip levels as you navigate back up the hierarchy, but it has no corresponding pushToViewController:animated: that would let you jump directly to a lower level in the hierarchy. That makes sense — the nav controller must push the intervening view/s onto the stack before it displays the target view. But the regular push methods display the intervening view/s, which I assume will cause a visual flash as well as exacting a performance hit.
If I simulate pushToViewController by calling pushViewController twice consecutively, with animated:NO for the intervening view and with the intervening view set temporarily to be transparent, will I get reasonable performance? Or, when the nav mode is requested, should I place the nav controller's stack behind the isolated views, do the multiple calls to pushViewController, and then move the nav controller to the front? Or should I forgo the use of UINavigationController altogether and do all the table-view-style navigation manually via UIView’s add/remove/insert/exchangeSubview?
I believe setViewControllers:animated: is going to help you do what you want.
When you want to add several view controllers to the stack and directly go to the last one, you can explicitly set the entire stack and have the UINavigationController animate navigation directly to the last one.
NSMutableArray* controllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
[controllers addObject:newController1];
[controllers addObject:newController2];
[self.navigationController setViewControllers:controllers animated:YES];
This will push newController1 and newController2 to your view stack and animate a transition to newController2.