So I have two views that I am trying to connect with a navigation.
I have embedded them in a navigation controller and created a push segue between them in the storyboard:
It the viewDidLoad of the first controller I add a button to the navigation together with a method it should call when clicked:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonClicked:)];
self.navigationItem.rightBarButtonItem = myButton;
}
- (void)buttonClicked
{
NSLog(#"hello");
}
It compiles fine and I can run it in the simulator but when I click on the button in the navigation bar, instead of logging "hello", I get:
Any ideas how to solve it? I have run out of ideas. I am using the latest XCode.
Try to remove the : in your target action:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonClicked)];
if you want to use the colon define your method like this:
- (void)buttonClicked:(id)sender
{
NSLog(#"hello");
}
Related
I am using 3 view controllers A, B, C.
Added a push segue in storyboard from A to B (say segue1) and B to C (say segue2).
A is embedded in a navigation controller.
In A, I am adding a right bar button and performing segue1 on its action.
Similarly in B, I am adding another right bar button and performing segue2 on its action.
If I click the button rapidly multiple times, view B gets pushed once and then view C gets pushed multiple times on it.
Is this a known issue? or Is there something wrong in my implementation?
Edit: Adding code
ViewController A:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertB:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)insertB:(id)sender {
[self performSegueWithIdentifier:#"segue1" sender:sender];
}
ViewController B:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertC:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)insertC:(id)sender {
[self performSegueWithIdentifier:#"segue2" sender:sender];
}
Set the userInteractionEnabled property of the button to false when pressed, and after pushing change it to true.
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;
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonItemStylePlain target:self action:#selector(refreshButtonClicked:)];
[self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}
- (void)refreshButtonClicked:(id)sender {
NSLog(#"Refreshed.");
}
I added a navigation controller programmatically in AppDelegate and set this view as the root.
When I was trying to add a navigationItem programmatically as shown above, the refreshButton showed in the navigation bar but refreshButtonClicked: was not invoked.
What's the problem? Any suggestions and comments are welcome.
Make a call to the super class in your viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonItemStylePlain target:self action:#selector(refreshButtonClicked:)];
[self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}
It will ensure that the root class setup is done
Problem solved. Weird. After I clean the builds, delete the derived data and restart Xcode, it goes runs properly.
I started out with a navigation based project and am pushing further views onto the controller. The problem is that if I do not give a title to the navigation item then the back button is not drawn! Only if I give the navigation bar a title, will the back button come. It seems apple could'nt write "back" or "go back" in case of NO title. I do not want to give the navigation item a title(I'll use a label inside my view). So how do I fix this?
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"Home"; /// <- without setting the title, the back button won't show !
}
In the view didLoad method, if I remove the title, the back button won't show
Just create the back button yourself:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
[back release];
}
(If you prefer dot notation, self.navigationItem.backBarButtonItem = back;)
I started my xcode project from a NavigationViewController template. And now, when I push a view, that view comes up with an edit button by default and no back bar button. I have commented out the editButton code and the corresponding setEditing delegate method. But I can not get the back button to show up. What am I doing wrong?
Pushing the new view:
PlaylistViewController *playlistViewController = [[PlaylistViewController alloc] init];
playlistViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:playlistViewController animated:YES];
[playlistViewController release];
in my PlaylistViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"";
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
// doesn't matter if this is here or not
self.navigationItem.hidesBackButton = false;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
When you tell a UINavigationController to pushViewController, it automatically sets the navigationItem.leftBarButtonItem to be a button with the title of the previous UIViewController's title.
If no title is set, the default text is "Back".
If the title is set to "", no button will display at all.
self.title = #"";
Try changing this text and your back button should match the text set here.
Or you can manually override the text of the leftBarButtonItem from your new UIViewController.