Segue related functions - ios

I know, I can programmtically do segue action by using:
-performSegueWithIdentifier:sender:
I also know the other segue related method on UIViewController, -prepareForSegue:sender:, in which I can do customization on the destination view controller.
But I am wondering, which one is called first?
say in a scenario, there is ViewControllerOne that has a button which triggers the -performSegueWithIdentifier:sender: , then goes to ViewControllerTwo.

The first is performSegueWithIdentifier:sender: and actually this is the one you performed.
The second call you mentioned prepareForSegue:sender: is a delegate call, which you are not forced to implement

Got it myself:
Before performSegueWithIdentifier
Before prepareForSegue
After prepareForSegue.
After performSegueWithIdentifier

Related

ios transitiondelegate with segue

I have implemented a transitiondelegate, and assigned it to the destination viewcontroller in the segue (at method viewDidLoad). My question is: How iOS knows to use the same delegate when peforming the segue from source to destination? I mean, I didn't assign the delegate at the source viewcontroller. Only assigned it at the destination, and still it is being called when performing the segue.
Who should have the delegate? The presentingViewController or the destinationViewController?
I would expected it to only be called when doing a segue the opposite way.
Thanks, and hope I was clear.
Before the transition, the destination view controller asks its transitioning delegate, for the animation controller.
You should set the transitioning delegate for the destination controller.

How to select ViewController?

The app starts on ViewContoller
I push TutoViewContoller
On some action, I pop TutoViewContoller
I would like to call a method of ViewContoller just after popping TutoViewController. How can I do that ?
You can implement viewWillAppear of ViewController. If you really need to distinguish between TutoViewController and other viewcontroller you have many options to call before you pop it: delegate pattern, passing reference of ViewController to TutoViewController etc.

How prepareForSegue works?

I am curious about this method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
I find this method in UIViewController.h, and it's a regular method.
I check it, it is not a delegate method (it is not a protocol).
Because UITableViewController extends from UIViewController, we can use this method in our custom class for any table views.
Strangely, this method behaves like a delegate, it fires without any calls.
It fires when the view is going to do a transition.
As far as I know, this behaviour only exist in delegation.
How can a UIViewController calls prepareForSegue method in it's child class?
prepareForSegue is a method that is called as and when a transition is going to occur between ViewControllers. So, whenever a segue is made, this method will be called mandatorily. Now if your main View has a subclassed View and you want to perform a Segue from it, you can do it using the following function.
[self performSegueWithIdentifier:#"segueIdentifier" sender:self];
In this case, this method will invoke a transition with the specified identifier which corresponds to a segue. And whenever this method is called, prepareForSegue is called immediately after this, after which transition occurs.
For information, it is better to connect segue between ViewControllers rather than creating segue directly from Controls in the view. As sometimes, some conditions need to be checked before performing a Segue.
Hope this helps.
Like many other methods of UIViewController, your subclass can override the default behavior of many UIViewController methods. A couple of examples include viewDidLoad and viewWillAppear.

Cannot programmatically perform segue

I have a storyboard segue with an identifier that is 'Push to ResumeView'.
I try calling it in the ViewController that I'm in at the point, by doing
[self performSegueWithIdentifier: #"Push to ResumeView" sender: self];.
But nothing happens?
I'd much rather just push the ViewController using the top NavigationController or something, but I can't see how to do that either.
Try implementing the shouldPerformSegueWithIdentifier:sender: or prepareForSegue:sender: methods in the 'from' view controller. Put a break point or NSLog() inside the method to inspect the segue identifier. This will prove that you indeed set up the segue correctly in the storyboard.
If you want to manually push your next view controller to the top of the navigation controller, use pushViewController:animated:. However, if you are using storyboard, the preferred way is to use segues.
Try this one.
UIViewController *yourResumeView=[self.storyboard instantiateViewControllerWithIdentifier:#"PushToResumeView"];
[self.navigationController pushViewController:yourResumeView animated:YES];

Segue not calling prepareForSegue

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.

Resources