overriding default behaviour of backBarButtonItem - iphone

I am facing an issues with navigating between the views, Basically I have 3 views:
RootView
view1
View2
What I want to do is when I click on back button of View2 I want RootView to be loaded instead of loading view1 (which is default).
.. I have written the below code in My view1.m before pushing the view2ViewController, code:
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back" target:self action:#selector(someMethod:)];
self.navigationItem.backBarButtonItem = back;
-(void)someMethod:(id)sender{ [self.navigationViewCOntroller popToRootViewControllerAnimated:YES] }
The above code is not working

Try this:
To go to a View:
Aview *aview =[[Aview alloc] initWithNibName:nil bundle:nil];
settings.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:settings animated:YES];
To go back:
[self dismissModalViewControllerAnimated:YES];
So in ur case its:
-(void)someMethod:(id)sender{
Rootview *rootview =[[Rootview alloc] initWithNibName:nil bundle:nil];
settings.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:settings animated:YES];
}

The second half of your question is not clear.
Try this
[self.navigationController popToRootViewControllerAnimated:YES];
Hope this will work for you. By this you can navigate from View2 to your root view.

Related

Nav Bar button loading random view before loading appropriate view

I have a back button on my answer view controller (a view controller that displays answers) if the user hits the back button I created it switches to a view that has the title of "Back" and just an empty tableview, before switching back to my main view of where all the questions to be answered are displayed. Why is this happening? Its a very brief thing, but definitely noticeable!
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 48)];
navBar.delegate = self;
UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:#"Back"];
[navBar pushNavigationItem:backItem animated:NO];
UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:#"Question"];
[navBar pushNavigationItem:topItem animated:NO];
topItem.leftBarButtonItem = nil;
[self.view addSubview:navBar];
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
ViewController *controller = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
return true;
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
ViewController *controller = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
You're creating your own UINavigationBar and UINavigationItem instances and you probably shouldn't be. The situation you describe is exactly what a UINavigationController is for. When using a UINavigationController it creates the UINavigationBar and each UIViewController that you show on screen (push into the navigation controller) has its own UINavigationItem (with the title taken from the title of the view controller).
The reason you get an empty 'view' titled "Back" is that you're creating it:
UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:#"Back"];
[navBar pushNavigationItem:backItem animated:NO];
Dispense with all of this, create a UINavigationController and make your question view controller it's root view controller, then add the navigation controller to the screen. Then when a question is answered, push the answer view controller:
[self.navigationController pushViewController:answerViewController animated:YES];

'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!

Adding UINavigationController to View Other Than Main View

I'd like to add a UINavigationController to my app info view (NOT to my main view). I've watched/read a number of tutorials showing how to add it to the main window through the AppDelegate using IB. In my case, I only want it to appear when a user presses the info button and is brought to the infoView. Here is how I switch to the infoView within my MainViewController:
- (IBAction)infoButtonPress:(id)sender
{
// Create pointer to instance of InfoViewController
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
// Add view switching animation
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// Change view using animation
[self presentModalViewController:infoView animated:YES];
}
At this point the infoView is displayed and I would like THIS to be the RootView of the UINavigationController. I have tried adding the line:
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
after creating an instance of InfoViewController, but the app crashes. Is it possible to add UINavigationController to views other than the main view?
Thanks.
You are very close with your implementation. Try it in this order.
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
[infoView release]; // skip this if using ARC
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:infoNavController animated:YES];
UPDATE
To kill this modal, you will have to add your buttons to the main modal view.
InfoViewController.m
-(void)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *cancelButton =
[[UIBarButtonItem alloc] initWithTitle: #"Cancel"
style: UIBarButtonItemStylePlain
target: self
action: #selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}

iPhone - Navigation bar Back button item is not responding

I have a fullscreen modalView called like this:
PreferencesController *nextWindow = [[[PreferencesController alloc] initWithNibName:#"Preferences" bundle:nil] autorelease];
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:nextWindow] autorelease];
[self presentModalViewController:navController animated:YES];
Then from this modalView I push another view :
MyController *nextWindow = [[[MyController alloc] initWithNibName:#"tmp" bundle:nil] autorelease];
[self.navigationController pushViewController:nextWindow animated:YES];
In this new controller, I have this viewDidLoad :
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Borders";
self.navigationController.navigationBarHidden = NO;
}
The leftBarButtonItem is not active, I mean touching it does not highlight it nor does it go back to the previous view.
My views are displayed fullScreen, with [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; called at the application initialisation.
The navigationBar frame is 0,0,320,44.
The navigationBar superview frame is 0,0,320,480.
The viewController view frame is 0,0,320,436.
I've tried to call in viewDidLoad self.navigationController.navigationBar.userInteractionEnabled = YES; and self.navigationItem.leftBarButtonItem.enabled = YES; without effect.
What happens?
EDIT :
My self.navigationController.navigationItem.backBarButtonItem is NIL.
self.navigationController.navigationItem is not NIL
Whenever this sort of unresponsiveness happens to me, it is always because of framing issues. i.e. the superview of the NavigationController is smaller than the NavigationController's view. I know you say that everything is set to full screen, but I would verify that everything is actually full screen by turning "clipsSubviews" on for each view in the hierarchy.
I just had this issue, I'm not sure why this works, but instead of doing:
UIBarButtonItem *backButton =
[[[UIBarButtonItem alloc] initWithTitle:#"Back"
style: UIBarButtonItemStyleBordered
target:nil
action:nil] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
I replaced the second line with
self.navigationController.navigationItem.leftBarButtonItem = backButton;
That works for me.
I found the solution.
The problem was that the first view was called from the overlay, and not from the picker.
Keeping a reference to the Picker into the overlay, and calling the view from it solves the problem:
From the overlay:
[self.picker presentModalViewController:navController animated:YES];
works
instead of:
[self presentModalViewController:navController animated:YES];

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