dubbeat
dubbeat

Reputation: 7847

Problems accessing rootviewController of navcontroller iphone

Im working off the seismic xml iphone example. In that example there is simply the application delegate which has a rootViewController of type UITableViewController.

I wanted to modify it slightly so that I would have a navigationController and use its rootViewController as the table view. My root view controller class for my navigation controller is named "firstLevelViewController"

In the sample code it says

 [rootViewController.tableView reloadData];

I want to say [navController.firstLevelViewController.tableView reloadData]

but I get errors saying "request for member firstLevelViewController in something is not a structure or union"

How can I reference the rootViewController of the navigationcontroller from the main app delegate?

Upvotes: 2

Views: 3834

Answers (2)

Nishant
Nishant

Reputation: 12617

Access the rootViewController like 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

Upvotes: 0

kennytm
kennytm

Reputation: 523154

There isn't a firstLevelViewController property in a UINavigationController. If you want to access the current controller you can use

UITableViewController* firstLevelViewController = navController.topViewController;
assert([firstLevelViewController isKindOfClass:[UITableViewController class]]);
[firstLevelViewController.tableView reloadData];

If firstLevelViewController means the view controller at the bottom you can use

UITableViewController* firstLevelViewController = [navController.viewControllers objectAtIndex:0];
....

Upvotes: 3

Related Questions