by
8,516 33
1
7
4,307
1
Top 1% !
Popular
Famous
Easy-to-find
Specified
Refined
MultiPlatform
Popularity: 2186th place
Languageswift
LicenseMIT_X11
OSMac OS X

Swift Collections: Create UIAlertViews in Swift (iOS 8)

This is how you make an alert popup on your iPhone application.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/119027/Swift-Collections--Create-UIAlertViews-i?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
For beginners UIAlertViews were, on iOS 7, one of the main ways that iOS Developers had to let the app users know something important about the application and its current state. UIActionSheet was sort of the same thing, but with more action-based focus, prompting the user to do something. On iOS 8 these two classes have been merged into one, called UIAlertController. We can display a message to the user doing the following: ---------------- var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) self.presentViewController(alert, animated: true, completion: nil) ---------------- That will display a message to the user, but with no buttons so the user can take any action. Also, note that the constructor for the UIAlertController object accepts a preferredStyle param, which can be anyone of the following: .Alert: shows the message to the user at the center of the screen. .ActionSheet: shows the message to the user at the bottom of the screen. => Actions So far we’ve showed the user a message, but we haven’t provided them a way to give feedback to us. Enter UIAlertAction. This class has a closure-based syntax and allows us to add buttons to a UIAlertController object, and define what should happen when the user taps that button. ---------------- var alert = .... alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in // What happens when the user taps 'Ok'? That goes in here })) self.presentViewController(alert, animated: true, completion: nil) ---------------- The UIAlertAction constructor accepts three params: a title (String), a style (UIAlertActionStyle) and a handle (a closure). The style of the UIAlertAction object can be anyone of the following: .Destructive: renders the title property on red .Default .Cancel We can add as many UIAlertActions to a UIAlertController as we want, just be careful not to give your users too many options. Keep things simple.

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus