ViewController addSubview [英] ViewController addSubview

查看:51
本文介绍了ViewController addSubview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我快要疯了:

GolOlurActionViewController *golOlur = [[GolOlurActionViewController alloc] init];
[self.view addSubview:golOlur.view];

我有上面的代码,我在ViewController内的IBACtion中称呼它.大家都可以猜到GolOlurActionViewControllerViewController.

I have the above code, and I call this in an IBACtion inside a ViewController. GolOlurActionViewController is a ViewController as you all can guess.

该过程开始时,将调用golOlur的viewDidLoadviewDidAppear方法,但不会显示视图.

When the process starts, golOlur's viewDidLoad and viewDidAppear methods are called but the view is not presented.

我已尽我所知,但无法解决此问题.

I have tried everything I know but could not solve this.

推荐答案

如果您打算在视图之间进行转换,则实际上不应该使用addSubview.如果这样做,您将不会收到旋转事件,因为您允许视图控制器层次结构与视图层次结构不同步.您应该仅使用addSubview向视图添加真实的子视图(例如,UILabelUIImageViewUIButton等,或者子视图,如果执行适当的视图控制器约束等). .使用addSubview在视图之间进行转换表示视图控制器和视图之间的根本混淆.

You really should not use addSubview if your intent is to transition between views. If you do so, you won't receive rotation events because you are allowing your view controller hierarchy to get out of sync with the view hierarchy. You should use addSubview only to add a true subview (e.g. a UILabel, a UIImageView, a UIButton, etc., or, the child view if doing proper view controller containment, etc.) to a view. The use of addSubview to transition between views represents a fundamental confusion between view controllers and views.

正确管理视图的关键是确保视图控制器层次结构与视图层次结构同步.最简单的方法是在视图控制器之间进行转换,并让它们负责视图的表示.因此,如果您使用的是NIB,通常是:

The key to proper management of your views is to make sure that your view controller hierarchy is synchronized with your view hierarchy. The easiest way to do this is to do your transitioning between view controllers and let them take care of the presentation of their views. Thus, if you're using NIBs, it would generally be:

GolOlurActionViewController *golOlur = [[GolOlurActionViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:golOlur animated:YES completion:nil];

或者,带有导航控制器的NIB:

Or, NIBs with navigation controller:

GolOlurActionViewController *golOlur = [[GolOlurActionViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:golOlur animated:YES];

或者,如果您正在使用情节提要(那么,再一次,如果您正在使用情节提要,那么您可能正在使用segues,并且不需要其中的任何一个,只是为了完整起见):

Or, if you're using storyboards (then again, if you were using storyboards, you'd probably be using segues and wouldn't need any of this, but just for the sake of completeness):

GolOlurActionViewController *golOlur = [self.storyboard instantiateViewControllerWithIdentifier:@"GolOlurActionView"];
[self presentViewController:golOlur animated:YES completion:nil];

,如果您的情节提要板使用的是导航控制器:

and if your storyboards are using navigation controllers:

GolOlurActionViewController *golOlur = [self.storyboard instantiateViewControllerWithIdentifier:@"GolOlurActionView"];
[self.navigationController pushViewController:pushViewController:golOlur animated:YES];

在极少数情况下,您尝试进行控制器封闭,请告诉我们,因为这稍有不同(需要调用addChildViewControllerdidMoveToParentViewController),但是如果您在视图之间进行基本转换,则可以控制器的初始化以及随后对presentViewControllerpushViewController的调用都应该为您完成.

In the unlikely event you're trying to do controller containment, let us know, because that's slightly different (requiring calls to addChildViewController and didMoveToParentViewController), but if you're doing basic transitioning between views, the proper initialization of your controller and the subsequent call to presentViewController or pushViewController should do it for you.

更新:

顺便说一句,如果您使用情节提要(我认为不是,但以防万一),而不是instantiateViewControllerWithIdentifier,我可能实际上建议您在情节提要上定义一个序列,并为其提供Interface Builder中的一个标识符字符串,然后使用以下代码过渡到下一个场景:

As a quick aside, if you are using storyboards (I don't think you are, but just in case), rather than instantiateViewControllerWithIdentifier, I might actually suggest that you define a segue on the storyboard, supply it with an identifier string in Interface Builder, and then use the following code to transition to the next scene:

[self performSegueWithIdentifier:@"yourIdentifier" sender:self];

如果您采用这种方式,它将为您实例化控制器,整个应用程序的流程将在情节提要中得到准确表示.

If you do it this way, it takes care of instantiating your controller for you and the flow of your entire app will be accurately represented in the storyboard.

这篇关于ViewController addSubview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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