Return to initial view controller when user logs out

折月煮酒 提交于 2019-11-30 05:09:07

Try this,

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" 
                                                     bundle:nil];
LoginViewController *add = 
           [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];

[self presentViewController:add 
                   animated:YES 
                 completion:nil];

Write Below Method in root viewcontroller

- (IBAction)returnToDashboard:(UIStoryboardSegue *)segue;

Give segue connection to destination view controller like below

Give identifier to segue and assign method to that segue

use below method in destination view controller

 [self performSegueWithIdentifier:@"pushtoDashboard" sender:self];

First of all, I don't think you need that many UINavigationControllers. Using only one in your application should be enough. The reason popToRootViewController is not working in your case is because it will go to the first view controller withing a UINavigationController. You have nested many UINavigationControllers, thus when you click the blue button in the settings view controller it will go to the sidebar view controller (can't read it properly, the image is small).

You can do the following to get to the root view controller of your app:

UINavigationController *rootController =[(UINavigationController*)[(AppDelegate*)
        [[UIApplication sharedApplication]delegate] window] rootViewController]];

Replace AppDelegate with however it's called in your app.

But my advice is to remove all intermediate UINavigationControllers. Then just doing popToRootViewController should do the trick.

Tommie C.

Problem

You'd like to return from a view controller (source) to the start (a destination view controller) when the user clicks the blue button (on the top)

Recommendation

I recommend you take a quick look at the highly rated SO answer which demonstrates how to use the unwind segue (that's the little green exit box on your view controller in the storyboard). Unwind segues are the modern way of accomplishing your goal, but you can also call dismissViewController:animated from the source controller. You should also take a quick read of a very small Apple note (TN2298) on using the unwind segue.

Essentially you will want to add the following method to your destination view controller:

- (IBAction)unwindToMainMenu:(UIStoryboardSegue*)sender
{
}

Then use ctrl+drag and click from the blue button down to the green exit icon on the source view controller. This will popup a menu and you can select unwindToMainMenu from the list. You will need to give the new segue an identifier in the Identity Inspector (e.g. segueToMain).

Manual Unwind

The technical note above (TN2298) will also show you how you can create a manual unwind segues that may be called programmatically (similar to how one might say performSegueWithIdentifier...).

I was working on a very similar problem. I am using a storyboard with a navigation controller & implemented the highly recommended SWRevealViewController, with iOS 7 & Xcode 5.1. I tried unsuccessfully to implement some of the solutions mentions. Either the screen didn't change or I got blank table. I used a hybrid version of the programatic examples provided in SWRevealController & other answers here to get a working solution. I added this as apart of my login button action

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
InboxTableViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:@"inbox"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.revealViewController pushFrontViewController:nav animated:YES];

I retrieved my storyboard & initiated the view controller I wanted from the storyboard & then added to a navigation controller. Finally I used the SWRevealViewController method to Push the view I desired to the front.

I'm using swift and what worked for me was this:

var loginVC: UIViewController? = self.storyboard?.instantiateViewControllerWithIdentifier("UILogin") as?    UIViewController

self.presentViewController(loginVC!, animated: true, completion: nil)

When you have different storyboards simply "presenting" the required VC from the initial storyboard does the trick:

Swift 3

if let loginVC = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController() {
   present(loginVC, animated: true, completion: nil)
}

In some cases there might be leaking UITransitionView's. You might remove them right after the "presenting" code, but not before it and not in it's completion:

if let subviews = UIApplication.shared.keyWindow?.subviews,
    let transitionViewClass = NSClassFromString("UITransitionView") {
    for subview in subviews where subview.isKind(of: transitionViewClass) {
        subview.removeFromSuperview()
    }
}

<– This works as of Xcode 8.2.1 and iOS 10.2 but no guarantee if will work forever. Be careful with it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!