I'm developing a iphone app that has a main viewcontroller(UITabBarController) with UINavigationControllers in each tab. The problem is that I can't change the TabBarItem's data(title and image).
This is how I load the viewcontrollers:
AroundViewController *aroundViewController = [[AroundViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aroundViewController];
[[navController navigationBar] setTintColor:[UIColor grayColor]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];
[self presentViewController:tabBarController animated:YES completion:nil];
I'm trying set the tabbaritem in the aroundviewcontroller's init, but I don't succeed.
I was reading and one solution was, in the init,
[self setTitle:#"whatever"];
This worked but I couldn't change the image.
Thanks in advance.
Use the tabBarItem instance of your view controller to access the tab bar item directly!
[self.tabBarItem ....]
[self.tabBarItem setImage: ...]
[self.tabBarItem setTitle: ...]
EDIT:
hm, try the following then in your init or viewWillAppear method:
UITabBarItem* tabBarItem = [[UITabBarItem alloc] initWithTitle:#"YourTitle" image:[UIImage imageNamed:#"YourImage.png"] tag:9];
self.tabBarItem = tabBarItem;
[tabBarItem release];
Before setting the UIViewControllers for the UITabBarController you can do this:
[navController.tabBarItem setImage:[UIImage imageNamed:#"MY-IMAGE"]];
[navController setTitle:#"MY-TITLE"];
NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];
I found a solution.
In the viewWillAppear I put:
[[[self navigationController] tabBarItem] setTitle:#"HELLO"];
That works.
Related
I have made a UIScrollView inside a UIViewController .and now I want to add a UITabBarController to it .But when I do it ,I couldnot see the TabBarController added to it..
I have written this code
[testscroll setScrollEnabled:YES];
[testscroll setContentSize:CGSizeMake(320,800)];
FirstViewController *first = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
first.title=#"Search";
UserProfile *second=[[UserProfile alloc]initWithNibName:#"UserProfile" bundle:nil];
second.title=#"My Profile";
UserActivities *third=[[UserActivities alloc]initWithNibName:#"UserActivities" bundle:nil];
third.title=#"My Activities";
LogOut *logout=[[LogOut alloc]initWithNibName:#"LogOut" bundle:nil];
logout.title=#"Sign Out";
NSArray *viewArray= [NSArray arrayWithObjects:first,second,third,logout, nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
[self presentModalViewController:tabBarController animated:NO];
I have added this in a fifthViewController.I can see the UIScrollView and some Labels and textFields added but not the TabBarController added.Wher i m going wrong ..?
As far as I can see, you are creating a tab bar controller and then presenting it modally:
NSArray *viewArray= [NSArray arrayWithObjects:first,second,third,logout, nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
[self presentModalViewController:tabBarController animated:NO];
It does not appear in scrollView because you are not "connecting" the two in any ways.
If you want your tab bar controller to appear inside of the scroll view, add it as a subview to the latter:
[testscroll setScrollEnabled:YES];
[testscroll setContentSize:CGSizeMake(320,800)];
...
[testscroll addSubview:tabBarController.view];
(I assume that tabBarController is a property and that the scroll view is displayed in some way)
in my app from a login screen i am navigating to a class say classA , like this
classA *objUserHome = [[classA alloc]init];
[self presentModalViewController:objUserHome animated:YES];
[objUserHome release];
and ClassA is having a navigating bar and a tabbar(5 tabs in it), i have created my tab bar programmatically like this
- (void)viewDidLoad
{
[super viewDidLoad];
//Create tab bar controller and navigation bar controller
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *arrControllers = [[NSMutableArray alloc] initWithCapacity:5];
//Add PunchClock to tab View Controller
PunchClock* objPunchClock = [[PunchClock alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objPunchClock];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objPunchClock release];
//Add Time_Sheet to tab View Controller
Time_Sheet* objTime_Sheet = [[Time_Sheet alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objTime_Sheet];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objTime_Sheet release];
//Add PTO to tab View Controller
PTO* objPTO = [[PTO alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objPTO];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objPTO release];
//Add PunchClock to tab View Controller
CrewPunch* objCrewPunch = [[CrewPunch alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objCrewPunch];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objCrewPunch release];
//Add PunchClock to tab View Controller
Reports* objReports = [[Reports alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objReports];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objReports release];
// Add this view controller array into the tab bar
//self .viewControllers = arrControllers;
tabBarController .viewControllers = arrControllers;
[arrControllers release];
[self.view addSubview:[tabBarController view]];
}
And ClassA is inherited from UIViewController
now the problem is, after navigating to classA , view of classA is shifted some 4mm downwards why its so?? how can i fix this,,pls help me out ,, thanx in advance
When using storyboards and Modal Transition between 2 or more views you can encounter an error similar to the above.
If you use Modal Transition from ViewControllerA to ViewControllerZ and then attempt to Modal Transition from ViewControllerZ back to ViewControllerA sometimes the view of ViewControllerA gets pushed down the Window slightly.
This can be prevented using:
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
On ViewControllerZ to go back to ViewControllerA from an event on ViewControllerZ
You might have chosen some top bar in your Interface Builder or XIB file and additionally set the navigation bar. Dont choose any top bar in the XIB file.
Try as below
[self.navigationController.view addSubview:[tabBarController view]];
After a long research i finally fixed this issue just by inheriting the class from UINavigationController instead of UIViewControler
I have an UIViewController class(Say it is XXX). I present this view controller as modally by the code..
XXX *xxx = [ [XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];
I want to add a navigation bar on the top of the XXX view. So I used UINavigationBar object in XXX's loadView method.
UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navBar];
[navBar release];
But, It throws an error as "EXC_BAD_ACCESS". Any help...?
Thanks
OPTION-1:
Try adding navigation bar from the XIB of viewController called XXX.
OPTION-2:
Add a UINavigationController and present it modally.
Replace your code :
XXX *xxx = [[XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];
with this code:
XXX *xxx = [[XXX alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];
Hope this helps you.
Replace your code with:
XXX *xxx = [[ [XXX alloc] init]autorelease];
[self presentModalViewController:xxx animated:YES];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];
I think it will solve your "EXC_BAD_ACCESS" problem.
you can try this by adding toolbar at the top of the view. In many cases i have seen for poping MODAL controller this is nice solution. but if you want to navigate more controllers from MODAL controller then you should use UINavigationController.
you do it like this:
XXX *xxx = [[XXX alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigationController animated:YES];
[xxx release];
[navigationController release];
my app is based on tabbar controller
now in my default view i am showing a viewController and lets say it has Button A, when user press A it should load a my tableviewController but nothing is happening??
-(IBAction)promo:(id)sender
{
aRoot= [[tableViewController alloc] initWithNibName:#"tableViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:aRoot animated:YES];
}
but its not loading anything no error even???
/////////// UPDATE
i did this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Promo *aPromo = [[Promo alloc] initWithNibName:nil bundle:nil];//button A is deifned on this VC
// then...
aNav = [[UINavigationController alloc] initWithRootViewController:aPromo];
// [pageOne release];
and in promoviewController
-(IBAction)promo:(id)sender
{atab= [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
//TableViewController *atab1 = [[TableViewController alloc]initWithNibName:#"TableViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:atab animated:YES];
}
You need a navigationController to push a viewController.
you can't add a viewcontroller to a uitabbar if you want to use the navigation controller.
Where you make your tab bar controller you have to do this:
MyFirstTabViewController *pageOne = [[MyFirstTabeViewController alloc] initWithNibName:nil bundle:nil];
// then...
UINavigationController *ncOne = [[UINavigationController alloc] initWithRootViewController:pageOne];
[pageOne release];
then add ncOne to the tab bar instead of the view controller. :) Then your code in the question should work (given that you're properly declaring aRoot in the header).
EDIT
Start again... choose a view based application call it TabBarTest.
Right click on classes and add three new classes. They need to be subclasses of UIViewController or UITableViewController. Lets say they're called RootViewOne RootViewTwo and SecondaryViewController.
Then open TabBarTestViewController.m
Uncomment the viewDidLoad method.
You need to now put this code in that method:
UITabBarController *tbc = [[UITabBarController alloc] init];
NSMutableArray *viewControllers = [NSMutableArray array];
RootViewOne *vc1 = [[RootViewOne alloc] initWithNibName:nil bundle:nil];
UINavigationController *nc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
nc1.view.backgroundColor = [UIColor redColor];
[viewControllers addObject:nc1];
[vc1 release];
RootViewTwo *vc2 = [[RootViewTwo alloc] initWithNibName:nil bundle:nil];
UINavigationController *nc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
nc2.view.backgroundColor = [UIColor blueColor];
[viewControllers addObject:nc2];
[vc2 release];
[tbc setViewControllers:viewControllers animated:YES];
[self presentModalViewController:tbc animated:YES];
Now open RootViewOne.m and in viewDidLoad put this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Click to move through stack" forState:UIControlStateNormal];
[button addTarget:self action:#selector(moveToNextView:) forEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Now you're going to need a custom method:
-(void)moveToNextView:(id)selector {
SecondaryViewController *page = [[SecondaryViewController alloc] initWithNibName:nil bundle:nil];
page.title = #"Next Page";
page.view.backgroundColor = [UIColor greenColor];
[self.navigationController pushViewController:page animated:YES];
[page release];
}
This is only basic, but you should get an understanding of the kinad process you need to go through. I typed this straight into the browser so there may be spelling mistakes... watch out if you get any errors or warnings. Hopefully this can help you with your project.
Finally i solved this , i changed that VC to Navigation view Controller and then i can push the new view based on button tap,, thanks to thomas also who helped me a lot but i couldn't figure it out.
I currently have my navigation controllers defined in my appDelegate as followed (code summarized):
- (void) applicationDidFinishLaunching {
tabBarController = [[UITabBarController alloc] init];
FlagList *flagList = [[FlagList alloc] initWithApiCall:API_PUBLICTIMELINE andTitle:#"Home"];
UITabBarItem *homeTab = [[UITabBarItem alloc] initWithTitle:#"Home"
image:[UIImage imageNamed:#"home.png"]
tag:0];
flagList.tabBarItem=homeTab;
[homeTab release];
tabBarController.viewControllers=[NSArray arrayWithObjects:flagList,nil];
[flagList release];
[rootViewController release];
rootViewController = [[UINavigationController alloc] initWithRootViewController:[tabBarController autorelease]];
rootViewController.navigationBar.barStyle=UIBarStyleDefault;
}
I want to set a title in the navigationBar of my FlagListView. HOWEVER, I want to be able to do this in the -viewDidLoad method of my FlagList UITableViewController class. How can I access this property?
I tried:
[[self navigationItem] setTitle:#"Home"];
..but it doesn't seem to work. Can someone please tell me what I'm doing wrong?
Assuming FlagList is a descendant if ViewController use [self setTitle:#"Home"] instead of [[self navigationItem] setTitle:#"Home"];
[self setTitle:#"Home"];
This should work.