特定于设备的加载类别

发布于 2024-12-07 17:17:54 字数 144 浏览 1 评论 0原文

我有一个 iPhone 应用程序,我想使其通用,大多数视图可以保持不变,但需要针对 iPad 进行一些小的修改。

是否可以根据用户使用的设备加载类别?

或者有更好的方法吗?通用方法(而不是每次创建类的新实例时专门检查,并在两个类之间进行选择)

I have an iPhone app that I'd like to make universal, most views can be kept the same, but there are some minor modifications that need to be made for the iPad.

Is it possible to load a category depending on what device the user is using?

Or is there a better way of doing this? A generic way (rather than specifically checking each time I create a new instance of a class, and choosing between 2 classes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甩你一脸翔 2024-12-14 17:17:54

您可以在运行时通过一些方法混合来做到这一点。举一个简单的例子,如果您想在 UIView 子类中拥有一个与设备相关的 drawRect: 方法,您可以编写两个方法,并决定在该类被调用时使用哪个方法。已初始化:

#import <objc/runtime.h>

+ (void)initialize
{
    Class c = self;
    SEL originalSelector = @selector(drawRect:);
    SEL newSelector = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
                      ? @selector(drawRect_iPad:) 
                      : @selector(drawRect_iPhone:);
    Method origMethod = class_getInstanceMethod(c, originalSelector);
    Method newMethod = class_getInstanceMethod(c, newSelector);
    if (class_addMethod(c, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
        class_replaceMethod(c, newSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, newMethod);
    }
}

- (void)drawRect_iPhone:(CGRect)rect
{
    [[UIColor greenColor] set];
    UIRectFill(self.bounds);
}

- (void)drawRect_iPad:(CGRect)rect
{
    [[UIColor redColor] set];
    UIRectFill(self.bounds);
}

- (void)drawRect:(CGRect)rect
{
    //won't be used
}

这会导致 iPad 上显示红色视图,iPhone 上显示绿色视图。

You could do this with some method swizzling at runtime. As a simple example, if you want to have a device-dependent drawRect: method in your UIView subclass, you could write two methods and decide which to use when the class is initialized:

#import <objc/runtime.h>

+ (void)initialize
{
    Class c = self;
    SEL originalSelector = @selector(drawRect:);
    SEL newSelector = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
                      ? @selector(drawRect_iPad:) 
                      : @selector(drawRect_iPhone:);
    Method origMethod = class_getInstanceMethod(c, originalSelector);
    Method newMethod = class_getInstanceMethod(c, newSelector);
    if (class_addMethod(c, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
        class_replaceMethod(c, newSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, newMethod);
    }
}

- (void)drawRect_iPhone:(CGRect)rect
{
    [[UIColor greenColor] set];
    UIRectFill(self.bounds);
}

- (void)drawRect_iPad:(CGRect)rect
{
    [[UIColor redColor] set];
    UIRectFill(self.bounds);
}

- (void)drawRect:(CGRect)rect
{
    //won't be used
}

This should result in a red view on the iPad and a green view on the iPhone.

恍梦境° 2024-12-14 17:17:54

查看 UI_USER_INTERFACE_IDIOM() 宏,这将允许您根据设备类型分支代码。

如果您只想保留每个文件 iPhone 或 iPad,您可能必须创建一个辅助类或抽象超类来返回适当的实例。

Look into the UI_USER_INTERFACE_IDIOM() macro, this will allow you to branch your code based on the device type.

You may have to create a helper class or abstract superclass which returns the appropriate instance if you want to keep each file iPhone or iPad only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文