UIBarButtonItem not reacting to click [duplicate] - iphone

From the rootViewController I navigate to a UIViewController
if (self.contr == nil) {
ExampleViewController *controller = [[ExampleViewController alloc]
initWithNibName:#"Example"
bundle:[NSBundle mainBundle]];
self.contr = controller;
[controller release];
}
[self.navigationController presentModalViewController:self.contr animated:YES];
In the UIViewController I have the method
-(IBAction) goBack:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
I added the signature to the .h file.
In the .xib file, I have a UIToolbar with a UIBarButtonItem. I connected the button to the File's Owner - goBack:
Everything appears in the screen, but when I click on the button, goBack isn't called. I also tried to do this programatically instead, but I got the same result - everything appears, but no reaction to the click.
Any ideas why it isn't working?
Edit:
I just found out something invisible is over the toolbar. If I click on a specific point (over the toolbar), then goBack: is called. Since I navigated to this screen using presentModelViewController, the navigation bar isn't appearing... but probably it's there and that's what is hiding the tool bar.

Have bind your Toolbar with File Owner?
As your UIBarButton is subview of UIToolbar so you have to bind Toolbar with File Owner.

Presenting a modal view controller do not require you to pass through a UINavigationController. I suggest you to change this:
[self.navigationController presentModalViewController:self.contr animated:YES];
[self.navigationController dismissModalViewControllerAnimated:YES];
to this:
[self presentModalViewController:self.contr animated:YES];
[self dismissModalViewControllerAnimated:YES];
Let me know if this helps.

Try this in the goBack method :
[self.navigationController popToRootViewControllerAnimated:YES];

If you are not hitting the breakpoint that means you did not connect them properly in the xib.

Related

Navigate from a ViewController to another

