iPhone/iPad 的 UIViewController 问题
我有一个带有一个 MainWindow.xib 文件的应用程序。然后我有 ViewControllerA.xib 和 ViewControllerB.xib。我的 MainWindow.xib 有一个 ViewController 指向两个 ViewControllerA.xib。
在 ViewControllerA 上,我有一个按钮,我希望按下该按钮时将 ViewControllerB 移动到屏幕上。我该怎么做?
我尝试了这段代码,但我认为我遗漏了一些东西:
- (IBAction)btMyButton:(id)sender
{
ViewControllerB * viewController = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];
[[UIApplication sharedApplication].keyWindow addSubview:viewController.view];
[self.navigationController pushViewController:viewController animated:YES];
}
我的 ViewControllerB 确实出现了,但它被挤压在屏幕顶部,位于前一个视图之上。非常感谢任何帮助。
谢谢
I have an App with one MainWindow.xib file. Then I have ViewControllerA.xib and ViewControllerB.xib. My MainWindow.xib have one ViewController that points two ViewControllerA.xib.
On ViewControllerA I have a button and I would like the button, when pressed, to move ViewControllerB onto the screen. How do I do that?
I tried this code, but I think I am missing something:
- (IBAction)btMyButton:(id)sender
{
ViewControllerB * viewController = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];
[[UIApplication sharedApplication].keyWindow addSubview:viewController.view];
[self.navigationController pushViewController:viewController animated:YES];
}
My ViewControllerB does appear, but it is squashed at the top of the screen, over the previous view. Any help is greatly appreciated.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除第二行:
您将添加视图两次。第二行和第三行都导致视图添加到视图层次结构的不同位置。
- 更新 -
如果您删除第二行并且看不到您的视图,那么 self.navigationController 很可能为零。尝试使用 [selfpresentModalViewController:] 来代替。
Remove the second line:
You are adding the view twice. The 2nd and 3rd lines both cause the view to be added to the view hierarchy in different places.
--update--
If you remove the 2nd line and are not seeing your view then self.navigationController is most likely nil. Try [self presentModalViewController:] instead.
确保控制器 B 的视图具有正确的高度,您还应该减去导航栏高度 44 px 和状态栏高度 20 px。这些值适用于 iPhone。
更新
正如上面的评论所说,如果您可能没有导航控制器来将控制器 B 推入其中。因此,在主窗口中添加一个导航控制器而不是视图控制器,并将其根视图控制器设置为控制器 A。
我希望这对您有帮助,
Make sure that the view of controller B has correct hight you should subtract the navigation bar height 44 px and status bar 20 px as well. Those values are for iPhone.
Update
As the above comment says if you may don't have a navigation controller to push Controller B into. So Add a navigation controller into the main window instead of the view controller and make its root view controller you Controller A.
I hope this helps you,
您将需要从主窗口中删除旧的视图控制器。
you will need to remove old viewcontroller from main windows.