Unwind a Modal UINavigationController to another ViewController - ios

I am trying to segue back to another UIViewController after modally presenting a UINavigationController and with it a root ViewController. What I want to happen is for a button to unwind from the root VC of the UINavigationController and return to the initial UIViewController that presented it. I have the unwind method written in the destination VC and button is hooked up to the Exit point of the VC. But when I press the button, nothing happens. Can someone explain what I am doing wrong? Any advice is appreciated.
Storyboard:
Storyboard
Code:
-(IBAction)unwindToClockView:(UIStoryboardSegue*)sender;
{
NSLog(#"Back to Clock View");
}
Additional code can be found here: https://github.com/xinkecf35/TrafficAlarmClock

Okay, I have a solution. It just involves programmatically calling a method trigger by the button that outright dismisses the view controller. The method is below.
-(IBAction)dismissSettings:(id)sender
{
[self dismissViewControllerAnimated:TRUE completion:nil];
}
If anyone can present a solution that uses Storyboards as well, it will be greatly appreciated.

Related

popViewController not working with storyboards

I create a UINavigationController (as a initial screen) and connect them with my UITableViewController (as a root).
Next, I create another UINavigationController and connect with my UIViewController. Inside my UITableViewController I connect my first cell with UINavigationController (That was connected with my UIViewController) (Segue -> show).
When I run the project my table appears, when I select my first row, my UIViewController appears. Thats great!
When I was in my UIViewController the back bar button doesn't appears, in my case I create a left bar button, that will run a function to dismiss that view and go back to my UITableViewController, for that I use many codes:
-(void)back{
NSLog(#"Called");
[self.parentViewController.navigationController popViewControllerAnimated:YES];
}
-(void)back{
NSLog(#"Called");
[self.navigationController popViewControllerAnimated:YES];
}
-(void)back{
NSLog(#"Called");
[self.navigationController popViewControllerAnimated:YES];
}
But all of they doesn't works and my view don't dismiss, how can I solve this problem?
The problem is that your navigation controller, that you call from your table view cell, has only a single view controller (yours) on its navigation stack. So it cannot pop anything from this stack, else the stack were empty.
What you have to do instead is to dismiss the navigation controller itself.
I think that the solution would be to remove the second navigation controller. Since the TableView is already embedded inside a Navigation Controller, the show segue to the UIViewController must be directly connected to it.

segue from second view controller to first view controller ios8

I have 4 ViewControllers. Three of those view controllers are connected to the fourth one, which I call Photo_Detail. The segues there work fine, but now I have to make the back button go to the VC that someone came from. How can I do that? A research said to create a unwind segue in the Photo_detail, what i did but it didn't work out. That's the code I tried to use:
- (IBAction)done:(UIStoryboardSegue *)segue {
NSLog(#"Popping back to this view controller!");
// reset UI elements etc here
}
Thanks for any help in advance!
- (IBAction)done:(UIStoryboardSegue *)segue {
NSLog(#"Popping back to this view controller!");
// reset UI elements etc here
[self dismissViewControllerAnimated:NO completion:nil];
}
For Modal type segues, this will help you to move back.
And also select it as normal action from ViewController rather then exit. That will fix it.
Hope this helps.
If the second VC is presented modally, you simply have to dismiss the controller
[self dismissViewControllerAnimated:NO completion:nil]:

Redirect to new view controller in the middle of navigation stack

I am fairly new to navigation view controllers and stacks in iOS. Please help me with this, tried searching for a long time couldn't find anything relevant.
I have a LoginViewController, a SWRevealViewController for side menu operation, a FirstViewController (the first one in navigation stack) and a SecondViewController (second in navigation stack).
I have to do the following in LoginViewController, assume login and found are two boolean variables:
if (login && !found) {
//redirect to FirstViewController : the following works
[self presentViewController:swReveal animated:NO completion:nil];
}
else if (login && found) {
//redirect to SecondViewController
}
Please help me fill the else if. Really appreciate your time!
So based on what I understood from your comments, FirstViewController is a subclass of TableViewController and SecondViewController is the DetailView.
So based on that, the best approach for "redirecting" to the DetailView (SecondViewController), would be creating a segue on the Storyboard, and then calling that segue programatically once the FirstViewController is loaded (in its viewDidLoad)
On both cases you would first show the FirstViewController and then depending on the if statement, you would either stay or go to the SecondViewController.
So first step: Create the segue from the Storyboard, and give it an identifier.
Second: Perform that segue programatically when the FirstViewController is loaded (and of course, the if statement is valid) by using:
[self performSegueWithIdentifier: #"MySegue" sender: self];
More on that subject:
Alternative ways to push view controllers with storyboard programmatically
Creating a segue programmatically

How to dismiss UIPageViewController?

I added a UIPageViewController for instructions in my new game, and I want to dismiss the UIPageViewController after the user pressed the "GOT IT" button on the last UIViewController that connect to the UIPageViewController
I've tried this so far:
[self.pageVC dismissViewControllerAnimated:YES completion:nil];
And it doesn't remove the UIPageViewController, Why that?
And how can I dismiss it?
In order to show/present the UIPageViewController I do this:
[self addChildViewController:self.pageVC];
[self.view addSubview:self.pageVC.view];
[self.pageVC didMoveToParentViewController:self];
Please try the opposite calls you have described:
[self.pageVC.view removeFromSuperview];
[self.pageVC removeFromParentViewController];
I had an issue with the solution I found here and in other posts, because I called dismissViewController in one of the children views of the pageViewController, so I didn't dismiss the pageViewController itself, just the shown children view.
Turns out, I found a quick way to dismiss the pageViewController from one of its subViews by pressing a button: just connect this button to the Exit button in the storyboard to unwind (and provide the unwind method in the presenting vc).
If you want to dismiss the UIPageViewController from a button from within one of the pages, you could add the below code, in the button action. This is similar to the answer given by Alex Peda, the difference being the reference to the pageViewController is through the parentViewController property.
let pvc = self.parentViewController as! UIPageViewController
//remove the page controller content view from the superview.
pvc.view.removeFromSuperview()
//remove the pageviewcontroller from its parent view controller.
pvc.removeFromParentViewController()
Swift 4 / XCode 9:
While all the proposed answers didn't work for me, this simple line did work:
navigationController?.popViewController(animated: true)

iOS: Loading UIViewController with a popover segue, trying to dismiss itself

I have a UIViewController instance segued to by way of a popover segue. It is NOT a UIPopOverViewCOntroller so there's no dismissPopoverAnimated: method.
I have a button in the its main UIView that I want to dismiss the VC. How can this be done?
I have this, which does not work:
- (IBAction)submitButtonClicked:(id)sender
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
That is the correct code usually if you aren't going to do anything in the completion handler just set it to nil. Did you create this button in the storyboard and if so did you link it to this action?
If you set a breakpoint on that line of code does it actually run when you click the button? If not then your button isn't linked to the IBAction.

Resources