ViewController addSubview

12,331

Solution 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.

Solution 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];    
Share:
12,331
iremk
Author by

iremk

software.developer / american.football.player / wing.man from the day that i start thinking for my future , i was in to the computers ... it was because of my father of course , he made me to use my first computer even when i'm 3 years old.. and my addiction to technology started then..until college , i have not worked on any programming, but, after that , i saw that it was always inside me.. the addiction started to grow and here i am.. working in 2 different jobs at the same time.. even making my own projects for iphone and have some more in my mind for iphone, for just web entertainment and social networks.. it's not all about software development , working for my family company for a long time.. so my future will be the mix of software & health field , development and management side.. for my future , i know where it is headed , it's in the way i want but if you think that you could make it better , you are welcome :)

Updated on June 04, 2022

Comments

  • iremk
    iremk almost 2 years

    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.