How to Get the Rootviewcontroller from a Pushed Controller

How do I get the RootViewController from a pushed controller?

Use the viewControllers property of the UINavigationController. Example code:

// Inside another ViewController
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];

This is the standard way of getting the "back" view controller. The reason objectAtIndex:0 works is because the view controller you're trying to access is also the root one, if you were deeper in the navigation, the back view would not be the same as the root view.

How to get root view controller?

if you are trying to access the rootViewController you set in your appDelegate. try this:

Objective-C

YourViewController *rootController = (YourViewController*)[[(YourAppDelegate*)
[[UIApplication sharedApplication]delegate] window] rootViewController];

Swift

let appDelegate  = UIApplication.sharedApplication().delegate as AppDelegate
let viewController = appDelegate.window!.rootViewController as YourViewController

Swift 3

let appDelegate  = UIApplication.shared.delegate as! AppDelegate
let viewController = appDelegate.window!.rootViewController as! YourViewController

Swift 4 & 4.2

let viewController = UIApplication.shared.keyWindow!.rootViewController as! YourViewController

Swift 5 & 5.1 & 5.2

let viewController = UIApplication.shared.windows.first!.rootViewController as! YourViewController

how to set pushed view as root view controller using transition

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("drawer")
appDelegate.navigationController = UINavigationController.init(rootViewController:initialViewController )
appDelegate.navigationController?.setViewControllers([initialViewController], animated: false)
appDelegate.window?.rootViewController = appDelegate.navigationController
appDelegate.window?.makeKeyAndVisible()

How to pop back to root view controller but then push to a different view?

An easy way to accomplish what you want to do is to build some simple logic into your main root view controllers -(void)viewWillAppear method and use a delegate callback to flip the logic switch. basically a "back reference" to the root controller. here is a quick example.

main root controller (consider this controller a) - well call it controllerA
set a property to keep track of the jump status

@property (nonatomic) BOOL jumpNeeded;

setup some logic in

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.jumpNeeded ? NSLog(@"jump needed") : NSLog(@"no jump needed");

if (self.jumpNeeded) {
NSLog(@"jumping");
self.jumpNeeded = NO;
[self performSegueWithIdentifier:@"controllerC" sender:self];
}
}

Now, in your main root controller,when a tableview row is selected do something like this
when pushing to controllerB in your tableView did select method

[self performSegueWithIdentifer@"controllerB" sender:self];

then implement your prepare for segue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

//setup controller B
if([segue.identifier isEqualTo:@"controllerB"]){
ControllerB *b = segue.destinationViewController;
b.delegate = self; //note this is the back reference
}

//implement controller c here if needed
}

Now move on to controllerB
you need to set a property called "delegate" to hold the back reference
and you need to import the header file from the root controller

#import "controllerA"

@property (nonatomic,weak) controllerA *delegate;

then just before you pop back to controllerA, you set the flag

   self.delegate.jumpNeeded = YES;
[self.navigationController popViewControllerAnimated:YES];

and that is about it. You don't have to do anything with controllerC. There are a few other ways to do, but this is pretty straight forward for your needs. hope it works out for you.

Back to root view controller in swift

You can just set a new RootController like this:

let storyboard = UIStoryboard(name: "sName", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<YOUR ROOT CONTROLLER>")
self.window?.rootViewController = viewController

If you don't have a window at self.window you need to instanciate one based on your AppDelegate.

If you're within a NavigationController you can also use the answer of @Anshul Bhatheja

How to check if navigation controller is pushed or is a root view controller?

[self.navigationController viewControllers];

will return an array of all the viewControllers on the stack. Simply compare the first element in this array to see is the controller the root or not.

e.g.

UIViewController *vc = [[self.navigationController viewControllers] firstObject];

if([vc isEqual: <viewController to check> ])
{
// code here
}

EDIT: Add Swift

let vc = self.navigationController?.viewControllers.first
if vc == self.navigationController?.visibleViewController {
//Code Here
}

How to push Viewcontroller from rootViewController in iOS

You not setting UINavigationViewcontroller at the didFinishLaunchingWithOptions of Window's RootViewcontroller, We can't Push a UIViewController without UINavigationViewcontroller

So First Set LoginViewController as a RootViewController of UINavigationViewcontroller like Bellow:-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

loginViewControl = [[YourLoinViewcontroller alloc] initWithNibName:@"YourLoinViewcontroller" bundle:nil];
UINavigationController *objNavigationController=[[UINavigationController alloc]initWithRootViewController:loginViewControl];

self.window.rootViewController = objNavigationController;
[self.window makeKeyAndVisible];

return YES;
}

EDIT

Connect your StatusViewcontroller's view with File owner like bellow:-

enter image description here

your push method like this:-

- (IBAction)statusButtonClick:(id)sender;
{
StatusViewController *statusView = [[StatusViewController alloc]initWithNibName:@"StatusViewController" bundle:nil];
[self.navigationController pushViewController:statusView animated:YES];

}

how to get navigation controller from NSObject class and push a view controller in ios

It isn't entirely clear exactly what you're trying to do, so arguably you should be passing a controller to use for presentation or passing a controller back to something else who knows how to present it...

Anyway, I guess your root VC is a navigation controller so you should be using that directly instead of asking it for its navigation controller (which is nil).

UINavigationController *navController = (UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
[navController pushViewController:pushtoVC animated:YES];


Related Topics



Leave a reply



Submit