xcode ios4 - 单击按钮加载新视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5674112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:50:14  来源:igfitidea点击:

ios4 - Load new view on clicking a button

xcodeios4

提问by Angelo

I am currently using XCode 3.2.3 and iOS4. I'm working on an app that simply starts with one screen and on a button click moves to the next.

我目前正在使用 XCode 3.2.3 和 iOS4。我正在开发一个应用程序,它只是从一个屏幕开始,然后单击按钮移动到下一个屏幕。

I have gone thorugh the ViewController programming guide and a post here.

我已经阅读了 ViewController 编程指南和此处的帖子。

What I am doing is very similar to whats happening on the post. So let me explain the steps, I followed:

我正在做的事情与帖子上发生的事情非常相似。所以让我解释一下步骤,我遵循了:

  1. In IB, drag and drop, a View from the library into the editor. I renamed the new UIView to myView.

  2. In my AppControllerDelegate, I added the new view "myView" as a property of the view controller (File's Owner). I synthesized it as well in the implementation.

  3. Now, in the implementation of the ViewController, within the button pressed action handler, I wrote the following lines of code:

  1. 在 IB 中,从库中拖放一个 View 到编辑器中。我将新的 UIView 重命名为 myView。

  2. 在我的 AppControllerDelegate 中,我添加了新视图“myView”作为视图控制器(文件的所有者)的属性。我在实现中也综合了它。

  3. 现在,在 ViewController 的实现中,在按钮按下动作处理程序中,我编写了以下代码行:

[self.view addSubView: myView];

[self.view addSubView: myView];

  1. On clicking the button however, I do not see a new screen or my new view. However if I do this, I get a new screen or new view:
  1. 但是,在单击按钮时,我没有看到新屏幕或新视图。但是,如果我这样做,我会得到一个新屏幕或新视图:

UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame]; [self.view addSubView: anotherView];

UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame]; [self.view addSubView: anotherView];

I do know that the best way is to do it with separate NIBs for each UIView. However, I am new to iPhone development and have not explored that path as yet.

我知道最好的方法是为每个 UIView 使用单独的 NIB。但是,我是 iPhone 开发的新手,还没有探索过这条路。

My question: What am I missing upto step 3?

我的问题:我在第 3 步之前遗漏了什么?

采纳答案by learner2010

One way you could be able to do it is by trying it this way

你可以做到的一种方法是通过这种方式尝试

myView = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle: [NSBundle mainBundle]]; 
[self.view addSubview: myView.view];

Try that and see if it is working.. if yours is a view based project then instead of [self.view addSubview: myView.view], just give [self.view addSubview : myView];

试试看它是否有效..如果你的项目是基于视图的项目,而不是 [self.view addSubview: myView.view],只需给出 [self.view addSubview : myView];