如何将Cocoa应用程序移植到iPhone-OS? [英] How to port a Cocoa app to iPhone-OS?

查看:124
本文介绍了如何将Cocoa应用程序移植到iPhone-OS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个Cocoa应用程序,我想确保有一天我可以轻松地将其移植到iPad或甚至iPhone上。如何提前计划?

I am about to create a Cocoa app and I want to ensure that one day I can easily port it to the iPad or even the iPhone. How can I plan for this in advance?

我知道我必须重做所有NIB,并可能设计不同的工作流程。

I know I will have to redo all NIBs and probably design a different workflow.

但是代码怎么办?只是替换每个NSsomething与UIsomething不会削减它,对吧?

But what about the code? Just replacing every NSsomething with UIsomething won't cut it, right? Any tips on how to make sure now that I won't shoot myself in the foot later?

谢谢!

(iPad-SDK是在NDA下的,为了这个问题,只是假设我问过iPhone,OK?或者认为iPhone有更大的屏幕。)

(The iPad-SDK is under NDA. For the sake of this question just assume i asked about the iPhone, OK? Or think iPhone with a bigger screen.)

推荐答案

与任何好的项目布局一样,您应该将UI从非UI组件中分离出来。这不仅仅意味着在磁盘布局(虽然这是有意义的),而是使用MVC方法,其中你的控制器(C)知道模型(M)和UI(V)是单独渲染。

As with any good project layout, you should separate out your UI from your non-UI components. That doesn't just mean on disk layout (though that makes sense as well) but rather using a MVC approach, whereby your controllers (C) know about the Models (M) and the UI (V) is rendered separately.

您可以使用键值观察(也称为KVO)来设置模型,以便在它们触发时,向任何已注册的侦听器发送通知以进行更新。如果你使用XIB来生成你的用户界面,那么这是当你绑定一个对象到你的显示窗口部件时会发生什么。

You can use Key-Value Observing (aka KVO) to set up your models, such that when they fire, it sends around a notification to any registered listeners to update. If you are using XIB to generate your user interfaces, then this is what happens when you bind an object to your display widget(s).

单独的XIB为您的iPhone,Mac OS和(稍后)iPad - 虽然如果你调整正确的东西,你可以有相同的XIB为iPhone和iPad。

So you can end up with separate XIBs for your iPhone, Mac OS and (later) iPad - though if you resize things correctly, you can have the same XIB for the iPhone and iPad.

经常有一些情况下,你需要注入逻辑到你的模型(例如,添加一个图像从一个方法返回)。在这种情况下,iPhone和Mac OS具有不同的Image类。为此,您可以创建以下内容:

Lastly, there are often some cases where you need to inject logic into your models (for example, adding an image to return from a method). In this case, the iPhone and Mac OS have different Image classes. To do this, you can create the following:

MyModel.m         // contains the data, no UI
MyModel+UIImage.m // contains a category for adding the UIImage
MyModel+NSImage.m // contains a category for adding the NSImage

类别如下所示:

@interface Host(UIImage)
-(UIImage *)badge;
@end

@implementation MyModel(UIImage)
-(UIImage *)badge 
{
    if (green)
        return [UIImage imageNamed:@"green.png"];
    if (red)
        return [UIImage imageNamed:@"red.png"];
}
@end
---
@interface Host(NSImage)
-(NSImage *)badge;
@end

@implementation MyModel(NSImage)

-(NSImage *)badge 
{
    if (green)
        return [[NSImage alloc] initWithContentsOfFile: @"green.png"];
    if (red)
        return [[NSImage alloc] initWithContentsOfFile: @"red.png"];
}
@end

只是加载模型(没有加载任何图像类别),而在运行时,需要处理图像(例如,在视图控制器中)的代码可以加载模型与类别并加载徽章 [模型徽章] 以透明的方式,无论其编译的平台。

This has the added advantage that your unit tests can just load the model (without any image categories being loaded) whereas at runtime your code that needs to process the images (say, in a view controller) can load the model-with-category and load the badge with [model badge] in a transparent way, regardless of which platform it's compiled for.

这篇关于如何将Cocoa应用程序移植到iPhone-OS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