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 ?

mcfly soft
  • 11,289
  • 26
  • 98
  • 202

3 Answers3

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 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 – Sawsan Mar 05 '15 at 08:17
  • 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" ? – mcfly soft Mar 05 '15 at 08:20
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()

Max Komarychev
  • 2,836
  • 1
  • 21
  • 32
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 ;)

Kevin Machado
  • 4,141
  • 3
  • 29
  • 54
  • 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 ? – mcfly soft Mar 05 '15 at 08:33
  • Thanks a lot for helping. Now it works and I learned a lot of the basics with viewcontrollers. I accept your answer. – mcfly soft Mar 05 '15 at 08:42