Unrecognized selector sent to instance when calling back button programmatically - ios

I'm viewing a detail view when clicking on a record from the table. When I call the back button from the detail view using
- (IBAction)loadDispensary
{
[self.navigationController popViewControllerAnimated:YES];
}
I get the unrecognized selector error.
I've tried pushing the the detail controller the following two ways.
Push type 1
detailViewController *detailView = [[self storyboard] instantiateViewControllerWithIdentifier:#"detailView"];
detailView.strain = self.selectedStrain;
[self.navigationController pushViewController:detailView animated:YES];
Push type 2
[self performSegueWithIdentifier: #"detailSegue" sender: self];
//Set the strain on the prepareForSegue

how the button is connected? can u show the code? perhaps you might have not properly linked the button to the IBAction.

I think you may be trying to pop a view controller that is already popped. If you have a UINavigationController and you "Push" a view controller through a segue, the back button that is displayed at the top left of the screen is already configured. You should not have to put one there yourself. If you programmitically place a button in the left bar button position it will display in place of the UINavigation's button.
Try removing the button and allowing the UINavigationController to manage going back and see if that works better.

Someone had asked me to paste the full error code and when I went to do that I saw that the method the button was pointed to was no longer there, so it was trying to call a method that didn't exist.

Related

Segue from screen with several buttons?

I am trying to write an app using UINavigationViewController. My first screen has several buttons on it, and on the click of each button, I want to segue to a UIViewController. I know that I can add a segue on each button, all pointed to the UIViewController that I want to go to, but I was wondering if it is possible to use only one segue that can be fired from each of the buttons.
If that is not possible, I was wondering if it was possible to open the second UIViewController from the first one, on button click, and provide a Back button like the UINavigationView provides. I did manage to get everything on this idea working, except for the back button. I mean I can put a standard button somewhere on the screen and go back, but I'd like the standard back button on the UINavigationView.
Phew! I'm not sure if that makes any sense.
I know that I could also use a tableview, but I'm trying to set this up with buttons.
Thanks
Edit: Thank you to everyone that answered. I now have this working. I would vote up the answers, but I don't have enough posts to do it. I appreciate the answers!
If you need to have separate action functions for each button, suggest that you segue from the main controller to the other controller and create a segue identifier (see xcode procedure below); then, use performSegueWithIdentifier from each of the button action functions. You can also take advantage of the prepareForSegue. To create the segue, control-drag from the left button in the controller in the storyboard to the controller you want to segue to and pick show.
Check the example code in swift that I did for a very similar problem in the SO reference
Linking View Controllers through button
You can embed the main controller in a navigation controller and that will give you the ability to navigate back. If you have multiple layers you can also use unwind segue.
Link each button to one single action (ex. buttonClick) in that ViewController and then perform the appropriate segue using pushViewController method on self.navigationController
-(IBAction)buttonClick:(id)sender {
if(sender.id == self.button1) {
DestinationViewController *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"VC_IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];
}
Or if you already have that 1 segue defined in storyboards you can use
[self performSegueWithIdentifier:#"SegueIdentifier" sender:self];
And use that inside the buttonClick method. Using the 1st example, or the second one as long as the segue you setup in the storyboards is a push then you should already get the back button as that is the default behavior for pushing view controllers onto the navigation stack.

avoid navigation transition error with view controller identifier

I'm using Xcode 5 with storyboards and I should do something like this:
ViewController with a Start button that launches IntermediateViewController
IntermediateViewController that does an activity and then returns the value to the ViewController.
For the passage ViewController->IntermediateViewController I've set the start button to trigger a push segue. Actions are done and this part seems ok.
Now I have to go back to ViewController passing a string I got in IntermediateViewController methods.
If I use:
ViewController *viewController=[self.navigationController.storyboard instantiateViewControllerWithIdentifier:#"viewController"];
viewController.passedString=_mystring;
[self.navigationController pushViewController:viewController animated:NO];
I get this error:
"Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted"
Is there a way to retrieve my viewController first instance through its identifier or any other solution that will lead the app back to viewController setting also its variable?
Thanks in advance
Even if it's not the exact answer to the question, I solved using this steps:
1) checked in storyboards that each element triggers only one action or segue, not both.
2) calling second view using:
[self.navigationController pushViewController:intermediateViewController animated:NO];
2) going back to previous view.
[self.navigationController popViewControllerAnimated:YES];
3) doing this before popping:
pass string between controllers

Redirect to a specific view controller

Just Have an issue and a "miss" in my programming "capacity".
I've got a tableview controller with some data parsed from a json.
When you choose a "news" you to to the detail view with all data in it.
Everything is ok, but I add a "check" If you are logged or not.
And if not you are "redirected" to login screen.
I try to do it with a segue (modal). It's working, but when I do it, my "navigation" is broken, like if he "lost" he's path.
I try to do it programatically like :
LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
But when I'm doing it like that, nothing happen, my "detail view controller" load without redirecting
and got that log:
nested push animation can result in corrupted navigation bar Finishing
up a navigation transition in an unexpected state. Navigation Bar
subview tree might get corrupted.
Did someone have a hint for me ?
Thanks
you are doing it all wrong. you need to study the very basic of seque programming (storyboard).
follow this link
LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
in storyboard you already have a push segue, & again you are pushing loginviewcontroller. thats why you are getting "nested push animation" warning.
This isn't the normal way to do it. You would normally present that login view controller modally.
If this is just the way you want it you could do it using the following steps:
1) Connect your DetailViewController to your TableViewController (not from cell itself, but from that yellow icon which represents your ViewController at the bottom black bar). Choose push if you like and add the identifier to that Segue (for example, "DetailSegue")
2) Connect your LoginViewController to your TableViewController just like you connected your DetailViewController and add the identifier to that segue (for example, "LoginSegue").
Now, when the user clicks on some cell, you wish to check if session is still active, if it is you will do [self performSegueWithIdentifier:#"DetailSegue" sender:self];, and if it is not, you will do [self performSegueWithIdentifier:#"LoginSegue" sender:self];
Hope this helps, cheers.
it's pretty tough to understand what you want to do, the question isn't too clear.
If you want to link to a scene in your storyboard though, create a segue to this by ctrl + click & drag from the initial scene, then give the segue an ID (do this by clicking on the segue and using inspector to set an ID)
then in your tableview controller, where ever you are picking up your tab (assuming this is in the tableview delegate method) you can call the segue programatically
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"YOURSEGUEID" sender:self];
}

