I have an iPad app with a UITabBarController with 7 view controllers.
This is in my applicationDidFinishLaunching:
// Override point for customization after application launch.
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = [self createViewControllers];
self.window.rootViewController = self.tabBarController;
And this also in my appDelegate:
- (NSArray *)createViewControllers
{
MyView1 *viewController1 = [[MyView1 alloc] initWithNibName:#"MyView1" bundle:nil];
MyView2 *viewController2 = [[MyView2 alloc] initWithNibName:#"MyView2" bundle:nil];
MyView3 *viewController3 = [[MyView3 alloc] initWithNibName:#"MyView3" bundle:nil];
MyView4 *viewController4 = [[MyView4 alloc] initWithNibName:#"MyView4" bundle:nil];
MyView5 *viewController5 = [[MyView5 alloc] initWithNibName:#"MyView5" bundle:nil];
MyView6 *viewController6 = [[MyView6 alloc] initWithNibName:#"MyView6" bundle:nil];
MyView7 *viewController7 = [[MyView7 alloc] initWithNibName:#"MyView7" bundle:nil];
NSArray *views = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, viewController5, viewController6, viewController7, nil];
return views;
}
At any time, I want the user to be able to completely reload those view controllers, so any changes made, any animations in progress, any subviews etc, are flushed from memory and the view controllers are all loaded afresh.
I found the simplest way to do this is to add a shake gesture in the appDelegate. The shake gesture pops up a UIAlertView which asks "Do you want to reset everything?" If yes, this happens:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
self.tabBarController.viewControllers = [self createViewControllers]; // <-- this happens
// some of the other things I've tried
// [self.tabBarController setViewControllers:[self createViewControllers] animated:YES]; // same as above but with animation
// self.tabBarController = [[UITabBarController alloc] init]; // allocations go crazy!
// self.tabBarController.delegate = self;
// self.tabBarController.viewControllers = [self createViewControllers];
// self.window.rootViewController = self.tabBarController;
}
}
I replace the array of view controllers with a freshly created set. You can see a couple of the other things I've tried as well.
What I don't understand is why every time the reset is performed, the allocations in Instruments go up (and stay up) and the app starts to slow down. The more I reset, the more it slows down until it becomes unusable.
I'm using ARC and I would expect it to take care of the memory management each time I refresh the view controllers in the tab bar controller. Yet each refresh eats up more memory, no matter how I do it.
What could it be that is slowing everything down?
Related
I'm having troubles hiding the UITabBarController which I defined as my rootViewController for the entire app.
I'm trying to hide the UITabBarController - which is the root view controller for the entire app - on the first view displayed. The idea is that the first view has UIImageView instances which jump to the defined UIViewControllers (which are also defined as view controllers of the root UITabBarController).
Is there a way to have the first view controller without the root UITabBarController but keep it for all other views defined as viewControllers?
Here's the code in AppDelegate defining the view controllers and the UITabBarController as rootViewController.
- (void)initViewControllers {
anIdeaVC = [[IdeaViewController alloc] initWithNibName:#"IdeaViewController" bundle:nil];
[anIdeaVC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:#"Idea" image:[UIImage imageNamed:#"iconIdee.png"] tag:0] autorelease]];
aListTableVC = [[ListTableViewController alloc] initWithStyle:UITableViewStylePlain];
[aListTableVC setTitle:#"List"];
aListNC = [[ListNavigationController alloc] initWithRootViewController:aListTableVC];
[aListNC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:#"List" image:[UIImage imageNamed:#"iconList.png"] tag:0] autorelease]];
anInnMapVC = [[MapViewController alloc] initWithNibName:#"MapViewController" bundle:nil];
anInnMapNC = [[InnMapNavigationController alloc] initWithRootViewController:anInnMapVC];
[anInnMapNC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:#"InnMap" image:[UIImage imageNamed:#"iconInnMap.png"] tag:0] autorelease]];
aSearchTableVC = [[SearchTableViewController alloc] initWithNibName:#"SearchTableViewController" bundle:nil];
[aSearchTableVC setTitle:#"Search"];
aSearchNC = [[SearchNavigationController alloc] initWithRootViewController:aSearchTableVC];
[aSearchNC setTabBarItem:[[[UITabBarItem alloc] initWithTitle:#"Search" image:[UIImage imageNamed:#"iconSearch.png"] tag:0] autorelease]];
tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:anIdeaVC, aListNC, anInnMapNC, aSearchNC, nil] animated:NO];
[tabBarController setSelectedViewController:anIdeaVC];
[tabBarController setDelegate:self];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self initViewControllers];
[window setRootViewController:tabBarController];
[window makeKeyAndVisible];
return YES;
}
Thanks in advance for your help :-).
I think the best way to go about this is to make the vc with the icons the root to begin with. Then, when user makes a selection, create the tab bar vc and make it the root.
Create a view controller (not just a view) to show the icons and get the user selection. Make that the window's root on launch...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// don't do this
//[self initViewControllers];
// or this
//[window setRootViewController:tabBarController];
// instead do this, create the vc that lets user select an icon
// put your icon view in there
IconSelectVC *iconSelectVC = [[IconSelectVC alloc] init];
[window setRootViewController:iconSelectVC];
[window makeKeyAndVisible];
return YES;
}
Add the initViewControllers method to your app delegate's public interface, so it can be called from IconSelectVC. Then add one last line to it to make it replace the window's root vc.
// ... the rest of initViewControllers, then
[tabBarController setSelectedViewController:anIdeaVC];
[tabBarController setDelegate:self];
[window setRootViewController:tabBarController];
}
Now, in your IconSelectVC when you decide it's time to change the UI, get the app delegate singleton and change the window's root.
// in IconSelectVC.m
// when you decide to change to the tab bar.
// Be aware that this vc will be released here, so do any cleaning you need to do here
// e.g. unsubscribe from NSNotifications, clean any timers, finish any asynch requests, etc.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate initViewControllers];
EDIT - We didn't discuss how this transition should look - my suggestion here will cause an "ugly" transition (in the eye of the beholder, of course) where the UI just changes in one frame. One way (among a few) to get a nicer transition would be to use os7 custom vc transitions.
I try to push a UIViewController onto a UINavigationController. The NavigationBar changes (i.e. a back-button appears) but the view is not pushed (*).
I have a UITabBarController as my applications RootViewController.
When I switch to another tab and then switches back, the view (*) gets pushed.
I have never seen this behaviour before. My problem is exactly the same as this, however the methods that solved that issue did not solve mine.
Initially
After I press the row
I understand that this question might be related to issues in AppDelegate, therefore i post the code I use.
Code:
in AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[MagicalRecord setupCoreDataStackWithStoreNamed:#"DBModel"];
/* CONTACTS LIST CONTROLLER */
BoonContactListViewController *contactListViewController = [[BoonContactListViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *contactListNavigationController = [[UINavigationController alloc] initWithRootViewController:contactListViewController];
[contactListNavigationController setValue:[[BoonNavigationBar alloc]init] forKeyPath:#"navigationBar"];
contactListNavigationController.tabBarItem.title = [NSLocalizedString(#"CONTACTS", nil) capitalizedString];
contactListNavigationController.tabBarItem.image = [UIImage imageNamed:#"menu_contacts.png"];
/* INVITATIONS */
BoonInvitationListViewController *invitationListController = [[BoonInvitationListViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *invitationNavigationController = [[UINavigationController alloc] initWithRootViewController:invitationListController];
[invitationNavigationController setValue:[[BoonNavigationBar alloc]init] forKeyPath:#"navigationBar"];
invitationNavigationController.tabBarItem.title = [NSLocalizedString(#"SETTINGS", nil) capitalizedString];
invitationNavigationController.tabBarItem.image = [UIImage imageNamed:#"menu_invitations.png"];
/* SETTINGS */
BoonSettingsViewController *settingsViewController = [[BoonSettingsViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *settingsNavigationController = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
[settingsNavigationController setValue:[[BoonNavigationBar alloc]init] forKeyPath:#"navigationBar"];
settingsNavigationController.tabBarItem.title = [NSLocalizedString(#"SETTINGS", nil) capitalizedString];
settingsNavigationController.tabBarItem.image = [UIImage imageNamed:#"menu_settings.png"];
/* TAB BAR */
BoonTabBarViewController *tabBarController = [[BoonTabBarViewController alloc] init];
tabBarController.viewControllers = #[contactListNavigationController, invitationNavigationController, settingsNavigationController];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
[tabBarController showLogin];
return YES;
}
EDIT:
In the viewController that i am trying to push, neither viewWillAppear, viewDidLoad nor viewDidAppear is called.
If I use presentViewController: animated: completion: I get the preferred behaviour, id rather not though
EDIT 2
How I push my new VC
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BoonContactInfoViewController *contactInfoViewController = [[BoonContactInfoViewController alloc] initWithNibName:nil bundle:nil];
NSLog(#"NAV %#", self.navigationController);
[self.navigationController pushViewController:contactInfoViewController animated:YES];
}
EDIT 3
It is only the initial tab that cannot push ... if i swap places of the first and second tab, i can push a view controller using in the way i do above.
EDIT 4
It works if i (in my tabBarController) calls
self.selectedIndex = 1;
self.selectedIndex = 0;
EDIT 5
- (void)showLogin
{
if([BoonUserHandler getLogin].length > 0 && [BoonUserHandler getPassword].length > 0){
return;
}
BoonWelcomeViewController *welcomeWC = [[BoonWelcomeViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *welcomeNavigationController = [[UINavigationController alloc] initWithRootViewController:welcomeWC];
[welcomeNavigationController setNavigationBarHidden:YES];
[self presentViewController:welcomeNavigationController animated:NO completion:nil];
}
What version of iOS are you developing for?
I'd first ask why you're hacking in a nav bar using:
[settingsNavigationController setValue:[[BoonNavigationBar alloc]init] forKeyPath:#"navigationBar"];
rather than the iOS5+ UINavigationController method:
- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass
But my overall suggestion would be to remove all this code and use a storyboard. This looks like the perfect opportunity.
I think you are getting wrong Navigation controller to push that's why it showing this problem..
You have to fetch right navigation controller from tab controller
self.tabBarController.selectedIndex = 0;
just change tab controller selected index
I have a family of devices that are very similar and are controlled by an applet with three tabs. Within each view controller, I make use of the navigation controller to expand into the set up of each one of those features.
The first tab, the 'input' tab, especially is quite different between these three devices so when it is detected that I've switched between devices, I perform the following thing in my application delegate:
if ([self IsCrescendo])
{
//thirdViewController is really the crescendo'a input view - I need to rename that mess one day
crescendoInputView = [[ThirdViewController alloc] init : (UIViewController*) currentViewController];
crescendoInputView.title = [[viewControllers objectAtIndex:INPUT_TAB_INDEX] title];
crescendoInputView.tabBarItem = [[viewControllers objectAtIndex:INPUT_TAB_INDEX] tabBarItem];
[viewControllers replaceObjectAtIndex:INPUT_TAB_INDEX withObject:crescendoInputView];
[crescendoInputView release];
[self.tabBarController setViewControllers:viewControllers animated:FALSE];
}
if ([self IsSpirito3])
{ // similar to above using obviously a different view controller
}
if ([self IsSonata])
{ // similar to above using obviously a different view controller
}
Initially, this app just controlled one device so when I first created it, I set the three tabs up in the main window's XIB which works well. It defaults to the original device and the navigation bar is in tact and working.
Now that there are more devices to control, I figured to just use a replaceObjectAtIndex so swap a new view controller in but my Navigation bar disappears.
I'd very much appreciate any light you may be able to shed on this.
Thanks!
Okay, after lots more head scratching, the following fixed it:
I had initially used the main window's XIB to instantiate the three tabs.
This works fine if you're not doing a replaceObjectAtIndex. When I did do a ReplaceObjectAtIndex, it would lose the navigation bar.
Instead, if you instantiate the tabs programmatically, along with each having its own navigation controller, you can replace tabs with impunity and not lose features like the navigation bar.
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
// Input view (defaults to Crescendo)
UIViewController *viewController1 = [[[ThirdViewController alloc] initWithNibName:#"ThirdView" bundle:nil] autorelease];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
// Volume View
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil] autorelease];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
// System view
UIViewController *viewController3 = [[[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nil];
.
.
}
Not sure why it does not work when you set it up from a XIB. I could swear I had it working on a previous version so maybe something changed and apple forgot to tell us about it.
I like this approach better anyway. It isn't the first time a 'wizard like' programming tool has bit me so maybe this will save someone out there a little time.
I'm trying to make it so I have a tab bar at the bottom of my screen, and it's always there. I also want it so if I "click" into some menu in one of the tabs, it gives you the option to go back, thus a navigation controller and a tab bar controller.
I still don't really understand iOS, so the answer I found is confusing me.
This Answer: Having a UITabBar AND a UINavigationController in an app?
So how do I implement this? I'm guessing I change this method in my App Delegate,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[CFSDFirstViewController alloc] initWithNibName:#"CFSDFirstViewController_iPhone" bundle:nil];
viewController2 = [[CFSDSecondViewController alloc] initWithNibName:#"CFSDSecondViewController_iPhone" bundle:nil];
} else {
viewController1 = [[CFSDFirstViewController alloc] initWithNibName:#"CFSDFirstViewController_iPad" bundle:nil];
viewController2 = [[CFSDSecondViewController alloc] initWithNibName:#"CFSDSecondViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Thanks for your assistance!
First of all you don't need to to manually set up the nib name. You could just name them CFSDFirstViewController~iphone.xib and CFSDFirstViewController~ipad.xib. Then you can call [[CFSDFirstViewController alloc] init] and let iOS do the rest for you. For info see iOS Supports Device-Specific Resources.
About your question, you can only insert the UINavigationController within the UITabBarController. To do it wrap viewController1 within a UINavigationController like the following:
CFSDFirstViewController viewController1 = [[CFSDFirstViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
and then use navController instead of viewController1 like the folliwing
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, nil];
Check the code because I wrote by hand. And pay attention to memory if you use a non-ARC project.
Hope it helps.
I had a navigation controller based application. And I decided to use tab bars in my application.
When the user presses at a certain tab bar item I want to display a certain view controller - and I want programmatically in my code choose which one to display.
I tried to add in the Interface Builder a navigation controller into my tab bar, but viewWillAppear of its view controller is not being called.
How can I implement this feature?
I don't know if it's the "right way", but here's how I usually do this with three tabs.
- (void)initControls {
// Create the window.
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
// Create Tab Bar.
tabCon = [[UITabBarController alloc] init];
// Local array variable that holds the viewcontrollers.
// Capacity corresponds to the number of VC's
NSMutableArray *localVCArray = [[NSMutableArray alloc] initWithCapacity:3];
MyFirstViewController *oneViewController = [[MyFirstViewController alloc] init];
UINavigationController *oneNavCon = [[UINavigationController alloc] initWithRootViewController:oneViewController];
[localVCArray addObject:oneNavCon];
[oneViewController release];
[oneNavCon release];
MySecondViewController *twoViewController = [[MySecondViewController alloc] init];
UINavigationController *twoNavCon = [[UINavigationController alloc] initWithRootViewController:twoViewController];
[localVCArray addObject:twoNavCon];
[twoViewController release];
[twoNavCon release];
MyThirdViewController *threeViewController = [[MyThirdViewController alloc] init];
UINavigationController *threeNavCon = [[UINavigationController alloc] initWithRootViewController:threeViewController];
[localVCArray addObject:threeNavCon];
[threeViewController release];
[threeNavCon release];
// Set the tab bars array of view controllers to the localVCArray
[[self tabCon] setViewControllers:localVCArray animated:YES];
// Release the localVCArray, all of its contents are now retained by tabCon.
[localVCArray release];
// Add controls to window and show.
[window addSubview:[tabCon view]];
[window makeKeyAndVisible];
}
In the init method each viewController you can do something like:
[[self tabBarItem] setImage:[dataSource tabConImg]];
[[self tabBarItem] setTitle:[dataSource name]];
[[self navigationItem] setTitle:[dataSource navConName]];
To set the icon used in the tab bar, the title in the tab bar, and the title of you navigation item.