How to add UINavigationBar in an UIViewController? - iphone

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];

Related

Set UINavigationController's UITabBarItem inside a UITabBarController

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.

Remove modalViewController from view?

I have a view which pulls in another view using this code:
secondView = [[SecondView alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondView];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
The cancel button on this 2nd view has this:
[self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
But it doesn't appear to do anything. The view is still there and I can still interact with it. How do i remove it?
try:
[self.navigationController dismissModalViewControllerAnimated:YES]

loading nav controller from tabbarview controller on iphone

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.

Setting a navigationBar title

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.

Navigation Items in UITableViewController are not appearing?

I am displaying a UITableViewController inside of a UITabBarController that is being presented modally:
-(IBAction)arButtonClicked:(id)sender{
//this is a uitableviewcontroller
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
arTabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arViewController, lbViewController, nil];
arTabBarController.selectedViewController = arViewController;
[self presentModalViewController:arTabBarController animated:YES];
}
In my viewDidLoad for arViewController method I am setting the navigation items:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;
self.title = #"AR";
leaderBoardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:#selector(leaderBoardButtonClicked:)];
self.navigationItem.rightBarButtonItem = leaderBoardButton;
}
My navigation bar doesn't appear when it is inside of the UITabBarController, but when I push the view itself I am able to see it.
What am I missing?
Heh, I've been stumped by this too. What you need to do is send the rootViewController.
I've never used a tabBar for anything except on the main screen but ur code will probably look like this:
after arTabBarController.selectedViewController = arViewController;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController: arTabBarController] autorelease];
[self presentModalViewController: navController animated:YES];
Like I said I haven't done it with a tabBar but I'm pretty sure it will be something along these lines
I needed to add a UINavigationBar:
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];
arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
arTabBarController.selectedViewController = arNavController;
[self presentModalViewController:arTabBarController animated:YES];
There is a simple solution, put setting in view will appear
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
hope it help some newbies;