is there any way to popView controller in Storyboard? - ios

I have an application in storyboard and make the connections to second view controller by push operation in the storyboard itself(without using coding).
Is there any way to pop to first view controller without writing any code and using the storyboard?
Note:by using navigation controller we will have back button but if a button is created in the second view controller and when we tap on that button we should pop to first view controller.

Storyboards don't provide a way to return from a segue without coding, but this can be easily accomplished with this code:
[self.navigationController popToRootViewControllerAnimated:YES];

Storyboards doesn't provide a "no-code" way to popViewController, you need to implement by yourself.
And if you want to pop to the first viewController you need to use:
[self.navigationController popToRootViewControllerAnimated:YES];

You can use an UnwindSegue as well since iOS6, although it does require some code)* for push segues. For a good explanation of UnwindSegues, see SO post, scroll down to "In a Nutshell"
)*Specifically, you need an unwind action, e.g.
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue

A solution that involves writing code once but can be used afterwards in your storyboards as a segue without further implementations would be some kind of PopSegue as subclass of a UIStoryboardSegue.
#interface PopSegue : UIStoryboardSegue
Overwrite the -perform method as follows.
#implementation PopSegue
- (void)perform
{
[[self.sourceViewController navigationController] popViewControllerAnimated:YES];
}
#end
Use a custom segue for back-navigation and specify the PopSegue class.

Related

Cancel button in Navigation View not working

code for cancel button is
- (IBAction)cancelButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
While tracing the program I can see the method is called however the view is not dissmissing to the previous view controller.
if you use Navigation View controller you should use
[self.navigationController popViewControllerAnimated:YES];
instead of dismissViewControllerAnimated
PS actually, I think you use storyboards and segues and in this case you should use UnwindSegue What are Unwind segues for and how do you use them?
Also you should read some common documentation about in app navigation...

How to perform custom segue across two storyboards?

so I inherited an old project at work that's broken up into a ton of different storyboards. While this hasn't really been too much of a hassle, the client now wants a custom segue animation that goes from a View Controller on Storyboard A to a View Controller on Storyboard B and I can't figure it out for the life of me.
I have the animation worked out if the views are on the same Storyboard, but I can't get it hooked up to a transition to another storyboard.
You can't do this without using a trick, because you can't connect a segue between storyboards. You can instantiate the controller manually in the other storyboard, then do whatever custom animation you want to present or push it in code. Since the main reason for using a segue (as opposed to transitioning to the new controller in code) is that you can see the connections between your controllers in the storyboard, there's not much reason to use a segue in your case.
If you really insist on using a segue, then you need to put a "dummy" controller in your first storyboard that you connect a custom segue to. The code in that segue switches out the dummy controller for the real controller you want to segue to in the other storyboard. You can find a reference to that technique here, http://spin.atomicobject.com/2014/03/06/multiple-ios-storyboards/.
Find below code for accessing another storyboard view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"storyboard_A_view_identifier"]) {
UIStoryboard *storyBoardB = [UIStoryboard storyboardWithName:#"storyBoardB" bundle:nil];
UIViewController *anotherViewController = [storyBoardB instantiateViewControllerWithIdentifier:#"storyboard_B_view_identifier"];
// use anotherViewController and perform custom animation on it
}
}

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];

Pop the current view using Segues/Storyboard on iOS 5

