Reputation: 3110
I'm using a UIAlertController in my iOS app; however, for the sake of saving myself a duplicate line of code, I want to initialize the UIAlertController prior to an if/else branch. i.e.
var alertController = UIAlertController()
if /* something */ {
alert.title = "Hello!"
} else {
alert.title = "Goodbye!"
}
//yada yada actions yada yada
self.presentViewController(alert)
However, while I've found I can easily set the title and message after the fact, I can't seem to find a way to set the style. By default it seems to use an ActionSheet, but I really want an alert. I tried changing thepreferredStyle
property, but it seems to be read-only. Is there any way to change the style? For what it's worth, I'm on the latest iOS 8 SDK and using Swift.
Thanks!
Upvotes: 0
Views: 805
Reputation: 23078
Yes, preferredStyle
can only be set at initialization.
You can try something like this:
var alertController = UIAlertController(title: "", message: "", preferredStyle: .Alert)
if /* something */ {
alert.title = "Hello!"
} else {
alert.title = "Goodbye!"
}
//yada yada actions yada yada
self.presentViewController(alert)
Upvotes: 2