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];
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];
Hi friend I want to Develop an application for windows base First page of the loading page then after the next view i want to add UitabbarController. i have implement this code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
flag=1;
splash *objSplash=[[splash alloc]initWithNibName:#"splash" bundle:nil];
Home *placeholder1 = [[Home alloc] initWithNibName:#"Home" bundle:nil];
placeholder1.title=#"Home";
placeholder1.tabBarItem.image=[UIImage imageNamed:#"home1.png"];
shopHome *placeholder2 = [[shopHome alloc] initWithNibName:#"shopHome" bundle:nil];
placeholder2.title=#"Store";
placeholder2.tabBarItem.image=[UIImage imageNamed:#"Shop.png"];
Search1 *placeholder3 = [[Search1 alloc] initWithNibName:#"Search1" bundle:nil];
placeholder3.title=#"Search";
placeholder3.tabBarItem.image=[UIImage imageNamed:#"search2.png"];
info *placeholder4 = [[info alloc]initWithNibName:#"info" bundle:nil];
placeholder4.title=#"Info";
placeholder4.tabBarItem.image=[UIImage imageNamed:#"info1.png"];
CartView *placeholder5=[[CartView alloc]initWithNibName:#"CartView" bundle:nil];
placeholder5.title=#"Cart";
placeholder5.tabBarItem.image=[UIImage imageNamed:#"Cart.png"];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] init];
UINavigationController *localNavController;
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder1];
localNavController.navigationBar.tintColor=[UIColor blackColor];
// Add navigation controller to the local vc array (1 of 2)
[localViewControllersArray addObject:localNavController];
[localNavController release];
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder2];
[localViewControllersArray addObject:localNavController];
[localNavController release];
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder3];
[localViewControllersArray addObject:localNavController];
[localNavController release];
if(flag==1)
{
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder5];
[localViewControllersArray addObject:localNavController];
[localNavController release];
}
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder4];
[localViewControllersArray addObject:localNavController];
[localNavController release];
tabBarController = [[UITabBarController alloc] init];
//tabBarController.viewControllers = [NSArray arrayWithObjects:placeholder1, placeholder2, placeholder3, placeholder4, nil];
tabBarController.viewControllers = localViewControllersArray;
//tabBarController.tintColor=[UIColor colorWithRed:160/256.0 green:82/256.0 blue:45/256.0 alpha:1.0];
//tabBarController.tabBar.backgroundColor=[UIColor colorWithRed:160/256.0 green:82/256.0 blue:45/256.0 alpha:1.0];
tabBarController.delegate=self;
[placeholder1 release];
[placeholder2 release];
[placeholder3 release];
[placeholder4 release];
[self.window addSubview:objSplash.view];
//[self.window addSubview:navigationController.view];
//[self.window addSubview:tabBarController.view];
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(goToNext) userInfo:nil repeats:NO];
flag=1;
[self parsingData];
[window makeKeyAndVisible];
return YES;
}
-(void)goToNext
{
//[self.navigationController.view addSubview:tabBarController.view];
[self.window addSubview:tabBarController.view];
}
Simply try to implement this one :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
splash *objSplash=[[splash alloc]initWithNibName:#"splash" bundle:nil];
Home *placeholder1 = [[Home alloc] initWithNibName:#"Home" bundle:nil];
placeholder1.title=#"Home";
placeholder1.tabBarItem.image=[UIImage imageNamed:#"home1.png"];
shopHome *placeholder2 = [[shopHome alloc] initWithNibName:#"shopHome" bundle:nil];
placeholder2.title=#"Store";
placeholder2.tabBarItem.image=[UIImage imageNamed:#"Shop.png"];
Search1 *placeholder3 = [[Search1 alloc] initWithNibName:#"Search1" bundle:nil];
placeholder3.title=#"Search";
placeholder3.tabBarItem.image=[UIImage imageNamed:#"search2.png"];
info *placeholder4 = [[info alloc]initWithNibName:#"info" bundle:nil];
placeholder4.title=#"Info";
placeholder4.tabBarItem.image=[UIImage imageNamed:#"info1.png"];
CartView *placeholder5=[[CartView alloc]initWithNibName:#"CartView" bundle:nil];
placeholder5.title=#"Cart";
placeholder5.tabBarItem.image=[UIImage imageNamed:#"Cart.png"];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] init];
UINavigationController *localNavController;
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder1];
localNavController.navigationBar.tintColor=[UIColor blackColor];
// Add navigation controller to the local vc array (1 of 2)
[localViewControllersArray addObject:localNavController];
[localNavController release];
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder2];
[localViewControllersArray addObject:localNavController];
[localNavController release];
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder3];
[localViewControllersArray addObject:localNavController];
[localNavController release];
if(flag==1)
{
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder5];
[localViewControllersArray addObject:localNavController];
[localNavController release];
}
localNavController = [[UINavigationController alloc] initWithRootViewController:placeholder4];
[localViewControllersArray addObject:localNavController];
[localNavController release];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = localViewControllersArray;
tabBarController.delegate=self;
[placeholder1 release];
[placeholder2 release];
[placeholder3 release];
[placeholder4 release];
[self.window addSubview:objSplash.view];
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(goToNext) userInfo:nil repeats:NO];
[window makeKeyAndVisible];
return YES;
}
-(void)goToNext
{
[self.window addSubview:tabBarController.view];
}
#implementation Home
- (id)initWithTitle:(NSString*)theTitle {
if (self = [super init]) {
self.tabBarItem.image = [UIImage imageNamed:#"Home.png"];
self.title = theTitle;
}
return self;
}
Note :
This is already tested solution hope it will work for you as well.
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]
So what I'm trying to do is when you click view on receiving a push notification when the app opens it has the presented view push a new controller with details regarding the notification. I'm using a UITabBarController with UINavigationControllers. Any help would be much appreciated, I've tried searching but I couldn't seem to find anything that pointed me in the right direction. Current code below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
tabBarController = [[UITabBarController alloc] init];
controller = [[controller alloc] init];
UINavigationController *controller1 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller1.tabBarItem.image = [UIImage imageNamed:#"icon_news.png"];
[controller setTitle:#"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller2 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller2.tabBarItem.image = [UIImage imageNamed:#"icon_news.png"];
[controller setTitle:#"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller3 = [[[UINavigationController alloc] initWithRootViewController:controller3] autorelease];
controller3.tabBarItem.image = [UIImage imageNamed:#"icon_news.png"];
[controller setTitle:#"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller4 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller4.tabBarItem.image = [UIImage imageNamed:#"icon_news.png"];
[controller setTitle:#"View"];
[controller release];
controller = [[controller alloc] init];
UINavigationController *controller5 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller5.tabBarItem.image = [UIImage imageNamed:#"icon_news.png"];
[controller setTitle:#"View"];
[controller release];
tabBarController.viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, controller5, nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
launchDefault = YES;
//[self performSelector:#selector(handlePostLaunch) withObject:nil afterDelay:0];
// Push Notification info
NSDictionary *apns = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *result = [[[apns valueForKey:#"aps"] valueForKey:#"alert"] valueForKey:#"loc-args"];
NSString *playerID = [NSString stringWithFormat:#"%#", result];
playerID = [[playerID componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:#""];
playerID = [playerID stringByReplacingOccurrencesOfString:#" " withString:#""];
playerID = [playerID stringByReplacingOccurrencesOfString:#"(" withString:#""];
playerID = [playerID stringByReplacingOccurrencesOfString:#")" withString:#""];
NSLog(#"Player ID: %#", playerID);
if (![playerID isEqualToString:#"null"]) {
if (!detailViewController) {
detailViewController = [[PlayerDetailViewController alloc] init];
}
NSManagedObjectContext *moc = [[AppController sharedAppController] managedObjectContext];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Players"
inManagedObjectContext:moc];
[req setEntity:entity];
NSPredicate *pre = [NSPredicate predicateWithFormat:#"playerID=%#", playerID];
[req setPredicate:pre];
NSError *error;
NSArray *list = [moc executeFetchRequest:req error:&error];
[req release];
Players *player = [list lastObject];
[detailViewController setPlayer:player];
//Want to Push view here
[detailViewController release];
detailViewController = nil;
}
return YES;
}
If application is launched by a notification then it will be present in the application delegate's application:didFinishLaunchingWithOptions: launchOptions dictionary under the key UIApplicationLaunchOptionsRemoteNotificationKey, which has all the info the notification has (JSON converted to NSDictionary I believe).
EDIT:
Got the question wrong, I think what you're looking for is just the pointer to the currently selected navigation controller. You get that if you query [tabbarcontroller selectedViewController], which returns the visible navigation controller. Then simply push the newly created controller on top of the stack of that navigation controller.