【发布时间】:2012-01-10 20:23:02
【问题描述】:
我想在程序的某个点为 iphone 和 ipad 加载不同的 xib,但想重用大部分代码。我怎样才能做到这一点?
【问题讨论】:
我想在程序的某个点为 iphone 和 ipad 加载不同的 xib,但想重用大部分代码。我怎样才能做到这一点?
【问题讨论】:
将~ipad 或~iphone 附加到文件名的末尾。比如SomeViewController~ipad.xib 或SomeViewController~iphone.xib。
【讨论】:
如果您创建一个通用的模板项目,您可以检查为该版本创建的代码。例如:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController_iPhone" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
} else { // iPad
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController_iPad" bundle:nil];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
PSGameViewController *detailViewController = [[PSGameViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
}
[self.window makeKeyAndVisible];
return YES;
}
或来自tableView:didSelectRowAtIndexPath:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (self.detailViewController == nil) {
self.detailViewController = [[PSGameViewController alloc] initWithNibName:@"PSGameViewController_iPhone" bundle:nil];
}
self.detailViewController.model = selectedModel;
[self.navigationController pushViewController:self.detailViewController animated:YES];
self.pushedIndexPath = indexPath;
} else { // iPad
self.detailViewController.model = selectedModel;
}
【讨论】: