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.
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: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 useaddSubview
only to add a true subview (e.g. aUILabel
, aUIImageView
, aUIButton
, etc., or, the child view if doing proper view controller containment, etc.) to a view. The use ofaddSubview
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:
Or, NIBs with navigation controller:
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):
and if your storyboards are using navigation controllers:
In the unlikely event you're trying to do controller containment, let us know, because that's slightly different (requiring calls to
addChildViewController
anddidMoveToParentViewController
), but if you're doing basic transitioning between views, the proper initialization of your controller and the subsequent call topresentViewController
orpushViewController
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: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.