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 reloa开发者_如何学GodData];
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?
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];
....
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
精彩评论