I have this design in my storyboard:
As you see the middle ViewController is connected to a Navigation Controller ( I just made the navigation bar invisible). In this middle page I add all the controls programmatically ( using Parse mobile platform login wizard ).
The thing is I want to navigate to the third page, when Login is done successfully.
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES];
ViewController2 *viewController = [[[ViewController2 alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
printf("%s", [#"Ali" UTF8String]);
}
the message will be printed, but the navigation is failed. Can you help? Perhaps I need to fix something in storyboard.
I'm not sure you should be sending the dismissModalViewControllerAnimated: message. Are you presenting another view controller modally on top of your login view controller?
Anyway, when you have configured a view controller in a storyboard, you can't create the view controller using alloc and init. You need to ask the storyboard to create it. There are a couple of ways to do this.
One way
One way to get the storyboard to create your ViewController2 is to make a push segue in your storyboard.
Open your storyboard.
Control-drag from the login view controller to the ViewController2.
Choose the “push” segue type.
Click on the segue.
Choose View > Utilities > Show Attributes Inspector.
In the Attributes Inspector (right-hand side of the window), set the segue identifier to “didLogIn”.
To execute the segue, do this:
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES];
[self performSegueWithIdentifier:#"didLogIn" sender:self];
}
Another way
Another way to get the storyboard to create your ViewController2 is to give it a storyboard ID and ask the storyboard to instantiate the view controller by ID. Then you can push the view controller.
Before you can ask the storyboard to create it, you must give a “storyboard ID” to the ViewController2 instance in your storyboard.
Open your storyboard.
Select the ViewController2 instance.
Choose View > Utilities > Show Identity Inspector.
In the Identity Inspector (right-hand side of the window), enter “viewController2”. Case is important!
Then, in your code, ask the storyboard to instantiate viewController2:
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES]; // Should this be here?
ViewController2 *viewController = [[[ViewController2 alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
}
Try this way,
ViewController2 *myViewController=[storyboard instantiateViewControllerWithIdentifier:#"TheNameOfYourController"]
[self.navigationController pushViewController:myViewController animated:YES];
Because a lot of relevant code/settings is missing I would suggest the following:
Use this code:
-(void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissViewControllerAnimated:YES completion:NULL];
Class klass = [[NSBundle mainBundle] classNamed:#"ViewController2"];
ViewController2 *viewController = [[klass alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
NSLog("%#", #"Ali");
}
If that doesn't work check how you suppose to initialize ViewController2.
If all this doesn't help please try to post more relevant code.
To move next viewController using Storyboard Identifier
nextViewController *objnextViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"nextViewControllerIdentifier"];
[self.navigationController pushViewController: objnextViewController animated:YES];

displaying navigation bar when switching to another view

I have a button when it pressed, I want it to take me to another view (the "news" view). Within the news view, I want there to be a navigation bar with a back button. I have a navigationcontroller setup throughout my app but I can't seem to get this to work when this button is pressed. It takes me to the view I want but there is no navigation bar and no back button. This is my code that is implemented when the button is pressed.
If anybody know what I am doing wrong, it would be much appreciated.
Thanks
-(IBAction)news
{
newsViewController *view1 = [[newsViewController alloc] initWithNibName:#"newsViewController" bundle:nil];
view1.title=#"news";
[self.navigationController pushViewController:view1 animated:YES];
}
I am not in my mac, so I can not test code, but if it is working and the only issue you got is not show the bar, what you need to is set the bar to be visible:
From apple docs:
The navigation toolbar is hidden by default but you can show it for
your navigation interface by calling the setToolbarHidden:animated:
method of your navigation controller object. If not all of your view
controllers support toolbar items, your delegate object can call this
method to toggle the visibility of the toolbar during subsequent push
and pop operations.
Something like that is supposed to work:
-(IBAction)news {
newsViewController *view1 = [[newsViewController alloc] initWithNibName: #"newsViewController" bundle:nil];
view1.title=#"news";
[self.navigationController pushViewController:view1 animated:YES];
//Add this line!
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
I hope it can help you.
write the below code in page where you want to show navigation controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}

Switching Views gives me a SIGABRT Message

I'm having a little predicament switching between views, here.
Alright, so, I have this view controller class in my iPhone project called "BaseViewController," which is the default, which has a button called "GoToNextView." I added another ViewController to the storyboard called "NextViewController," and then I created another custom view controller class called "NextViewController." Under the inspector window for NextViewController on the storyboard I changed its custom class to "NextViewController;" I'm assuming everything should be hooked up, now. When I click on the "GoToNextView" button, though, the application stops with a SIGABRT message.
Here's the code for my button click action in the BaseViewController class.
- (IBAction)Transition_Next:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"SecondView"
bundle:[NSBundle mainBundle]];
[self.view addSubview:nextViewController.view];
}
What might I be doing wrong, here?
Thanks in advance...
You can use:
- (IBAction)Transition_Next:(id)sender
nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nextViewController animated:YES];
}
Instead of your code and it should work! Hope that helps!
You can use [self.navigationController pushViewController:nextViewController animated:NO]; or if you don't have a navigation controller, you may use
[self presentModalViewController:nextViewController animated:NO]; instead of [self.view addSubview:nextViewController.view];

How to Navigate one view to other view

I used button action to to pop sub view sing below code;
- (IBAction)pickerUpBtn:(id)sender {
PickerPopUpController *screen = [[PickerPopUpController alloc]initWithNibName:#"PickerPopUpController" bundle:Nil];
[self.view addSubview:screen.view];
[self presentModalViewController:screen animated:YES];
}
after i need to go previous window; So i used another button action for it and add bellow code inside action
PickerViewController *screen = [[PickerViewController alloc]initWithNibName:#"PickerViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:screen animated:YES];
But It does not navigate to previous view . How could i do it ??
to go back,
do
[self dismissModalViewControllerAnimated:YES]
You need to put only that single code in the Button action..No need of making object of parentViewController.
ie,
-(IBAction)btnaction:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
You need to add cancel and done for your ViewController which is present modely to come back to parent view Controller.
After implementing the Cancel button, you have to dismiss your viewcontroller in the action function of Cancel button using the dismissModalViewControllerAnimated function
[self dismissModalViewControllerAnimated:YES];
For push view controller, you can do like below,
PickerViewController *screen = [[PickerViewController alloc]initWithNibName:#"PickerViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:screen animated:YES];
Then for go back, you have to add one button in "Screen" view controller and it's click event you have to write below code,
[self.navigationController popViewControllerAnimated:YES];
Hope, it would be helpful.
Let me know in case of any difficulty.
If you are using the method "presentmodelviewcontroller" then you have to write below line in button click.
[self dismissModalViewControllerAnimated:YES];

How to change/add view to rootcontroller's view in UINavigationController

I have an application where I need to have a "custom" setting page. In my delegate I add a UINavigationController's view to the window with a UIViewController as rootviewcontroller.
In the rootviewcontroller I want to have a button and when I press the button the whole view changes to the settingview that I made. I only need the code to change view.
Thanks for the help in advance.
If Settings View a UIView SubClass then In the Button Action method you can do something like this:
[self.view addChild: settingsView]; //provided that settingsView is already allocated.
If you have written down a separate UIViewController SubClass for Settings then you can do something like this in your Button Action Method:
SettingsViewController *controller=[[SettingsViewController alloc]initWithNibName:#"SettingsViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];