0

I would like to simply show a second ViewController from a already active ViewController by code. I would like to do this as much as possible with only coding (instead of drag and drop thing in the storyboard)

I tried to do as follow, but the second view doesn't popup.

My Storyboard. Left is the initial view, right is the view I like to open from the initial view. In the screen is the right view clicked, so you can see the properties on the right:

enter image description here

My initial UIViewcontroller class:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad();
        dosomestuff();

        showTimeline(); // << --------------------
    }


func showTimeline() {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("Timeline") as! UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
    //self.showViewController(vc, sender: self);

}

Question Update, because it is unclear what I am asking : When I start the App only the first screen is present. No crash just a Warning in the Log "Warning: Attempt to present on whose view is not in the window hierarchy!"

any help how to do this ?

2
  • So what is the exact issue you encounter? It crashes? It does not show anything? Please specify your question. Commented Mar 5, 2015 at 7:53
  • Check the two answers here stackoverflow.com/questions/24099533/… Commented Mar 5, 2015 at 8:12

3 Answers 3

2

When view did load is called your view controller is not actually part of the view hierarch (and generally not part of the view controller hierarchy) and so can't present other views. You should have seen a log warning when you tried it. Also, what is the point of the first view if it always presents another view, why not just make the second view the root...

So, change the presentation by changing the root controller or presenting after the current root view is actually shown on screen.

Sign up to request clarification or add additional context in comments.

2 Comments

Regarding this "what is the point of the first view if it always presents another view,", maybe he wants to give the user the ability to return back to it to change the option of viewing the second viewController like whatsApp doing in their 'adding photos from library' feature
I don't know if my screen design is good or not. I would like to have an first view which initiate all stuff and loads data (Can take some minutes and presents pictures and progressbar during load) and then I would like to decide which view should be presented. @Wain: Your solution seems to be correct, because I get the mentioned warning from you. Know I am very beginner in IOS and I don't know what means "presenting after the current root view is actually shown on screen" ?
0

As Wain described, you need to change your rootController like this :

func showTimeline() {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("Timeline") as! UIViewController
    UIApplication.sharedApplication().keyWindow.rootViewController = vc;
}

Or add showTimeline() into ViewDidAppear:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        showTimeline();
    }

Think it should works ;)

2 Comments

Thanks a lot for helping. I guess you both are having the right solution. Now I stuck with the practic. I get an error Method does not override any method from its superclass, when I add the "override func viewDidAppear() {" to my "ViewController". Its on the same level as my "override func viewDidLoad() {" . I am using Xcode 6.3. Any help ?
Thanks a lot for helping. Now it works and I learned a lot of the basics with viewcontrollers. I accept your answer.
0

You shouldn't present view controllers in viewDidLoad. The proper place to present new view controller is after the current one is presented.

Try call showTimeLine() from viewDidAppear()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.