【问题标题】:how to load different xibs in iphone and ipad?如何在 iphone 和 ipad 中加载不同的 xib?
【发布时间】:2012-01-10 20:23:02
【问题描述】:

我想在程序的某个点为 iphone 和 ipad 加载不同的 xib,但想重用大部分代码。我怎样才能做到这一点?

【问题讨论】:

    标签: iphone ios ipad xcode4


    【解决方案1】:

    ~ipad~iphone 附加到文件名的末尾。比如SomeViewController~ipad.xibSomeViewController~iphone.xib

    iOS Supports Device-Specific Resources

    【讨论】:

      【解决方案2】:

      如果您创建一个通用的模板项目,您可以检查为该版本创建的代码。例如:

      - (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;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        相关资源
        最近更新 更多