I have a UITabBarController that is loading a UINavigationController.
When I push a new view controller, is it possible to change the UITabBarController to a new set of tabs?
Sure, why not just re-assign your new UIViewController's array to UITabBarController's viewControllers property? That will do the trick.
- (void)addNewControllers {
NSMutableArray *controllers = [NSMutableArray array];
UIViewController *c0 = [[[UIViewController alloc] init] autorelease];
[controllers addObject: c0];
UIViewController *c1 = [[[UIViewController alloc] init] autorelease];
[controllers addObject: c1];
UIViewController *c2 = [[[UIViewController alloc] init] autorelease];
[controllers addObject: c2];
[myTabBarController setViewControllers:controllers];
}
Read more about the potential side effects here.
Related
i am new in xcode development. I want to create a custom tab bar not using the tabbar application or using the storyboard , i want to make it programmatically . Can it be possible. I have go-through with this Video Tutorial but when i am trying to release the tab, there giving error. Can anyone help me.Here is my code,
main_tab = [[UITabBarController alloc] init];
viewController1 = [[Firstview alloc] init];
viewController1.title = #"View 1";
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
viewController2 = [[SecondView alloc] init];
viewController2.title = #"View 2";
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
And i am using xcode 4.2
When using ARC, you do not need to retain or release objects anymore.
You can enable or disable ARC by clicking on the project file and selecting the corresponding property. Here you have a good description.
if you are using storyboard then there should be starting point.
so take a uitabarcontroller in storyboard as starting point and link a reference to tabBarController
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
UIViewController *vc;
vc = [[UIViewController alloc] init];
vc.title = #"A";
[listOfViewControllers addObject:vc];
[vc release];
vc = [[UIViewController alloc] init];
vc.title = #"B";
[listOfViewControllers addObject:vc];
[vc release];
vc = [[UIViewController alloc] init];
vc.title = #"C";
[listOfViewControllers addObject:vc];
[vc release];
[self.tabBarController setViewControllers:listOfViewControllers
animated:YES];
}
Without storyboard and without taking UITabbarController in XIB
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
UIViewController *vc;
vc = [[UIViewController alloc] init];
vc.title = #"A";
[listOfViewControllers addObject:vc];
[vc release];
vc = [[UIViewController alloc] init];
vc.title = #"B";
[listOfViewControllers addObject:vc];
[vc release];
vc = [[UIViewController alloc] init];
vc.title = #"C";
[listOfViewControllers addObject:vc];
[vc release];
[tabBarController setViewControllers:listOfViewControllers
animated:YES];
In my function - (void)viewDidLoad:
FirstViewController *first = [[FirstViewController alloc]init];
SecondViewController *second = [[SecondViewController alloc]init];
ThirdViewController *third = [[ThirdViewController alloc]init];
ForthViewController *forth = [[ForthViewController alloc]init];
FifthViewController *fifth = [[FifthViewController alloc]init];
first.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"first" image:[UIImage imageNamed:#"FirstTab.png"] tag:0];
second.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"second" image:[UIImage imageNamed:#"SecondTab.png"] tag:1];
third.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"third" image:[UIImage imageNamed:#"ThirdTab.png"] tag:2];
forth.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"forth" image:[UIImage imageNamed:#"ForthTab.png"] tag:3];
fifth.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"fifth" image:[UIImage imageNamed:#"FifthTab.png"] tag:3];
UINavigationController *navigationFirst = [[UINavigationController alloc]initWithRootViewController:first];
UINavigationController *navigationSecond = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navigationThird = [[UINavigationController alloc]initWithRootViewController:third];
UINavigationController *navigationForth = [[UINavigationController alloc]initWithRootViewController:forth];
UINavigationController *navigationFifth = [[UINavigationController alloc]initWithRootViewController:fifth];
// [first release];
// [second release];
// [third release];
// [forth release];
// [first release];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:navigationFirst,navigationSecond,navigationThird,navigationForth,navigationFifth, nil];
[navigationFirst release];
[navigationSecond release];
[navigationThird release];
[navigationForth release];
[navigationFifth release];
self.tabbarController = [[UITabBarController alloc]init];
self.tabbarController.view.frame = CGRectMake(0, 0, 320, 480);
self.tabbarController.viewControllers = array;
[array release];
I intend to add five UINavigationControllers to tabcontroller,if I don't comment the fellowing code,it will crash:
// [first release];
// [second release];
// [third release];
// [forth release];
// [first release];
but I want to know what's the problem,I think it is correct to add thees code.
you are releasing first object twice. thats why your code is crashing.
replace [first release] with [fifth release]
You should release Navigation controllers in the end
UINavigationController *navigationFirst = [[UINavigationController alloc]initWithRootViewController:first];
UINavigationController *navigationSecond = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navigationThird = [[UINavigationController alloc]initWithRootViewController:third];
UINavigationController *navigationForth = [[UINavigationController alloc]initWithRootViewController:forth];
UINavigationController *navigationFifth = [[UINavigationController alloc]initWithRootViewController:fifth];
[first release];
[second release];
[third release];
[forth release];
[first release];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:navigationFirst,navigationSecond,navigationThird,navigationForth,navigationFifth, nil];
self.tabbarController = [[UITabBarController alloc]init];
self.tabbarController.view.frame = CGRectMake(0, 0, 320, 480);
self.tabbarController.viewControllers = array;
[navigationFirst release];
[navigationSecond release];
[navigationThird release];
[navigationForth release];
[navigationFifth release];
[array release];
Basically I want to make a login screen in my iPhone app before I show my TabBarController. I tried the following approach, adding first to the window subview my TabBarController and on top my LoginViewController. What am I doing wrong or should I do it completely different?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];
DefaultViewController *dvc = [[DefaultViewController alloc] init];
UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
dvc_nc.tabBarItem.title = #"Home";
//dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default" ofType:#"png"]];
[tabItems addObject:dvc_nc];
[dvc release];
[dvc_nc release];
OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
ovc_nc.tabBarItem.title = #"Option";
//ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Option" ofType:#"png"]];
[tabItems addObject:ovc_nc];
[ovc release];
[ovc_nc release];
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = tabItems;
self.tabController = tbc;
[tabItems release];
[tbc release];
[self.window addSubview:self.tabController.view];
LoginViewController *lvc = [[OptionsViewController alloc] init];
UINavigationController *lvc_nc = [[UINavigationController alloc] initWithRootViewController:lvc];
[self.window addSubview:lvc_nc.view];
[lvc release];
[lvc_nc release];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];
DefaultViewController *dvc = [[DefaultViewController alloc] init];
UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
dvc_nc.tabBarItem.title = #"Home";
//dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default" ofType:#"png"]];
[tabItems addObject:dvc_nc];
[dvc release];
[dvc_nc release];
OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
ovc_nc.tabBarItem.title = #"Option";
//ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Option" ofType:#"png"]];
[tabItems addObject:ovc_nc];
[ovc release];
[ovc_nc release];
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = tabItems;
self.tabController = tbc;
[tabItems release];
[tbc release];
[self.window addSubview:self.tabController.view];
LoginViewController *lvc = [[OptionsViewController alloc] init];
UINavigationController *lvc_nc = [[UINavigationController alloc] initWithRootViewController:lvc];
[self.tabController presentModalViewController:lvc_nc animated:no];
[lvc release];
[lvc_nc release];
return YES;
}
I would use two different views. One will handle the login proccess and one is your "Logged in view" that provides the features of your app. When your app starts up, you add the login view, check username/password and when the login went fine you switch to your second view.
i had programetically add the tabbar as shown below:-
FirstViewController *obj_FirstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil]; SecondViewController *obj_SecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil]; ThirdViewController *obj_ThirdViewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
navigation1 = [[UINavigationController alloc] initWithRootViewController:obj_FirstViewController];
navigation2 = [[UINavigationController alloc] initWithRootViewController:obj_SecondViewController];
navigation3 = [[UINavigationController alloc] initWithRootViewController:obj_ThirdViewController];
MainTabBar = [[UITabBarController alloc] init];
MainTabBar.delegate=self;
[MainTabBar setViewControllers:[NSArray arrayWithObjects:navigation1,navigation2,navigation3,nil]];
MainTabBar.view.frame=self.view.frame;
MainTabBar.selectedIndex=0;
[self.view addSubview:MainTabBar.view]
By writing this in (void)viewDidLoad i got the 3 tab in my viewcontroller. But the problem is i want to set the name of the tab as 1)Home 2)Favorites 3)About us
I had tried by writing the below code:
- 1)obj_FirstViewController.tabBarItem.title=#"Home"; 2)self.title = #"My View Controller";
But this does not work. Can anyone please help me how to do this programtically. Where to write the line so that i get this 3 name in my tabbar
Try this way...
NSMutableArray *controllers = [[NSMutableArray alloc] init];
FirstViewController *obj_FirstViewController = [[FirstViewController alloc] init];
[obj_FirstViewController setTitle:#"first"];
UITabBarItem *item = [[[UITabBarItem alloc] setTabBarItem: [[[UITabBarItem alloc] initWithTitle: #"First") image:[UIImage imageNamed:#"first.png"] tag:2] autorelease]];
[obj_FirstViewController setTabBarItem:item];
[controllers addObject:obj_FirstViewController];
[obj_FirstViewController release];
Pls Try this
FirstViewController *obj_FirstViewController = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *obj_SecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController *obj_ThirdViewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
navigation1 = [[UINavigationController alloc] initWithRootViewController:obj_FirstViewController];
navigation2 = [[UINavigationController alloc] initWithRootViewController:obj_SecondViewController];
navigation3 = [[UINavigationController alloc] initWithRootViewController:obj_ThirdViewController];
navigation1.title=#"Home";
navigation2.title=#"Second";
MainTabBar = [[UITabBarController alloc] init];
MainTabBar.delegate=self;
[MainTabBar setViewControllers:[NSArray arrayWithObjects:navigation1,navigation2,navigation3,nil]];
MainTabBar.view.frame=self.view.frame;
MainTabBar.selectedIndex=0;
[self.view addSubview:MainTabBar.view]
how can i ask an array if it contains an item and if it does it to [[NSArray alloc] initWithObjects:#"those objects" automatically.
this is my fav .h
#interface FavoriteViewController : UITableViewController {
NSMutableArray *favoritesArray;
NSMutableArray *didContain;
}
#property (nonatomic, retain) NSMutableArray *favoritesArray;
#property (nonatomic, retain) NSMutableArray *didContain;
this is the .m
favoritesArray = [[NSMutableArray alloc]init];
didContain = [[NSMutableArray alloc]init];
if
([favoritesArray containsObject:#"one"])
{
[didContain addObject:#"one"];
}
and in the detail view controller.m i have this code
[[NSMutableArray alloc] init];
[favoritesArray addObject: #"one"];
i get the table to work however nothing shows up....
NSArray *yourArray = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSMutableArray *didContain = [[NSArray alloc] init];
if([yourArray contains: #"Hello"])
{
[didContain addObject:#"Hello"];
}
or
NSArray *yourArray = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSMutableArray *didContain = [[NSArray alloc] init];
[didContain addObject: [yourArray objectAtIndex:[yourArray indexOfObject:#"Hello"]];
All of this and more is readily available in the apple docs. Please do some google searching first next time. Good luck hope this helps.
Use filteredArrayUsingPredicate:
See NSArray Class Reference and Predicate Programming Guide
It appears that you are trying to use an uninitialized property in your detail view controller.
Normally, you initialize properties in your init: or viewDidLoad method implementations then before presenting the view in your parent view controller, set the property using the property accessors
This line:
// DetailViewController.m initializer code
[[NSMutableArray alloc] init]; // returned object is not used
Should be:
favoritesArray = [[NSMutableArray alloc] init]; // view controller initialization code
Instead of calling this:
[favoritesArray addObject:#"one"];
After you create your detailViewController set the favoritesArray with the filtered array
// FavoriteViewController.m
MyDetailViewController *dvc = [[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
// populate the array
[dvc setFavoritesArray:didContain];
// Assuming you are using a navigation controller
[navigationController pushViewController:dvc animated:YES];
[dvc release];