Segue not calling prepareForSegue - ios

Perhaps I am missing something simple. I added a modal segue from a button to a view controller. I then added some steps to prepareForSegue (and checked I had named the segue correctly). I have done this a few other times with no problem.
Now, when I click the button, the modal window opens, but the prepareForSegue does not fire. I tried putting a log statement in the prepareForSegue before it even checks the description of the label (so theoretically it should fire for any segue). But I get nothing logged.
Any ideas?

Connecting a segue from a button to the next controller is the correct way to connect it, just remember that prepareForSegue: is called on the VC that owns the button not the incoming controller. You get the incoming controller by calling [segue destinationViewController].

Well I found the rookie error I suspected. I duplicated a VC and forgot to set it's class to my new VC class.

Wire up the Segue to the VC not the button. Then in the touchUpInside event, put
[self performSegueWithIdentifier:#"segueid" sender:nil];
I almost always wire the segue up to either the VC or a tableviewcell (if I am using a static cell TV)

You should set cell's reuse identifier set before segue is called.
I tried lots of solutions like above
and checked VC settings
but didn't set the prototype cell's reuse identifier.
Only after I set this to "Cell" , it worked finally.

Related

Segue performs even though identifier is not met

i've created a segue form a uibutton to a viewController with the style push. I only want this segue to be pushed if a criteria is met. Therefor i've created an identifier like this:
[self performSegueWithIdentifier:#"LoginSuccessful1" sender:self];
But it seems like that the segue is beeing pushed even though the performSegueWithIdentifier has not been called. How can i fix this?
Instead of having the segue go directly from the button, hook up the UIButton to an IBAction on your view controller. Then, in this IBAction, check your condition and call your performSegue. When hooking a segue directly from a button, the ViewController is not consulted first.
Remove the segue from button and connect it from ViewController to AnotherViewController as pushsegue not from UIButton
And check the condition and perform the segue
if(this == that){
[self performSegueWithIdentifier:#"LoginSuccessful1" sender:self];
}

segue not going to destination controller

In my app I have a manually triggered push segue called "details". It is wired from the source controller to the destination controller and it's identifier is set. In my code I call
[self performSegueWithIdentifier:#"details" sender:sender];
I can see the prepareForSegue function firing and have verified that the destination controller is of the proper type. I pass the destination controller a few bits of data it needs to display correctly. Oddly enough NOTHING happens. The app does not go to the destination controller.
Here are some things to look out for if you are having an issue with performSegueWithIdentifier while using a storyboard
You have correctly hooked up your transitions in your Storyboard
Your Segue Identifier matches the one on your Storyboard
Your ViewController's class has not become deallocated
(This can sometimes occur accidentally by calling from another class)
Try setting sender to "self" rather than sender
Also, if you are planning on passing data between ViewControllers it is suggested that you use the prepareForSegue method, allowing you to pass values before segue'ing.

Segue transition from table view cell to another view controller works only some arbitrary times - why?

I use three view controllers and on each view controller I put UITableView and UITableViewCell. When I connected from the first view controller's table view cell to another view controller's from within storyboard and ran the simulator, the segue works as expected. However, when I connect from the second view controller's table view cell to the last view controller from within storyboard IN THE EXACTLY SAME WAY as the first one, then for some reasons the transition doesn't work.
If I define didSelectRowAtIndexPath: and within it call [self performSegueWithIdentifier:#"showDetail" sender:self]; in the second view controller's implementation file, the transition can work as expected. I don't care about whether it's storyboard segue or methods defined in my code to perform the transition, as long as the transition does work correctly.
However, I'd still like to know why such inconsistency occurs. As I said, I connected two storyboard in the exactly same way in both cases and checked out attribute inspector and connection inspector, but I don't find any differences between the two connection there.
Also, while the first view controller can perform the transition without the method being defined, when I define it then the transition doesn't work, issuing the following error message:
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
I think I cannot use both approaches (i.e. storyboard segue and method calls) - I just wanted to know what derives the inconsistency here.
I use iOS 7 and Xcode 5.
First of all, if you use push segues, you can't make a push for the second segue if the first segue is modal (unless you embed your second VC in a navigation controller).
Second, make sure de segue identifiers are unique for each segue.
If you ctrl+drag a segue in storyboard, don't call performsegue in code, you just attempt to do the same operation twice. If the segue is in storyboard, in code you should use prepareforsegue delegate.
Another way of doing all of this is not using any segue in storyboard, then in code #didselectrowatindexpath you can instantiate your destination vc using [storyboard instantiateviewcontrolerwithidentifier...], then [self.navigationcontroller pushviewcontroller..] for a push segue or [self presentviewcontroller...] for a modal.
EDIT: Also, when you ctrl+drag, make sure you are dragging from the cell and not from the table.
Self Answer
I finally found out that the issue was not caused in storyboard - it's on the implementation code. Since I have to use UITableViewCellStyleValue1, I cannot use dequeueReusableCellWithIdentifier, and for some reasons the dequeueReusableCellWithIdentifier has to be used in order to make an automatic transition from cell to another view controller from within storyboard only. I checked out that using dequeueReusableCellWithIdentifier and disabling UITableViewCellStyleValue1 temporarily makes it successful to make the transition without didSelectRowAtIndexPath: method being defined.

IOS7 - performSegueWIthIdentifier does not have any effect

I have a problem in iOS7 where I am calling a segue with performSegueWithIdentifier (I have code just like this that works just about everywhere else), then I log the segue in prepareForSegue, then I log again the view controller (VC) that the segue is supposed to push to the top.
prepareForSegue gets called appropriately and the segue has the correct string as its identifier property. Yet the VC that it is supposed to push to the top never gets initialized nor viewWillAppear gets called.
The segue I am talking about, which is the only one that does not work (all the other ones work in both ios6 and 7), is the one leading form the center VC to the right VC. By the way, this works flawlessly in iOS6.
What could be the cause?
the code:
-(IBAction)gotoMainMenu:(id)sender{
DLog(#"DifferentName");
[self performSegueWithIdentifier:#"DifferentName" sender:self];
}
Get in the habit of not wiring up segue's to buttons. Wire them up to the VC and then in the touchUpInside action, fire off the performSegueWithIdentifier.
I had the same issue and solved it as follows. In view A I had a segue that was triggered by a button (UIButton) and the button was also connected to an action in my controller. When I clicked the button in View A, View B would appear as expected. However, when I tried clicking a button in View B to go to View C nothing happened just as you described above.
In my case the issue was resolved in View A. I removed the segue that was tied to the button and let the IBAction that was associated with the button handle calling the performSegueWithIdentifier, then I created a new manual segue that was only tied to the view and voila things worked as expected again.
In short, I guess make sure you don't have both and action and a segue linked to the same button. Hope this helps.

Fire a segue conditionally to multiple View Controllers

After struggling for days on firing a segue conditionally, I managed to solve it thanks to Simon's answer here. Please take a moment to have a look or you might not understand what I'm talking about below. I didn't copy paste his answer because he's already explained it nicely over there.
Now I've faced a new question. What if I have multiple View Controllers that I want to segue to from one View Controller?
To explain it further : Say I have one MainViewController with 2 buttons. When clicked upon each button, it should segue to their respective View Controller. First button to FirstViewController and the second button to SecondViewController.
The method described in Simon's answer can be used when you segue from one View Controller to another View Controller. Since in that method, you tie the segue to the View Controller itsrlf and not to the button, you have only one segue with an identifier for that particular View Controller. Therefore I cannot distinguish between the button taps separately.
Is there a workaround to solve this problem?
Thank you.
It might be bit premature to say this but I guess you should look into Segue more deeply.
Yes you can perform segure from button. Just control click the button and drag the cursor to view controller you want it SEGUE'd. And from my understanding only condition there is each button tap results a segue to a fixed view. There is no condition there.
Also, you can push the navigation controller manually by
YourViewController *destViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"YourDestinationViewId"];
[self.navigationController pushViewController:destViewController animated:YES];
UPDATE:
prepareForSegue is too late to stop a segue from proceeding. Yes you can create multiple segues from your view to other view controllers. And in this case you have to do so. Don't reate a segue from button, just define a IBACtion on the button click you can do the validation from there,
if(validationSuccess) {
[self performSegueWithIdentifier:#"segue1" sender:self];
}
if you are using ios6
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
return YES on validation success and NO on failure to stop it from proceeding.
I suggest you look a bit at reworking your code logic.
If I understand correctly, you have a VC (embedded in a Nav. Controller) with 2 buttons and you have figured out how to segue each button to a different VC.
Your problem is you want to make sure that even if one of the buttons are pressed, a validation is done before an action takes place. I would advise this is bad User Interface design because the user has the illusion that this button might do something and then they click it and nothing happens.
UIButton can be connected to IBActions (to initiate actions) and IBOutlets (to set their properties). If this is a button created in IB directly, I would connect it to your class as an Outlet property:
#property (nonatomic,weak) IBOutlet UIButton* myButton;
And then set its enabled value:
self.myButton.enabled=NO;
This will keep the button and dim it. This is much better UI design and the user knows they should not press the button because some condition is not satisfied.
I would rework the code so that you set this value as disabled by default for example and enable it appropriately in your code whenever your "condition" is satisfied.
Obviously if this button is created programmatically (in your code without IB) then it is easy to just use the second command above.
Hope this helps.
I just wrote another way to call multiple detail views from a single table. Each cell could essentially make a different view be displayed. The code is similar to what you see in this post but you essentially use identifiers and attributes on the list item to determine which view to show.
https://codebylarry.com/2016/07/15/multiple-detail-views-in-swift/
override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 1 {
self.performSegueWithIdentifier("secondView", sender: self)
} else {
self.performSegueWithIdentifier(“others", sender: self)
}
}

Resources