I want to create uitabbarviewcontroller programmatically for my application.
In that uitabbarcontroller, i want to add 2 buttons namely "Show Log", "My Contacts".
when tapping on that "ShowLog" button , i want to push new view and the same for "My Contacts" button also
I dont know how to do that
Please help me below is the code i have tried
myTabBarController = [[UITabBarController alloc] init];
myTabBarController.view.frame = CGRectMake(0, 0, 320, 460);
MyCallLogViewController *testVC = [[MyCallLogViewController alloc] init];
TemplateViewController *otherVC = [[TemplateViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:testVC, otherVC, nil];
myTabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[self.view addSubview:myTabBarController.view];
Related
I am developing one application which includes view controller because my first two pages content only view not tab-bar.after that i have created run time tab-bar controller using this code
UIViewController *viewcontroller1 = [[viewcontroller1 alloc] initWithNibName:#"viewcontroller1" bundle:nil];
viewcontroller1.title = #"sometext";
viewcontroller1.tabBarItem.image = [UIImage imageNamed:#"someimage.png"];
UIViewController *viewcontroller2 = [[viewcontroller2 alloc] initWithNibName:#"viewcontroller2" bundle:nil];
viewcontroller2.title = #"sometext";
viewcontroller2.tabBarItem.image = [UIImage imageNamed:#"someimage.png"];
tbc = [[UITabBarController alloc] initWithNibName:#"viewcontroller1" bundle:nil];
tbc.viewControllers = [NSArray arrayWithObjects: viewcontroller1,viewcontroller2, nil];
tbc.selectedViewController = viewcontroller1;
//// NSLog(#"Selected index = %d of %d", tbc.selectedIndex, [tbc.viewControllers count]);
[self presentModalViewController:tbc animated:NO];
it is working properly but in my second view i want tab-bar controller and navigation-controller both.
so in viewcontroller2 i have implemented code like that it's giving me navigation controller but it's hiding tab-bar controller
- (void)viewDidLoad
{
nvc = [[UINavigationController alloc] initWithRootViewController:[[viewcontroller2 alloc] initWithNibName:#"viewcontroller2" bundle:nil]];
[self presentModalViewController:nvc animated:NO];
[nvc release];
[super viewDidLoad];
}
so please help me what to i do so i can get both tab-bar controller & navigation-controller in this viewcontroller2.??
please guide me.
You can create new class for tabbar and in UI Design time you can add navigation controller as tab and set view in navigation controller. Whenever you want add tabbar controller in that page create object of tabbarcontroller class object and add it on view.
i searched before post my question, but i find nothing useful.
before: i'm a c# developer and i'm doing my first iphone app, don't kill me please!
in my app i have a tabbar controller on the bottom (4 buttons)
each button, is a navigation controller type, its show a navigation bar on the top and in the middle i have a viewcontroller (normally after i load an uitableview)
the mad person that asked me to do this app wants that in the top of the TabBar to show always an image (for advertisement, fixed as 320x50).
so the app should look in this way
< NavigationBar >
ViewController
< UIImage >
< TabBar >
but now is
< NavigationBar >
ViewController
< TabBar >
at first take tabbarview controller then make view controller then set the UIimage then set the navigation bar for the uiview controller
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
dashBoardView = [[DashboardViewController alloc] initWithNibName:#"DashboardViewController" bundle:nil];
dashBoardView.title = #"dashBoardView";
UINavigationController *mydashboarController = [[[UINavigationController alloc] initWithRootViewController:dashBoardView] autorelease];
mydashboarController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:mydashboarController];
[dashBoardView release];
ordersView = [[OrdersViewController alloc] initWithNibName:#"OrdersViewController" bundle:nil];
ordersView.title = #"ordersView";
UINavigationController *myorderController = [[[UINavigationController alloc] initWithRootViewController:ordersView] autorelease];
myorderController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderController];
[ordersView release];
orderList = [[OrderListViewController alloc] initWithNibName:#"OrderListViewController" bundle:nil];
orderList.title = #"orderList";
UINavigationController *myorderListController = [[[UINavigationController alloc] initWithRootViewController:orderList] autorelease];
myorderListController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderListController];
[orderList release];
[self.tabBarController setViewControllers:listOfViewControllers animated:YES];
i think it will help you some way...
In iOS, the TabBar property in the TabBarController is read only. How can I associate a custom item with a particular view controller? How do I access the UITabBarItems inside the tabBar?
Like this
CustomView *custom = [[CustomView alloc] init];
UITabBarItem *customTab = [[UITabBarItem alloc] initWithTitle:#"Custom" image:[UIImage imageNamed:#"custom.png"] tag:0];
SecondView *second = [[SecondView alloc] init];
UITabBarItem *secondTab = [[UITabBarItem alloc] initWithTitle:#"Next" image:[UIImage imageNamed:#"next.png"] tag:1];
NSArray *views = [NSArray arrayWithObjects:custom,second,nil];
[tabBarController setViewControllers:views];
//how do I set the individual TabBarItems (customTab,secondTab) to be associated
//with the views in question? tabBarController.tabBar is read only
Inside each view controller, you can set a tabBarItem property. If the view controller is owned by a UITabBarViewController the associated item on the tab bar will be updated accordingly.
Something like this
-(void)viewDidLoad {
[super viewDidLoad];
UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag];
[self setTabBarItem:tbi]
[tbi release];
}
You are not restricted to perform this operation in the viewDidLoad method, obviously.
A view is presented modally:
[self presentModalViewController:modalNavController animated:YES];
This view uses a UITabBarController with 4 elements. One of these elements, "Info" has a button that's only visible if its available. If the button is clicked, it needs to push to another view controller, but I'd also like to maintain the tab bar from it's parent view. I haven't been able to figure out how to do this with or without keeping the tab bar. Ive tried pushing and presentingModally in all the places that I could image. How should this be done properly?
Creating tab bar:
infoController.title = #"Info";
streetViewController.title = #"Street View";
reviewController.title = #"Reviews";
streetViewController.tabBarItem.image = [UIImage imageNamed:#"flag.png"];
infoController.tabBarItem.image = [UIImage imageNamed:#"openMarker.png"];
reviewController.tabBarItem.image = [UIImage imageNamed:#"reviews.png"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
UINavigationController *infoNC = [[[UINavigationController alloc] initWithRootViewController:infoController] autorelease];
infoNC.navigationBarHidden = YES;
[tabBarController setViewControllers:
[NSArray arrayWithObjects:infoNC, streetViewController, reviewController, nil]];
[self.view addSubview:tabBarController.view];
When you add the view controllers to the tab bar controller you need to do this:
MyCustomViewController *vc1 = [[MyCustomViewController alloc] initWithNibName:nil bundles:nil];
UINavigationController *nc1 = [[[UINavigationController alloc] initWithRootViewController:recipesRootView] autorelease];
[vc1 release];
then add nc1 instead of your custom view.
Then in MyCustomViewController to push another view controller do:
[self.navigationController pushViewController:(UIViewController *)page animated:YES];
That should work for you, and keep the tab bar controller.
I have four Tab bar items in a Tab bar which is being bottom of the view where i have the TableView. I am adding Tab bar and items programmatically (Refer below code) not through I.B.
Click on first three Tab bar items, will show the data in the same TableView itself. But clicking on last Tab bar items will push to another UIViewcontroller and show the data there. The problem here is, when i push to the viewController when clicking on last Tab bar item, main "Tab bar" is getting removed.
Tab bar code:
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 376, 320, 44)];
item1 = [[UITabBarItem alloc] initWithTitle:#"First Tab" image:[UIImage imageNamed:#"first.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"Second Tab" image:[UIImage imageNamed:#"second.png"] tag:1];
item3 = [[UITabBarItem alloc] initWithTitle:#"Third Tab" image:[UIImage imageNamed:#"third.png"] tag:2];
item4 = [[UITabBarItem alloc] initWithTitle:#"Fourth Tab" image:[UIImage imageNamed:#"fourth.png"] tag:3];
item5 = [[UITabBarItem alloc] initWithTitle:#"Fifth Tab" image:[UIImage imageNamed:#"fifth.png"] tag:4];
NSArray *items = [NSArray arrayWithObjects: item1,item2,item3,item4, item5, nil];
[tabBar setItems:items animated:NO];
[tabBar setSelectedItem:item1];
tabBar.delegate=self;
[self.view addSubview:tabBar];
Push controller code clicking from last Tab bar item:
myViewController = [ [MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
myViewController.hidesBottomBarWhenPushed=NO;
[[self navigationController] pushViewController:myViewController animated:NO];
I am not seeing bottom Tab bar when i push my current TableView to myViewController. I am seeing full screen view there. I want to see bottom Tab bar always when every tab item clicked.
What might be the problem here? Could someone who come across this issue, please share your suggestion to me?
Thank you.
You are using the TabBar itself (as view) as the main view initially.
Use UITabBarController like this:
//tabBarController is defined in the interface (the .h file)
tabBarController = [[UITabBarController alloc]init];
firstViewController = [[UIViewController alloc] init];
UITabBarItem *item1 = [[[UITabBarItem alloc]initWithTitle:#"First" image:nil tag:1] autorelease];
[firstViewController setTabBarItem:item2];
secondViewController = [[SecondViewController alloc]init];
UITabBarItem *item2 = [[[UITabBarItem alloc]initWithTitle:#"Sec" image:nil tag:1] autorelease];
[secondViewController setTabBarItem:item2];
//init the tab bar controller populated with two view controllers
[tabBarController setViewControllers:[NSArray arrayWithObjects:firstViewController,secondViewController,nil] animated:NO];
[window addSubview:tabBarController.view];