在uiviews与按钮之间切换,而不是uinavigation控制器 [英] switch between uiviews with buttons and not uinavigation controllers

查看:155
本文介绍了在uiviews与按钮之间切换,而不是uinavigation控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了如何通过按钮切换视图的帖子iPhone?,但这不回答如何使用按钮在视图之间来回切换。询问问题的人解决了他们可以使用uinavigation控制器在视图之间切换的答案。

I have seen the post for How to switch views by buttons on iPhone? but this doesn't answer how to switch back and forth between views with buttons. The person that asked the question settled on the answer that they could switch between views with uinavigationcontroller.

我将以下代码放在当在主视图中按下按钮时启动的ibaction中。

I put the following code in an ibaction that kicks off when a button is pressed in the primary view.

         PhoneNumberViewController *phoneNumberViewController1 = [[PhoneNumberViewController alloc] initWithNibName:@"PhoneNumberView2" bundle:nil];
self.phoneNumberViewController = phoneNumberViewController1;
[self.view removeFromSuperview];
[self.view insertSubview: phoneNumberViewController1.view atIndex:0];

当这段代码执行整个视图只是空白。如果我省略removefromsuperview部分,那么视图消失在我的按钮后面,但按钮仍然保留。我不知道这是否是正确的方法之间切换按钮,但如果任何人知道如何做到这一点请帮助。

When this code executes the whole view just goes blank. If I omit the removefromsuperview portion then the view disappears behind my button but the button still remains. I'm not sure if this is the right way to switch between buttons but if anyone knows how to do this please help. Also if anyone knows about any example projects that switch between views with buttons let me know.

感谢一百万!

推荐答案

您从它的superview中删除了视图控制器的视图,然后添加了一个子视图。视图层次结构在视图控制器的超级视图(可能是您的窗口)中断开。这就是为什么你得到一个空白的屏幕。

You removed the view controller's view from it's superview and then added a subview to it. The view hierarchy is broken at the view controller's superview (likely your window). That is why you are getting a blank screen.

你可能想保留一个参考的原始视图,然后交换到新的视图通过设置

You'd likely want to keep a reference around to the original view and then swap it out to the new view by setting the view controller's view to the new view.

// origView is an instance variable/IBOutlet to your original view.
- (IBAction)switchToPhoneView:(id)sender {
  if (origView == nil)
    origView = self.view;
  self.view = phoneViewController.view;
}

- (IBAction)switchToOriginalView:(id)sender {
  self.view = origView;
}

这篇关于在uiviews与按钮之间切换,而不是uinavigation控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