How to get UIBarButtonItem to open a new view controller? - iphone

I want to add method that opens up the new view controller when this info button is pressed.
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(action)];

-(void)action {
MyViewController *myView = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[myView presentModalViewController:YES]; // present view modally
[self.navigationController pushViewController:myView animated:YES]; // add to navigation stack
[myView release];
}

Related

'Done' button not visible in the Modal view

I am trying to present a modal view from one of my view. The presenter view is already shown as a modal from a custom view. My problem is that I am not able to see the 'Done' button on the new Modal view presented. Below is my code. Am I missing something?
UIViewController *aViewController = [[UIViewController alloc] init];
UINavigationController *aNavigationController = [[[UINavigationController alloc] initWithRootViewController:aViewController] autorelease];
[aNavigationController.navigationBar setBarStyle:UIBarStyleBlack];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissMe)];
[aNavigationController.navigationItem setLeftBarButtonItem:aBarButtonItem];
MyView *aView = [[MyView alloc] initWithFrame:self.view.frame];
[aViewController.view addSubview:aView];
[self presentModalViewController:aNavigationController animated:YES];
[aViewController release];
- (void)dismissMe {
[self dismissModalViewControllerAnimated:YES];
}
If I understand weel the question, you can try a solution like this:
Write this in the viewDidLoad or init method of the modal view controller you want to show from the actual view:
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self selector:#selector(dismissMe)];
self.navigationController.leftBarButtonItem = done;
and implement you dismissMe method.
Instead, in the presenter controller write this where you want to present the new modal controller:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controllerYouWantToShow];
[self presentModalViewController:navController animated:YES];
Obviusly, controllerYouWantToShow is a pointer/variable pointing your view controller you want to show... I usually do this to solve a problem like yours... However, check the code because I havent't tested it :)
Hope it helps!

navigation bar is missing

Navigation bar is missing while pushing kalviewcontroller on button click.How to add navigation bar? so that it can be pop to view controller where its being pushed on button click.
kal = [[[KalViewController alloc] init]autorelease];
kal.title = #"Calendar of game";
kal.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:#selector(addEvent)] autorelease];
kal.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered target:self action:#selector(backToPrevious)]
autorelease];
kal.delegate = self;
dataSource = [self init];
kal.dataSource = dataSource;
[self.navigationController pushViewController:kal animated:YES];
Put [self.navigationController setNavigationBarHidden:FALSE] in you viewDidLoad(assuming you have created navigation controller).

Right button not appearing on modal view controller

I'm trying to add a button to a modal view controller so it can be dismised but the button is not displaying. Any help would be appreciated.
Here is the code:
UIViewController* webView = [[UIViewController alloc] init];
UINavigationController* modalViewController = [[UINavigationController alloc] initWithRootViewController:webView];
[webView release];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hideModalView)];
modalViewController.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
[globalNavController presentModalViewController:modalViewController animated:YES];
You should do it like this.
webView.navigationItem.rightBarButtonItem = doneButton;
Try to add the doneButton to your UIViewController, then you should be fine.
Maybe you should change webView to webViewController, a better name.

Putting a back button inside of a Modal view pushed by another modal view

Check this, im pushing a modal view inside of another modal view. But, im trying to put a button inside of this modal view, but without luck.
What im doing wrong?
Thanks!
CadastroViewController *addController = [[CadastroViewController alloc] initWithNibName:#"CadastroViewController" bundle:nil];
// This is where you wrap the view up nicely in a navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
// You can even set the style of stuff before you show it
navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
navigationController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
// And now you want to present the view in a modal fashion all nice and animated
[self presentModalViewController:navigationController animated:YES];
// make sure you release your stuff
[navigationController release];
[addController release];
You'll have to add a new UINavigationItem to the navigationbar of the actual viewcontroller - NOT the navigation controller.
addController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
You should add your button in a
-(void) viewDidLoad of your CadastroViewController controller class
This will look like this:
- (void) viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
self.navigationController. leftBarButtonItem = button;
[button release];
}
[self presentModalViewController: navigationController animated:YES]; is ok in your example, just all other initializations you should do in viewDidLoad
It seems to me, that the problem is here:
[self presentModalViewController: navigationController animated:YES];
Instead try do this:
[self presentModalViewController: addController animated:YES];

How to add a NavigationController to a flipside view?

I have a flipside view showing the settings in my app. I'm trying to have a few levels of tableViews, but how do I implement a navigation controller to allow this? Usually for a normal root view, the navigation controller is by default in the App Delegate. However how do I do it if the root is the flipside?
Thanks.
You can just create a UINavigationController in the method which displays the flipside. Like so:
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:controller action:#selector(done:)];
controller.navigationItem.leftBarButtonItem = doneButton;
[self presentModalViewController:navController animated:YES];
[doneButton release];
[controller release];
[navController release];
}