Replacing a modal view - ios

Setting:
The first view controller for my app is PostsViewController. In viewDidLoad I check if the user is logged in, and if not I preset SignupVC modally. There is an option to login instead of signup of course, and I want to replace the signup VC with a login VC.
I tried making a "show detail (replace)" segue from signup to login VC, but that seemed to present login modal on top of signup VC. The problem with it is after the user is logged in, I have two modals to close, and it seems tricky to close both of them at the same time. Ideally, I want to replace the signup VC with the login VC and only close one modal.
Q1. How come the show detail segue works like present modally segue when applied to a modal view controller?
Q2. How can I replace the SignUp View controller with a login View controller?

Right so after rereading your question and your comment it's clear what you're trying to do here.
Q1: Because you can only present a viewController modally on parent / child views that are modally presented. You cannot push a new viewController onto a modally presented viewController. In order to push a viewController you need a UINavigationController in the view hierarchy (Normally the parent / root view)
Q2: You'll need to present it modally from the signupVC.
Example:
//Somewhere in SingupVC
[self presentViewController:LoginVC animated:YES completion:nil];
Edit
To answer your question in the comments:
No there really isn't a way to dismiss both VC's at the same time.
However there are a couple of ways to do it almost:
You can set a boolean flag on SingupVC that LoginVC was presented. That way, when viewWill/Did appear is called on singupVC and the boolean flag is set to YES you can call: [self dismissViewControllerAnimated:NO completion:nil];
And that way both VCs are dismissed. One when the user dismisses LoginVC via the back button and SingupVC via a if statement in one of the view life cycle methods (if loginVCPresent) dismiss view, type of thing.

Related

Dismiss Multiple Pushed View Controllers

I'm developing an app where the user can buy some food on the app and get delivered at home.
At some point the user's got some products on his cart (displayed in a tab view controller) and he goes to confirm his address and payment method (these are made in two different pushed view controllers). And when the purchase is done, the app displays some message informing if the payment was successful or not (also displayed in a pushed view controller after he confirms the ).
So, after the user complete the purchase I want to dismiss all those view controllers from the purchase process and go back to the tab view controller by tap on a button.
How do I dismiss all those pushed view controllers?
Here is a image of the storyboard:
You can use the navigation controller's built in functionality to pop back to the root view controller.
self.navigationController?.popToRootViewControllerAnimated(true)
This will remove all of the pushed view controllers you have on the navigation stack and return to the base controller on the stack. You can attach this functionality to the action selector for when a button is pressed.
Yes you can do that by the following.
Suppose you have a structure like this ---
Navigation Controller --(root)->VCa-->VCb-->VCc-->VCd
Now you are at VCd and you want to dismiss or rather pop to VCa, then simply do this[self.navigationController popToRootViewController]
This will pop or dismiss all VC and return to VCa, as because VCa is the root view controller of the Navigation Controller.
Now if you want to dismiss or Pop to VCb, then do the following
for(UIViewController *vc in self.navigationController.viewControllers){
if([vc isKindOfClass:[VCb class]]){
// this means you have successfully picked VCb among all the vc
// inside of the viewControllers in the Navigation stack.
[self.navigationController popToViewController:vc animated:YES];
}
}
This should do it. Hope this helps.

iOS - navigation controller back button

I'm developing an iPad App using storyboards. In this app when the user click on "preferences" appear a modal View. One button of this view send the user to another View Controller (but this view has an action bar to go back to his root view controller), but when user taps the action bar back button nothing happen (it's called navigationController popViewControllerAnimated), the user continue in the same view.
Can anyone help me??
Thanks.
UPDATE:
The code to handle the back button:
- (IBAction)btnBackTapped:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
I'm using Segue (from storyboard) to call this View Controller:
When the user click on "Meus Favoritos"
They will be redirect to this page:
The segue is with a Modal (from image one to two)...
When you are presenting a View Controller modally, it is likely not within a Navigation Controller, so probably the reference to navigationController in your code is nil, can you check that?
If you are presenting your View Controller modally this will work instead
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
However, if you actually want to use a Navigation Controller, you should embed the View Controller that is presenting the Preferences View Controller in a Navigation Controller and present the Preferences View Controller with a show segue instead of a modal one.

Dismissing a UITabBarVC and returning to the sign in screen

I have a sign in VC that pushes a sign up VC; both are under a Navigation Controller. From the sign up VC I call modaling a UITabBar VC with the whole application content. Inside the UITabBar VC I have another VC with a Sign Out method. My question is: how is the correct way to go back to Sign In VC? In this scenario, if I dismiss the UITabBar VC I return to the sign up VC.
I have drawn a diagram that describes the scenario:
Any help will be appreciated.
Thanks,
Marcos
You can change the state of the UINavigationController which is presenting the modal view before dismissing it. For instance calling
[(UINavigationController *)self.presentingViewController popToRootViewControllerAnimated:NO]
from the modal view controller, will result in your underlying UINavigationController to get back to its root view controller, which - in your specific scenario - will be the Sign In VC, so when you dismiss the modal view, the underlying view controller will be whatever you desire.

How do i avoid IOS storyboard seque loop

My iOS app starts with 2 login screens, then a main "List of accounts" screen. In storyboard I do:
Show "Enter User Reg No" View Controller. (VC1)
Modal Seque to "Enter PAC digits a,b,c". (VC2)
Then Modal Seque to account list. (VC3)
VC3 has a "Logout" button which Modal Seques to VC1.
This is a customer demo prototype. I know it's not correct, as I am building up a loop of VC1,VC2 and VC3s.
What is the correct approach to this? I've read Apple's seque docs, and I still can't find a convincing answer.
The best approach to this would be to use a Navigation Controller with push segues (you can keep the navigation bar hidden if you don't need it), then in your logout button IBAction you just put:
[self.navigationController popToRootViewControllerAnimated:YES];
In order to use a Navigation Controller, you just select your VC1 and then select
Editor->Embed In->Navigation Controller
Edit: I should also probably point out that they're called seGue, with a "g", not seQue!
Don't use any segue to go from a Modal View back to VC1. Just dismiss the modal view:
[self dismissViewControllerAnimated:YES completion:nil];
Usually there's only one modal view. If you must use two, dismiss them both to go back to VC1

presentModalViewController on TabBarController App - blank screen after dismissModalViewController

i am using a tabbarcontroller in my app and also want to use a loginview.
I have an LoginController which handles the loginprocedure.
I am presenting the loginView modally.
[self.tabbarcontroller presentModalViewController:loginView animated:NO];
And on other point i am dismissing it(after login is successfull):
[self dismissModalViewControllerAnimated:YES];
After dismiss i have just a blank screen. Thats the blank window in my MainWindow.xib, i have checked it with a testlabel.
Throubles with that. No ideas how to solve this issue.
Two things:
The self.tabbarcontroller should not be instantiating the modal view. It should be the initial view controller that the tabbar controller controls. In the initialization of that view controller, you check for a login session and present the login VC.
Make sure you are dismissing that modal VC you created in the method that presented it. You should have a delegate method for your Login VC (something like loginFinished) that is handled in the VC that presented it.

Resources