I am trying to add UITabBarItems to a UITabBar not to a tabbar controller. Here is what I tried to do. It is always crashing when I am calling setItems. Can any please point out whats wrong.
My_Accounts *my_AccountsVC = [[My_Accounts alloc] init];
Payments *paymentsVC = [[Payments alloc] init];
Transfer *transferVC = [[Transfer alloc] init];
NSArray *VCArray = [[NSArray alloc] initWithObjects:my_AccountsVC,paymentsVC,transferVC, nil];
[self.tabbar setItems:VCArray];
Thanks
If you look at items, it takes an array of UITabBarItems and not UIViewController subclasses which you seem to be passing.
You will have to keep track of the view controllers elsewhere and pass an array of UITabBarItems and handle the view controllers in the UITabBar's delegate.
Or much better, use UITabBarController.
I believe you are misunderstanding how a UITabBarController works (documentation link). You must add the UIViewControllers to the UITabBarController using the viewControllers property.
The last line you have should read:
[tabBarController setViewControllers:VCArray];
The tabBar property of the UITabBarController is read-only. You cannot set that.
If you have a UITabBar (documentation link) without a UITabBarViewController, then you will need to use the method:
- (void)setItems:(NSArray *)items animated:(BOOL)animated
However, these items are not UIViewControllers! They are instances of UITabBarItem (documentation link). You may set these all at once by putting them into an array, or you can set them per view controller. There are several system items you may use (More, Favorites, etc) or you may use – initWithTitle:image:tag: to create a custom item.
Code seems wrong. I guess
[self.tabbar setItems:VCArray];
Above line should have parameter of Array of UITabBarItems. You passed items of UIViewController I guess. You should Create UITabbarItems and pass array of that in setItems method.
You should do something like below:
UITabBarItem *tabOne = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
UITabBarItem *tabTwo = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
NSArray *arrTabbarItems = [NSArray arrayWithObjects:tabOne,tabTwo, nil];
[tabbar setItems:arrTabbarItems];
I am not sure what it will do as I am always using UITabBarController. Hope this help.
Related
I'm having a really strange issue. I've written an app with five tabs in a UITabBar. When I set the TabBarController's viewControllers property, I set it with five UINavigationControllers, so that each tab will have a UINavigationController within it.
Four of the tabs have it working perfectly. The navigation bar is there when I launch and switch to that tab. However, one of the tabs does not contain the UINavigationBar as I expected it to, and I can't understand why, because I initialized it exactly the same way I initialized all the others.
Here's some sample code from the AppDelegate.m file of initializing the individual view controllers:
SpotFilterViewController *spotList = [[SpotFilterViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:spotList];
[tabs addObject:navigationController];
[navigationController release];
[spotList release];
MySpotViewController *mySpot = [[MySpotViewController alloc] initWithSpot:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:mySpot];
[tabs addObject:mySpot];
[navigationController release];
[mySpot release];
Note: navigationController was declared above.
Anyone else run into this problem before? Or anyone have any idea why this might be happening? Any help is much appreciated. Thanks!
The problem is that you are doing this:
[tabs addObject:mySpot];
instead of this:
[tabs addObject:navigationController];
I wanna have a SettingViewMain with a TabBar, which can Flip between SettingView1 and SettingView2.
I tried this simple work since 3 hours and try nearly all tutorials I found, but I don't get it to work.
When I try to add a TabBar programmatically, I can flip between this 2 views, but in this views itself the TabBar is not shown, don't know why. When I add a TabBarController, this is not shown at all.
So, simlpy: How do I add a TabBar on a MasterView (not a AppDelegate-Window or something like this) and get the TabBar to switch between View1 and View2?
You can instantiate a UITabBarController using its alloc and init methods.
Instantiate both other ViewControllers and add them to an array.
After doing so, add it's view to your 'MasterView'.
Code:
UITabBarController *tab = [[UITabBarController alloc] init];
UIViewController *controller1 = [[UIViewController alloc] init];
UIViewController *controller2 = [[UIViewController alloc] init];
NSArray *controllers = [[NSArray alloc] initWithObjects:controller1, controller2, nil];
[tab setViewControllers:controllers];
[[self view] addSubview:[tab view]];
Or something closely compared to this.
Good luck!
Bryan
I have been working on a UINavigationController based application, using a UITableView for the rootview and Core Data for the data source.
Unfortunately I didn't plan ahead very well and now would like to implement tab bar navigation to the app on top of what I already have.
Can anyone recommend a simple way to do this? Or am I better off starting again with the TabBar based template and try to plugin my existing code?
Thanks guys!
Adam
Rather than start over, you may just want to create a tab-bar-based app and look at the code it produces, then try to do the same thing in your app.
You probably just need to change your application delegate's application:didFinishLaunchingWithOptions: method to look something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray *viewControllers = [[[NSMutableArray alloc] init] autorelease];
// First tab
MyController *myController = [[[MyController alloc] init] autorelease];
mapViewController.navigationItem.title = #"First";
UINavigationController *myRootController = [[[UINavigationController alloc]
initWithRootViewController:myController]
autorelease];
myRootController.tabBarItem.title = #"First";
myRootController.tabBarItem.image = [UIImage imageNamed:#"MyControllerTab"];
[viewControllers addObject:myRootController];
// Second tab
MyOtherController *myOtherController = [[[MyOtherController alloc] init] autorelease];
myOtherController.navigationItem.title = #"Second";
UINavigationController *otherRootController = [[[UINavigationController alloc] initWithRootViewController:myOtherController] autorelease];
otherRootController.tabBarItem.title = #"Second";
otherRootController.tabBarItem.image = [UIImage imageNamed:#"OtherControllerTab"];
[viewControllers addObject:otherRootController];
// Create other tabs
// ...
// Tab bar
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
[tabBarController setViewControllers:viewControllers];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
If you want to plug in your existing navigation controller to one of the tabs on a tab bar, it's pretty simple - just define a new UITabBarController to serve as the root controller and have it load your navigation controller into a single tab. Then change your app delegate to load the tab bar controller instead of your navigation controller.
If, however, you want to pull out multiple controllers from your existing navigation stack, you might have to do a bit more work - find places where you call pushViewController:animated: to modify your navigation stack, and instead plug those view controller instances into tabs on your tab bar. Here's where it might be worthwhile to start from a fresh tab bar template and copy over existing controllers, depending on the complexity of your code.
Adam,
Not sure how much coding you've done already, but if you're not that familiar with connecting things in IB and setting delegates you might be better off starting over with a TabBarController template.
Here's what you do:
Define new IBOutlet for UITabBarController in AppDelegate and Add TabBarController to IB
Connect UITabBarController to IBOutlet in AppDelegate in IB
Set Delegate for UITabBarController in IB to AppDelegate
Change rootViewController to tabBarController in AppDelegate
Drag All ViewControllers into Tab Bar Controller in IB
Setup other Tabs using NavigationControllers or UIViewControllers depending on your design
Run, Fix, Repeat
In my iphone app, i have a navigation Controller and a tabBar Controller.
The TabBarController has three tabs. In the second and third Tab the NavigationController are added to the viewControllers.
Problem :
In third tab viewController shows the NavigationBar but the in second tab viewController doesnot display navigationBar.
Things I have tried and checked:
1) I checked that all the connections in IB are done properly
2) I checked the size of frame for the view. It doesnot overlap the navigationBar.
3) I also tried using self.navigationController.navigationBar.hidden = NO;
But still it does not show the navigationBar in the second tab.
What should I do?
Please Suggest
Please Help
Thanks!!
We can't do much without looking at your code.
Assuming your TabBarController is properly connected in Interface Builder, you'll need something similar to this:
UIViewController *firstView = [[UIViewController alloc] init];
UIViewController *secondView = [[UIViewController alloc] init];
UIViewController *thirdView = [[UIViewController alloc] init];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:secondView];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:thirdView];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, firstNav, secondNav, nil];
Of course, have every view released afterwards.
Hmmm thats a problem i faced too.
If you look at your IB file ,you'll see that you can do a bit of tweaking and acheive it.
i'll post details as soon as i get time.
Oops!!! a big mistake on my part.I did not check the checkbox for "show navigation bar" in inspector for NavigationController. Hope this helps someone.
i have coded in applicationdidfinishing method in appdelegate.m file.RtbfViewController,infoViewController are UItableviewcontrolleras.but when i click HOME tab,it does not show table.what i have to do? anyone can help?
tabBarController = [[UITabBarController alloc] init];
RtbfViewController *rtbfViewController = [[RtbfViewController alloc]
initWithStyle:UITableViewStyleGrouped];
rtbfViewController.tabBarItem.title = #"HOME";
InfoViewController *infoViewController = [[InfoViewController alloc]
initWithStyle:UITableViewStyleGrouped];
infoViewController.tabBarItem.title = #"INFO";
tabBarController.viewControllers = [NSArray arrayWithObjects:
rtbfViewController,infoViewController,nil];
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
If you haven't put any data in the tables yet, you won't get much indication of the tables being present. It's hard to tell if this is your problem or not without seeing the code for your subclasses RtbfViewController and InfoViewController.
Just in case this is your problem: You can add data to your tables by implementing the UITableViewDataSource protocol, and from there implementing at least the two required methods:
– tableView:cellForRowAtIndexPath:
– tableView:numberOfRowsInSection:
and this methods is used for grouped tables:
– numberOfSectionsInTableView:
Also, don't forget to set the dataSource instance variable of your UITableView if needed.
In general, the TableViewSuite is a useful code example from Apple for learning about the UITableView related classes.