ViewController addSubview

痞子三分冷 提交于 2020-01-02 10:29:22

问题


I am nearly going crazy:

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

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

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.


回答1:


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.

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];

Or, NIBs with navigation controller:

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

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];

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.

Update:

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.




回答2:


I believe your issue is that you are expecting the view in a storyboard or .xib to show up when you create a UIViewController the way you are, which will not work. You need to either wire up a push segue in the storyboard (which will require a UINavigationController), or present the new controller modally, which you can also do in the storyboard. If you have a a nib for this ViewController, you can do this:

 CustomViewController *controller = [[CustomViewController alloc] initWithNibName:@"CustomViewController.xib" bundle:[NSBundle mainBundle]];

 [self presentViewController:controller animated:YES completion:NULL];    


来源:https://stackoverflow.com/questions/11710463/viewcontroller-addsubview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!