I am creating an app using iOS 5 SDK. I managed to push views using the Storyboard's Segues, but I cannot find the proper way to pop the current view and go back to the previous one.
I am not using any navigationController (the app doesn't have any top or bottom bars).
I don't think using modal or push segue the other way would be the solution as it instantiates a new controller.
Do I have to use a custom Segue with the opposite animation and deletion of the view at the end ? Or is there a better way ?
Storyboards in iOS 5 don't provide a "no-code" way to return from a segue -- that's something you'll need to implement yourself.
If you use "push" segues (which require a navigation controller), use the navigation controller's popViewControllerAnimated: method to undo the last push segue. (Or other methods to undo more; see the UINavigationController documentation.)
If you use "modal" segues, call dismissViewControllerAnimated:completion: on the view controller which presented the current view controller (which you can get from its presentingViewController property).
Update: In iOS 6 and later there's unwind segues for going "back" in a storyboard. It's still not a no-code solution -- and it shouldn't be, because you need to be able to do things like differentiating between "Done" and "Cancel" exits from a modal view controller. But it does let you put more of the semantic flow of your app into the storyboard. Apple has a tech note that describes them in detail, and they're also covered in the video from WWDC 2012 Session 407.
You could try calling [self dismissViewControllerAnimated:YES completion:nil]; from the controller you want to dismiss (whether the controller has been pushed, or shown modally).
Here is the related documentation : UIViewController Class Reference
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
Just to clarify.
In the class that was pushed. Simply wire up the following and the controller and view will be popped off.
[self.navigationController popViewControllerAnimated:YES];
Create Segue type "Custom" on your stroyboard. This can be from a button.
Create a new UIStoryboardSegue class named "popSegue"
In the popSegue.m file add the following;
-(void)perform{
UIViewController *sourceViewContreoller = [self sourceViewController];
[sourceViewContreoller.navigationController popViewControllerAnimated:YES];
}
-In the storyboard editor.
-Select the segue and change the Segue Class to "popSegue"
-Set the Identifier to "popSegue"
Done!
You can use the same "popSegue" class throughout your project.
Hope this helps
I'm using Xcode 5 also and here's how it's done. First, in the view code file that pushed the other, create an IBAction method in the .h file such as this:
- (IBAction)exitToHere:(UIStoryboardPopoverSegue *)segue sender:(id)sender;
Then in the .m file add this:
- (IBAction)exitToHere:(UIStoryboardPopoverSegue *)segue sender:(id)sender {
}
You can add any cleanup code you want executed in this method. Next go to your storyboard and select the pushed view. I assume you've got some kind of button on the view that the user taps to signal he's finished. Click on that button, hold down the key and drag to the the green box below the view which is the Exit. Release the mouse button but continue to hold the key. A popup will appear and your method will show in the list. Select that method. Now when the user clicks on the button, the view will pop and you'll be returned to the starting method.

What is the proper way to dismiss a modal when using storyboards?

Using storyboards, what is the proper way to dismiss a modal?
using IBAction and writing code to dismiss after a button click?
using segue and notify the parent view controller after a button click?
See Here Dismissing a Presented View Controller about halfway down
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it.
So you should use an IBAction and writing code to dismiss after a button click
According Alex Cio answer for Swift 3 and XCode 8.3:
Create class:
import UIKit
class DismissSegue: UIStoryboardSegue {
override func perform() {
self.source.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
But in storyboard you should choose:
Action Segue -> Custom -> dismiss
Only after this option appear on Action Segue menu
I've found that usually when I'm attempting to do this in storyboard I'd rather not create extra classes. It still makes sense to perform the dismiss from the presenting view controller, so that requires a class to back it.
If you create an IBAction in the presenting view controller and name it appropriately e.g.
- (IBAction)dismissAnyModel:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Then from storyboard wherever you want to trigger the dismiss from you create an action to the first responder as shown below. You can extend this to work with multiple presenting view controllers by creating unique names for the IBActions.
More information on first responder and the responder chain
See my answer here. It gives you two ways to dismiss the modal view controller with storyboard. I like method two described because one you add the class in your project your return from modal views can be done with no code using storyboard alone. That said, if you have implemented a delegate and delegate protocol, it is also a good place to put the dismissModalViewController statement.
To do this inside the UIStoryboard you need first to create an Object of the type UIStoryboardSegue in your project
Then insert following method inside the class. Here is my class
#implementation DismissController
- (void)perform{
UIViewController *sourceVC = self.sourceViewController;
[sourceVC.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
Now you can use it inside your UIStoryboard. Select the button that should make the UIViewController Disappear and drag it to the UIViewController you want to go to. In my case it shows **dismiss Controller* because of the name of my Class.
Select it and you are done!
There is also a very good explanation on this website.
As the Apple online documentation indicates, the presenting view controller is responsible for dismissing the modal (presented) view.
There's a post and example available
here

Resources