Move to start of storyboard from button on last screen

I'm trying to get a storyboard to bring the user back to the first screen of a storyboard when a button is pressed.
I have the button click hooked up and the method is hit when it's tapped but it's either doing nothing or crashing the app depending on what I've got in that method (I've tried so many things at this stage that I can't remember the original setup)
The best I can achieve is that once tapped the user gets brought to the third screen in the storyboard, rather than the first.
Here's the storyboard, I want to get the user to move from the button circled in red back to the very first view controller (top left of screenshot).
Maybe the storyboard layout will help people, I'll post some of the various methods I've tried as well.
Here's the method
- (IBAction)done:(id)sender
{
//Multitude of attempts have been in here, either they don't do
//anything or they just send the user back to the NEW REPORT screen.
//Button does nothing in these following events
[self.navigationController popToRootViewControllerAnimated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
If you want to start fresh, have the button instantiate a new instance of the first controller, and set it as the window's root view controller:
FirstViewController *first = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
self.view.window.rootViewController = first;
It's not clear to me exactly how you're getting to that controller with the button in question, but you might want to put dealloc methods in all your controllers with a log to see if all are getting deallocated at some point in your navigation, and when you go back to the new first controller as I've outlined above.

ViewDidLoad Method is called again

I have two View Controller. In the first I do some stuff and then I can push the second View Controller by clicking a button (the button connected with the other ViewController in the storyboard). There I can do some settings and so on. I get back to the first View Controller with the button "Done". But then the ViewDidLoad method is called again and all the "stuff" (text in textfields, ...) is gone.
I hope you guys understand my problem.
Why? And how can I disable this?
How are you going back to the first view controller from the second one? I think your problem is you're re-instantiating the first view controller when the user hits "Done".
Instead, you should be using either "popViewControllerAnimated" or "dismissViewControllerAnimated" to go back to the first view controller.
e.g: (one of these 2 should work):
[self.navigationController popViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
Maybe the firstViewController is unLoaded because of receiving memory warning. So when it opened again, it calls ViewDidLoad

Resources