iPhone/iPad 的 UIViewController 问题

发布于 2024-11-07 18:16:01 字数 668 浏览 0 评论 0原文

我有一个带有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生殊途 2024-11-14 18:16:01

删除第二行:

[[UIApplication sharedApplication].keyWindow addSubview:viewController.view];

您将添加视图两次。第二行和第三行都导致视图添加到视图层次结构的不同位置。

- 更新 -
如果您删除第二行并且看不到您的视图,那么 self.navigationController 很可能为零。尝试使用 [selfpresentModalViewController:] 来代替。

Remove the second line:

[[UIApplication sharedApplication].keyWindow addSubview:viewController.view];

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。

  - (IBAction)btMyButton:(id)sender
    {
        //you should load from the main app bundle
        ViewControllerB * viewController = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:[NSBundle mainBundle]];
        // you don't need the following line      
        //[[UIApplication sharedApplication].keyWindow addSubview:viewController.view];
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }

更新
正如上面的评论所说,如果您可能没有导航控制器来将控制器 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.

  - (IBAction)btMyButton:(id)sender
    {
        //you should load from the main app bundle
        ViewControllerB * viewController = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:[NSBundle mainBundle]];
        // you don't need the following line      
        //[[UIApplication sharedApplication].keyWindow addSubview:viewController.view];
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }

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,

绅刃 2024-11-14 18:16:01

您将需要从主窗口中删除旧的视图控制器。

- (IBAction)btMyButton:(id)sender
{

//Assuming you declare two Iboutlet controllerA, controllerB mapping with that view as class variable
{
if (self.controllerB == nil)
{
    ViewControllerB * viewController = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];
    self.controllerB = viewController;
[viewController release];
}
[controllerA.view removeFromSuperview];
[self.view insertSubview:controllerB.view atIndex:0];
}

}

you will need to remove old viewcontroller from main windows.

- (IBAction)btMyButton:(id)sender
{

//Assuming you declare two Iboutlet controllerA, controllerB mapping with that view as class variable
{
if (self.controllerB == nil)
{
    ViewControllerB * viewController = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];
    self.controllerB = viewController;
[viewController release];
}
[controllerA.view removeFromSuperview];
[self.view insertSubview:controllerB.view atIndex:0];
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文