UIBarButtonItem segue to next viewcontroller - ios

I created a UIBarButtonItem programmatically and want it to go to the next ViewController when it is pushed.
This is how I create the custom UIBarButtonItem in the viewDidLoad:
UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
initWithTitle:#"Found it!"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(foundView)];
After that I created this method:
-(void)foundView:(id)sender {
UIViewController *foundVC = [self.storyboard instantiateViewControllerWithIdentifier:#"foundView"];
[self.navigationController pushViewController:foundVC animated:YES];
}
The ViewController where I want to go after the UIBarButtonItem is clicked has a Storyboard ID: "foundView".
What am I doing wrong here?

Change your code like this :
UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
initWithTitle:#"Found it!"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(foundView:)];
you forgot : after foundView in your selector so you don't call the method.

You can create a segue from ViewControllerA to ViewControllerB. Note, this segue connects two view controllers. Not a button to a view controller. Now, on the storyboard, give the segue a descriptive name. For example: segueFoundView.
Now, do this:
-(void)foundView:(id)sender {
[self performSegueWithIdentifier:#"segueFoundView" sender:self];
}
Edit: corrected spelling

Related

How to get the method of backBarButtonItem of navigationItem?

I have add the navigationItem.backBarButtonItem when turn to the new page like the following code , but I want to add a Timer for change some image before turn back to the first by backBarButtonItem.
UIViewController *ReconnectView = [[AITReconnectView alloc] initWithNibName:#"AITReconnectView" bundle:nil] ;
ReconnectView.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Back", nil) style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationController pushViewController:ReconnectView animated:YES];
For example : When I click the backBarButtonItem , it will run the Timer for 3 second. And then turn back to the first view.
I have search for some information , but it only overwrite the new method for backBarButtonItem.
How to add a Timer in method of backBarButtonItem but retain the original method of backBarButtonItem ?
Thanks in advance.
There are two ways:
Create your own back button (similar like native) and selector method and assign it to button:
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:#"BackToVcA"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
Use UIViewController life cycle method, viewWillDisappear or viewDidDisappear.
Hope this is what you're looking for.
If you want to delay the transition to the first view by 3 seconds,
Just add,
[NSThread sleepForTimeInterval:3.0]; to viewWillDisappearmethod of the second view.
If you want to execute method invocation in objective C, then
[self performSelector:#selector(methodName) withObject:self afterDelay:3.0];
should work fine. Share code for some more information.

My Custom UINavigationController won't show right button

I have a "LoginViewController" which presents a new Controller which is a subclass of UINavigationcontroller when clicking a button:
MPNavigationViewController *controller = [[MPNavigationViewController alloc] initWithRootViewController:[[MPQuestionFirstViewController alloc] init]];
[self presentViewController: controller animated:YES completion:nil];
"MPNavigationViewController" subclass UINavigationController and uses "REMenu" to have a sliding-from-top menu ("Link") and on viewDidLoad I try to add a right button to open it:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *toggleMenuButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(toggleMenu:)];
self.navigationItem.rightBarButtonItem = toggleMenuButton;
[self initMenu];
}
It doesn't show any button on the navigation bar. Why could it be?
If I try to add the button from one of the "viewControllers" that will handle sections on the menu. It shows the button, but it doesn't paint it at all.
Thanks.
You are using subclass of UINavigationcontroller which is not actually view controller.
There is only one solution, You need to create your custom button and add it to UINavigationbar as a subview..
Use this hope it will help.
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goToDoneButtonAction)];
self.navigationItem.rightBarButtonItem = doneButton;

Back button pressed, losing my custom UIToolBar when i pop the view controller off the stack

I hope everyone is having a great day so far.
I'm running into a problem with my app that I need some help figuring out. I have a view controller (lets call "A") with a custom toolbar which works nicely.
When I push a view controller (lets call it "locationsController") from view controller "A" ...
[self.navigationController pushViewController:self.locationsController animated:YES];
...and I press the back button on the locationsController which closes like this...
[self.navigationController popViewControllerAnimated:YES];
I lose my custom toolbar in view controller "A" when locationController is popped. How do I fix it so I can intercept and call my buildtoolbar method?
Also, the locationsController can be pushed/popped from one other view controller so I'll need to determine which view controller is popping locationsController and either fire the buildtoolbar method or do nothing since the other view controller doesn't have a toolbar.
view controller "A" is a XIB while locationsController is defined in the storyboard.
the XIB does not Have a toolbar in the view/layout thingy.
toolbar code called from viewDidLoad [self buildToolbar]...
-(void) buildToolbar{
blah blah blah
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
UIBarButtonItem *comments = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Comments-selected.png"] style:UIBarButtonItemStyleDone target:self action:#selector(cameraButtonTapped:)];
UIBarButtonItem *pin = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Pin-straight_60wide.png"] style:UIBarButtonItemStyleDone target:self action:#selector(pinBarButtonItemPressed:)];
NSArray *items = [NSArray arrayWithObjects:customBarButtonItem_right, flexableItem, comments, flexableItem, pin, flexableItem, refresh, nil];
// [self.navigationController.toolbar setItems:items animated:YES];
self.toolbarItems = items;
}
Thanks in advance!
I moved my call to toolbarSetup from viewDidLoad to viewWillAppear. Apparently the 2nd VC's viewDidLoad doesn't fire when the top most VC is popped off the stack.
viewWillAppear fires followed by viewDidAppear, etc....

