简体   繁体   English

适用于通用(iPhone / iPad)应用的iOS控制器

[英]iOS Controllers for universal (iPhone/iPad) app

I am just working on iOS app and I want to make it universal for both iPhones and iPads. 我只是在开发iOS应用,我想使其在iPhone和iPad上都通用。 This is done and works without any problems: 这已经完成并且可以正常工作:

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; 

There is a view for each controller (ViewController_iPad.xib, ViewController_iPhone.xib). 每个控制器都有一个视图(ViewController_iPad.xib,ViewController_iPhone.xib)。 It doesn't matter which view is loaded in my problem. 在我的问题中加载哪个视图都没关系。 In a view there is a subview added (UIScrolView). 在视图中,添加了一个子视图(UIScrolView)。 And in this ScrollView there are two views from xib: 在此ScrollView中,有xib的两个视图:

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

(This code is in iPad/iPhone controller). (此代码在iPad / iPhone控制器中)。 Still everything is OK. 一切都还好。 But I don't know how to set owners (in IB) of these subviews that are shown in ScrollView. 但是我不知道如何设置ScrollView中显示的这些子视图的所有者(在IB中)。 These subviews are in ScrollView which is in a main view so I want to set owners of these subviews as iPad/iPhone controller. 这些子视图位于主视图中的ScrollView中,因此我想将这些子视图的所有者设置为iPad / iPhone控制器。 But as a owner can be only one class. 但是作为所有者只能是一个阶级。 Can you tell me how to set owners if I have two main controllers and I don't know which one will be loaded in runtime. 如果我有两个主控制器并且我不知道在运行时将加载哪个主控制器,您能告诉我如何设置所有者。 Thank you. 谢谢。

EDIT: I have another question: I have ViewController_iPhone. 编辑:我还有另一个问题:我有ViewController_iPhone。 It has a View property and this property is assigned to the "root" view in the main view in ViewController_iPhone (.xib). 它具有View属性,并且此属性已分配给ViewController_iPhone(.xib)主视图中的“根”视图。 Can I assign this view property also to subview view? 是否可以将此视图属性也分配给子视图视图? Because I got EXC_BAD_ACCESS error if I assign view property of ViewController_iPhone to a "root" view of subview in IB. 因为如果将ViewController_iPhone的view属性分配给IB中子视图的“根”视图,则会出现EXC_BAD_ACCESS错误。

Looks like you need to use a class cluster. 看起来您需要使用类集群。 This will abstract the iPhone/iPad instantiation, so you don't explicitly need to instantiate one of the two. 这将抽象化iPhone / iPad的实例化,因此您无需明确地实例化这两个实例之一。 You can read a bit about class clustering in the Apple documentation: 您可以在Apple文档中了解有关类集群的知识:

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

It boils down to creating a master view controller which will handle the allocation of iPhone or iPad subclasses based on the current device. 归结为创建一个主视图控制器,该控制器将根据当前设备处理iPhone或iPad子类的分配。

You should override the ViewController alloc class method: 您应该重写ViewController分配类方法:

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

This way you can just allocate the ViewController class and at runtime the correct class definition will be used to instantiate your view controller. 这样,您可以只分配ViewController类,并且在运行时将使用正确的类定义来实例化您的视图控制器。 This will allow you to use the ViewController class as the owner in IB provided that you create an abstraction of iPhone and iPad interfaces and define it in their super class. 这将允许您使用ViewController类作为IB中的所有者,前提是您创建了iPhone和iPad接口的抽象并在其超类中定义了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM