Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

presenting a modal in viewdidappear with swift

Tags:

ios

swift

I'm new to swift and ios programming in general. I'm trying to display a modal view when my app first loads which it does. The problem I'm running into is that my modal keeps appearing over and over and over. Not sure where I'm going wrong.

BONUS QUESTION: Ultimately I'd like this to only happen the first time the user opens the app.

class ViewController: UIViewController {

    var introModalDidDisplay = false

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func showIntroModal() {
        if (!introModalDidDisplay) {
            println(introModalDidDisplay)
            introModalDidDisplay = true
            let intro = self.storyboard?.instantiateViewControllerWithIdentifier("introModal") as IntroModalViewController
            intro.modalPresentationStyle = UIModalPresentationStyle.FormSheet
            self.presentViewController(intro, animated: true, completion: nil)
        }
    }
}
like image 207
timT Avatar asked Oct 15 '14 18:10

timT


People also ask

What is difference between viewWillAppear and viewDidAppear?

The viewWillAppear method is called before loading the actual view. The viewDidAppear method is called when the view is already loaded, and you want to show something.

What is the difference between viewDidLoad and viewDidAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.

How do I view a controller in full screen?

You can do it in Interface Builder. Select the viewController. Replace automatic by Full screen.


1 Answers

Found it. My "intro" class was extending ViewController rather than UIViewController...apparently that's bad. Thanks for the help! Sorry for the wild goose chase.

like image 181
timT Avatar answered Oct 04 '22 10:10

timT