Custom back button selector not triggered

I have two UIViewcontrollers, let's call them vcA and vcB in an UINavigationController.
I want vcB to have a custom backbutton that triggers some code, the goal is do some custom animation
In vcA I put this code:
UIViewController *vcB = [UIViewController alloc] init]
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack)];
self.navigationItem.backBarButtonItem = custombackBackButton;
[self.navigationController pushViewController: vcB animated: YES];
Then I added this code both in vcA and vcB:
-(void) handleBack
{
NSlog(#"Going back to vcA");
}
The handleback method is never called. Any hint?
Thanks
Nicola
Dont get me wrong but i think you need to be clear about push pop of UINavigationController.
A navigation controller manages views by pushing/popping them on/off the controller's view stack. When you push an item, the current view slides off screen to the left, and the new view slides over from the right. Ofcourse these animations can be changed according to your wish.
I think This is what you need completely.
Put this in vcA where you want to push vcB from vcA.
UIViewController *vcB = [UIViewController alloc] init];
[self.navigationController pushViewController: vcB animated: NO];
[UIView transitionWithView:self.navigationController.view
duration:0.8
options:UIViewAnimationOptionTransitionFlipFromRight
animations:nil
completion:nil];
In vcB, you can make a barbuttonitem
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:#"BackToVcA"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
In addActionMethod: you can put the below code for navigating back with your required animation
- (void)addAction:(id)sender
{
UIViewController *vcA = [UIViewController alloc] init];
[[self retain] autorelease];
[self.navigationController pushViewController: vcA animated: NO];
[UIView transitionWithView:self.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
[self.navigationController popViewControllerAnimated:NO];
}
Use trick given by William Jockusch Setting action for Back Button
And also As per the Updating the Navigation Bar
If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.
If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.
So if you want to have custom selector You need to write this inside vcB NOT IN vbA
vcB.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack)];
self.navigationItem.backBarButtonItem = customBackButton;
}
-(void) handleBack
{
NSlog(#"Going back to vcA");
}
NOTE:
UINavigationController Class Reference
When this navigation item is immediately below the top item in the
stack, the navigation controller derives the back button for the
navigation bar from this navigation item. When this property is nil,
the navigation item uses the value in its title property to create an
appropriate back button. If you want to specify a custom image or
title for the back button, you can assign a custom bar button item
(with your custom title or image) to this property instead. When
configuring your bar button item, do not assign a custom view to it;
the navigation item ignores custom views in the back bar button
anyway.
Here is is mentioned that If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) but it ignores custom view. So your selector is not invoking.
Your doing mistake. First Push to vcB and in vcB viewDidLoad method put this code.
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(handleBack)];
self.navigationItem.backBarButtonItem = custombackBackButton;
and also put this in vcB:
-(void) handleBack{
NSlog(#"Going back to vcA");
}
you can not modify the backBarButtonItems action , it is do the default (back) action , you should do your custom thing in the leftBarButtonItems
in your vcBs class add this :
- (void)viewWillAppear:(BOOL)animated
{
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack)];
self.navigationItem.leftBarButtonItem = customBackButton;
}
- (void)handleBack
{
NSLog(#"back");
}

Adding a UIBarButtonItem programmatically to UINavigationBar

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:self
action:#selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
But I do not see my UIBackBarButtonItem on the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?
I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar as well. Any help would be appreciated. Thanks!
You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
To get the Back Button on the left side like you wish, Try changing
self.navigationItem.backBarButtonItem = backButton;
to
self.navigationItem.leftBarButtonItem = backButton;
making a call such as this from a view controller
{
NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:[NSBundle mainBundle]];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
[vcRootView release];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
}
will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.
In The NextViewController implementation file all you need is this
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self
action:#selector(barButtonBackPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
}
-(void)barButtonBackPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
to have the back button to dismiss the modalview. Hope it helps.
Use below code snippet :
//Add button to NavigationController
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#“back”, #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
//Perform action on back Button
- (void) goBack { // Go back task over-here
}
Different style types available are :
UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone
You may use this setters without creation new UIBarButtonItem:
[self.navigationItem.leftBarButtonItem setAction:#selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];

Resources