适用于通用 (iPhone/iPad) 应用程序的 iOS Controller

标签 ios view controller subview owner

我正在开发 iOS 应用程序,我想让它在 iPhone 和 iPad 上通用。这已经完成并且没有任何问题:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.viewController_iPhone = [[ViewController_iPhone alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
    self.viewController_iPad = [[ViewController_iPad alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}

if (self.viewController_iPhone == nil)
    self.window.rootViewController = self.viewController_iPad;
else
    self.window.rootViewController = self.viewController_iPhone; 

每个 Controller 都有一个 View (ViewController_iPad.xib、ViewController_iPhone.xib)。在我的问题中加载哪个 View 并不重要。在 View 中添加了一个 subview (UIScrolView)。在这个 ScrollView 中有两个来自 xib 的 View :

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"SubView1" owner:self options:nil];
UIView *view = [nibContents objectAtIndex:0];
view.frame = CGRectMake(2, 0, scrollView.frame.size.width - 2, scrollView.frame.size.height);
[scrollView addSubview:view];


nibContents = [[NSBundle mainBundle] loadNibNamed:@"SubView2" owner:self options:nil];
view = [nibContents objectAtIndex:0];
view.frame = CGRectMake(scrollView.frame.size.width + 2 , 0, scrollView.frame.size.width - 4, scrollView.frame.size.height);
[scrollView addSubview:view];

(此代码在 iPad/iPhone Controller 中)。仍然一切正常。但我不知道如何设置 ScrollView 中显示的这些 subview 的所有者(在 IB 中)。这些 subview 位于主视图中的 ScrollView 中,因此我想将这些 subview 的所有者设置为 iPad/iPhone Controller 。但是作为所有者只能是一个类。如果我有两个主 Controller 并且我不知道运行时将加载哪个 Controller ,您能告诉我如何设置所有者吗?谢谢。

编辑:我还有一个问题:我有 ViewController_iPhone。它有一个 View 属性,这个属性被分配给 ViewController_iPhone (.xib) 主视图中的“根” View 。我可以将此 View 属性也分配给 subview 吗?因为如果我将 ViewController_iPhone 的 View 属性分配给 IB 中 subview 的“根” View ,我会收到 EXC_BAD_ACCESS 错误。

最佳答案

看来您需要使用类集群。这将抽象化 iPhone/iPad 实例化,因此您不需要明确地实例化两者之一。 您可以在 Apple 文档中阅读一些关于类聚类的内容:

http://developer.apple.com/library/ios/#documentation/general/Conceptual/DevPedia-CocoaCore/ClassCluster.html

它归结为创建一个主视图 Controller ,它将根据当前设备处理 iPhone 或 iPad 子类的分配。

您应该覆盖 ViewController alloc 类方法:

   + (id)alloc {
       NSString *classString = NSStringFromClass([self class]);

       NSString *append = nil;

       if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
           append = @"_iPhone";
       } else {
           append = @"_iPad";
       }

       NSString *subClassString = [classString stringByAppendingString:append];

       id subClass =  NSClassFromString(subClassString);


       id object;

       if (subClass && ![self isKindOfClass:[subClass class]]) {
           object = [subClass alloc];
       } else {
           object = [super alloc];
       }

       return object;
   }

这样你就可以只分配 ViewController 类,在运行时正确的类定义将用于实例化你的 View Controller 。 这将允许您使用 ViewController 类作为 IB 中的所有者,前提是您创建 iPhone 和 iPad 接口(interface)的抽象并在它们的父类(super class)中定义它。

关于适用于通用 (iPhone/iPad) 应用程序的 iOS Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16686253/

相关文章:

iphone - 为什么我不能将子 Pane plist 文件添加到项目的 Settings.bundle 中?

android - 如何在一个 View 中将 2 个 edittext 和一个按钮分组,并在屏幕上显示该 View ?

ruby-on-rails - 参数变为 nil

ios - 检测 View Controller 何时从弹出窗口出现

ios - 左对齐图像并将文本放置在 UIButton 的中心

css - Default.ctp 和一个完全不同的登录页面

asp.net-mvc - 如何将整数列表传递给 MVC 操作?

ruby-on-rails - 带有 Rails Controller 错误 : undefined method `action' for MessageController:Class 的 websocket

ios - 启用/禁用 UISegmentedControl

android - 使自定义 Android SurfaceView 透明