我是iOS开发的新手,需要一些帮助。我搜索了高低,找不到答案。这更像是我不明白发生了什么。我的问题是,我正在尝试在不使用故事板的情况下编写Master-Detail应用程序。这有助于我更好地理解ViewControllers的管理和处理方式。我试图在iPhone上的MasterViewController中的tableView中选择一个项目后呈现DetailViewController。在iPad上它运作良好,因为两个视图是并排的。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Pass data to detailViewController
if ([_detailDelegate respondsToSelector:@selector(item:selectedAtIndexPath:)]) {
[_detailDelegate item:[[BNRItemStore sharedStore] findItemAtIndex:indexPath.row] selectedAtIndexPath:indexPath];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
// show DetailViewController
NSArray *viewControllers = self.splitViewController.viewControllers;
UINavigationController *detailNavigationViewController = [viewControllers objectAtIndex:1];
[self showDetailViewController:detailNavigationViewController sender:nil];
}
}
在故事板中,所选的ViewCell与detailViewController具有show detail segue
。如何在不使用故事板的情况下实现同样的目标?
我的AppDelegate代码可以获得更好的图片
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
self.window.rootViewController = splitViewController;
UINavigationController *masterNavigationController = [[UINavigationController alloc]init];
UINavigationController *detailNavigationController = [[UINavigationController alloc]init];
MasterTableViewController *masterTableViewController = [[MasterTableViewController alloc] init];
DetailViewController *detailViewController = [[DetailViewController alloc] init];
masterTableViewController.detailDelegate = detailViewController;
detailViewController.masterDelegate = masterTableViewController;
masterNavigationController.viewControllers = @[masterTableViewController];
detailNavigationController.viewControllers = @[detailViewController];
[splitViewController setViewControllers:@[masterNavigationController,detailNavigationController]];
[self.window makeKeyAndVisible];
return YES;
}
感谢。
答案 0 :(得分:1)
尝试更改:
[self showDetailViewController:detailNavigationViewController sender:nil];
为:
[self.splitViewController showDetailViewController:detailNavigationViewController sender:nil];