简体   繁体   English

在iOS中编写通用应用程序

[英]Programming a Universal App in iOS

When I am programming a universal app, lets say I have an IBAction like so: 当我编写通用应用程序时,假设我有这样的IBAction:

(IBAction)magicCode:(id)sender {

    textField1.text = "TEST";

}

Do I need to create a new IBAction for each view (iPad and iPhone). 我是否需要为每个视图(iPad和iPhone)创建新的IBAction。 I can't have textField1 twice in header file, so I am just wondering how everyone else does this. 我不能在头文件中使用textField1两次,所以我只是想知道其他人是如何做到这一点的。 Do I need to put a textfield in the iPhone app with a different name than the one in the iPad app? 我是否需要在iPhone应用程序中放置一个文本字段,其名称与iPad应用程序中的名称不同? Or is there some other way everyone else is doing this? 或者其他人是否有其他方式这样做?

Do I need to create a new IBAction for each view (iPad and iPhone). 我是否需要为每个视图(iPad和iPhone)创建新的IBAction。

First, actions are typically included in view controllers, not views. 首先,操作通常包含在视图控制器中,而不是视图中。 I think that's probably what you meant, but I point out the difference because I've seen a lot of people get confused on this point. 我认为这可能就是你的意思,但我指出了不同之处,因为我看到很多人对这一点感到困惑。

When you're creating a universal app, ie a single app that adapts its UI to the device (iPad or iPhone/iPod Touch) on which it's running, a common strategy is to provide different view layouts that make the best use of the available screen size, but to use the same view controllers. 当您创建一个通用应用程序,即一个应用程序,使其UI适应其运行的设备(iPad或iPhone / iPod Touch)时,一个常见的策略是提供不同的视图布局,以充分利用可用的屏幕大小,但要使用相同的视图控制器。 So, for example, say you have an app with a master/detail interface. 因此,例如,假设您有一个带有主/详细界面的应用程序。 On small devices, you'd present the master part of the interface first, and when the user chooses something you'd display the detail part of the interface. 在小型设备上,您首先会显示界面的主要部分,当用户选择某些内容时,您将显示界面的详细信息部分。 On an iPad, with it's larger screen, you'd display both master and detail interfaces simultaneously in a split view. 在iPad上,屏幕较大,您可以在分屏视图中同时显示主界面和详细界面。 Comparing the two, the views are likely to be different, and the way the view controllers are presented is different, but the view controllers themselves should stay the same. 比较两者,视图可能不同,并且视图控制器的呈现方式不同,但视图控制器本身应保持不变。 This is a good thing, since much of the work of creating an app goes into creating the view controllers. 这是一件好事,因为创建应用程序的大部分工作都是创建视图控制器。

If your app is similar to what I've described (or if you can make it similar), then no, you don't need separate actions for iPad and iPhone because you'll be using the same view controllers in both cases. 如果您的应用程序与我所描述的类似(或者如果您可以使其类似),那么不,您不需要为iPad和iPhone单独执行操作,因为在这两种情况下您将使用相同的视图控制器。 There may be times, though, when the behavior of the app on the two different devices is different enough that it makes sense to have iPad-specific and iPhone-specific view controllers. 但是,有时候,两个不同设备上的应用程序行为不同,以至于拥有特定于iPad和iPhone的视图控制器是有意义的。 You might still be able to use the same actions by deriving each of those from a common parent class that contains the actions. 您可能仍然可以通过从包含操作的公共父类派生每个操作来使用相同的操作。 If not, you'll need to have each class implement its own actions. 如果没有,您需要让每个类实现自己的操作。

No, you can have the same IBAction and IBOutlet in a UIViewController dealing with textfields in two different Nibs (one for iPhone and one for iPad). 不,你可以在UIViewController中使用相同的IBAction和IBOutlet来处理两个不同Nib中的文本字段(一个用于iPhone,一个用于iPad)。 That's the whole point of separation between View Controller and Views in MVC architecture. 这就是MVC架构中View Controller和Views之间的分离点。

Just use the same UIViewController as File's Owner in both the Nibs and make all the appropriate IBOutlet and IBAction connections and everything will work. 只需在两个Nib中使用与文件所有者相同的UIViewController,并进行所有适当的IBOutlet和IBAction连接,一切都会正常工作。

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

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