Problems accessing rootviewController of navcontroller iphone

梦想的初衷 提交于 2019-12-24 03:00:56

问题


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?


回答1:


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];
....



回答2:


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


来源:https://stackoverflow.com/questions/2185466/problems-accessing-rootviewcontroller-of-navcontroller-iphone

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