1

In my iOS project I use two kinds of UIStoryboardSegue, which present a view either within a navigation controller or as a modal view. I set the kind property in Interface Builder to:

  • Show (e.g. Push)
  • Present Modally

Now I want to be able to programmatically identify the kind of segue in order to customise the appearance of my ViewController. Like so:

class ViewController : UIViewController {
    var isModal : Bool = false
    ...
}

class OtherViewController : ViewController {
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.destinationViewController is ViewController {
            let vc = segue.destinationViewController as! ViewController
            vc.isModal = TODO
        }
    }
}

I was hoping there would be a property, but I can't find it. I was also hoping that the segue class would differ, but I also can't find enough documentation.

I originally stumbled upon this problem trying to use the isModal in order to alternate between dismissing the ViewController vs. popping the ViewController. I have noticed that there now seems to be a better alternative, which is the UnwindSegue. However, I still need the flag in order to customise appearance..

Thanks

Eduard Feicho
  • 568
  • 4
  • 8
  • In the meantime I found a solution to the main problem, which is to identify whether a ViewController is modal or not: http://stackoverflow.com/questions/2798653/is-it-possible-to-determine-whether-viewcontroller-is-presented-as-modal. But this question remains unanswered, if anyone feels interested. – Eduard Feicho Jan 20 '16 at 19:29

1 Answers1

0

Maybe I'm totally wrong but can't you use the identifier of the segue?

For example name all modal view controllers with Modal<Name>. Then check

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{
    if segue.identifier.hasPrefix("Modal") {
        let vc = segue.destinationViewController as! ViewController
        vc.isModal = TODO
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Yeah you could name all Segues, but I dislike the idea that you need to name all Segues (easy to forget). I mean, the information that this is supposed to be a modal segue already exists somewhere, so it feels a bit redundant to duplicate it in the form of a prefix. And it relies on String comparison... In my opinion it should be possible to just query a kind attribute or do class inspection. – Eduard Feicho Jan 21 '16 at 12:20
  • Class introspection is more expensive than string comparison. You could use also an enum type for the segue identifiers with a switch statement. – vadian Jan 21 '16 at 12:27