在 XCode 4 中布局通用应用程序
我正在尝试在 Xcode 4 中执行创建通用(iphone/ipad)应用程序的基本步骤。如果可能的话,我不想使用 INterface Builder,并且我想摆脱繁琐的 if 语句:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
我创建一个“基于窗口的项目”来获取Apple提供的通用模板。这给了我三个应用程序代表,其中 iPhone 和 iPad 在其特定文件夹内有自己的 XIB。
我现在想添加一个视图控制器,因此我创建一个主“MasterViewController”,以及两个子类版本“MasterViewController_iPhone”和“MasterViewController_iPad”。我不会使用其中任何一个创建 XIB。 (我需要吗?)
这就是我感到困惑的地方。在主应用程序委托中,我放置了
[self.window makeKeyAndVisible];
不添加任何其他代码的代码,允许子类应用程序委托实例化它们各自的视图控制器。例如,在 iphone 应用程序委托中,我放置:
myVC_iPhone = [[MasterViewController_iPhone alloc] initWithNibName:nil bundle:nil];
[self.window addSubview: myVC_iPhone];
但这给了我错误:
Incompatible pointer types sending 'MasterViewController_iPhone *' to parameter of type 'UIView *'
我在这里缺少一些基本的东西。也许我的命名约定是错误的;也许 makeKeyAndVisible 不应该位于主应用程序委托中;也许我需要每个视图控制器的 XIB。有人能在这里推动我吗?谢谢!
I'm trying to do the basics steps of creating a universal (iphone/ipad) app in Xcode 4. I don't want to be using INterface Builder, if possible, and I want to get away from the cumbersome if statements:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
I create a "window-based project" to get the Universal template that Apple provides. This gives me the three app delegates, with the iPhone and iPad having their own XIBs, inside their specific folders.
I would now like to add a view controller, so I create a master "MasterViewController", and two subclassed versions "MasterViewController_iPhone" and "MasterViewController_iPad". I do not create XIBs with any of these. (Do I need to?)
Here is where I get confused. In the master app delegate, I place the code
[self.window makeKeyAndVisible];
I don't add any other code, allowing the sub-classed app delegates to instantiate their respective view controllers. For example, in the iphone app delegate I place:
myVC_iPhone = [[MasterViewController_iPhone alloc] initWithNibName:nil bundle:nil];
[self.window addSubview: myVC_iPhone];
But this gives me the error:
Incompatible pointer types sending 'MasterViewController_iPhone *' to parameter of type 'UIView *'
I'm missing something basic here. Perhaps my naming conventions are wrong; perhaps makeKeyAndVisible shouldn't be in the master app delegate; perhaps I need XIBs for each view controller. Can anyone push me along here? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
addSubview:
采用 UIView 作为参数,而不是它周围的视图控制器。您应该可以使用[self.window addSubview: myVC_iPhone.view];
。addSubview:
takes an UIView as parameter not the view controller surrounding it. You should be fine using[self.window addSubview: myVC_iPhone.view];
.将视图控制器添加为 rootViewController 怎么样?
How about adding your view controller as the
rootViewController