Change UIAlertView content with buttons - ios

I have created a UIAlertView. The main content is loaded with JSON and I want that when I click "Completo" button, the content change to another variable value completo. When I hit Mapa I need to open a map. And when I click Atrás it should close the alert (right now all the buttons close it).
var tabla: String = "" // JSON Data
...
#IBAction func Itinerario(sender: AnyObject) {
let alert = UIAlertView(title: "Itinerario de Procesión", message: (tabla), delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "Completo", "Mapa", "Atrás")
alert.show()
}
Also I wanted to change buttons color.
I am getting this advice: /Users/Javi/Desktop/ElPenitente/El Penitente/Domingo R.swift:87:21: 'UIAlertView' was deprecated in iOS 9.0: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

So what the error means is that you should use a UIAlertController instead.
Usage is quite straightforward:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
let action1 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
let action2 = UIAlertAction(title: "Action", style: UIAlertActionStyle.Default) { (action) in
// Do something here when action was pressed
}
alert.addAction(action1)
alert.addAction(action2)
self.presentViewController(alert, animated: true, completion: nil)
Where action1 has a system style .Cancel and no handler, and action2 has a default style and a handler.
Changing the color of the actions can be done using the tintColor attribute of the views, see this answer.

Related

Unable to change UIAlertController title and message

I am setting up the UIAlertController in this way:
this implementation is done in viewDidLoad -
actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionButton1 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionButton2 = UIAlertAction(title: " Resend", style: .default) {
action in
self.resendCode()
}
actionButton2.setValue(UIImage(named: "MailForActionSheet"), forKey: "image")
actionButton2.setValue(0, forKey: "titleTextAlignment")
actionSheet.addAction(actionButton1)
actionSheet.addAction(actionButton2)
I've set title and message as nil because I want no header in the action sheet.
The function resendCode tries to modify the action sheet in this way (implemented in the same viewcontroller):
func resendCode()
{
actionButton2.isEnabled = false
actionSheet.title = "Resending Code not be done again"
actionSheet.message = ""
}
However all I see is blank title and message (header is now present). What's wrong here and how should I correct this?
Before:
After:
edit:
Action sheet is presented on click of a button
present(actionSheet, animated: true, completion: nil)

What is the name of this popover class?

I cannot find the name of the class for this 'popover'. Apple uses it a lot in their applications.
I've searched for popover, NSAlert, custom hidden/visible views, and many more.
What is it called?
This is UIAlertController. Before ios7 it is know as UIActionSheet
A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts.
#IBAction func showActionSheetTapped(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .ActionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
//Code for launching the camera goes here
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
//Code for picking from camera roll goes here
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Output:
May be it will help you.

Show an UIAlertController from a button on an UITableViewCell

This is what I coded inside a button in a UITableViewCell.
var shareAlert = UIAlertController(title: "Post Alert",
message: "Your Post has been Shared with your Friends",
preferredStyle: UIAlertControllerStyle.Alert)
var Ok = UIAlertAction(title: "Ok",
style: UIAlertActionStyle.Default,
handler: nil)
shareAlert.addAction(Ok)
shareAlert.presentViewController(shareAlert, animated: true, completion: nil)
When the user clicks the button I want it to show this alert, but the app crashes as soon as I click the button. Am I doing it wrong? How do I show an alert view from a button in a UITableViewCell?
It crashed because of this line
shareAlert.presentViewController(shareAlert, animated: true, completion: nil)
A controller cannot present itself, so
Replace
shareAlert.presentViewController(shareAlert, ...)
by
yourCurrentViewController.presentViewController(shareAlert, ...)
And you should not use UIAlertView because it was deprecated in iOS 9 and maybe obsoleted in the next iOS, so use UIAlertController and you don't have to change it in near future
Please check this:
https://stackoverflow.com/a/32574681/5304286
You can call alertShow function in this file to show simple message by the way:
Utility.UI.alertShow("message", withTitle: "title", viewController: self)
Instead of using UIAlertController you can use UIAlertView, like this:
var alert = UIAlertView(title: "Post Alert", message: "Your Post has been Shared with your Friends", delegate: nil, cancelButtonTitle: "Ok")
alert.show()
Documentation:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html
Note:
UIAlertView is deprecated in iOS 9.

Where to find a clear explanation about swift alert (UIAlertController)?

Couldn't find a clear and informative explanation for this.
After searching a while on a subject I didn't
find a clear explanation , even in it's class reference
UIAlertController Reference
It is ok, but not clear enough for me.
So after collecting some peaces I decided to make my own explanation
(Hope it helps)
So here it goes:
UIAlertView is deprecated as pointed out :
UIAlertView in Swift
UIAlertController should be used in iOS8+
so to create one first we need to instantiate it,
the Constructor(init) gets 3 parameters:
2.1 title:String -> big-bold text to display on the top of alert's dialog box
2.2 message:String -> smaller text (pretty much explains it's self)
2.3 prefferedStyle:UIAlertControllerStyle -> define the dialog box style, in most cases: UIAlertControllerStyle.Alert
Now to actually show it to the user, we can use showViewController or presentViewController and pass our alert as parameter
To add some interaction with a user we can use:
4.1
UIAlertController.addAction to create buttons
4.2
UIAlertController.addTextField to create text fields
Edit note: code examples below, updated for swift 3 syntax
Example 1: Simple Dialog
#IBAction func alert1(sender: UIButton) {
//simple alert dialog
let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert);
//show it
show(alert, sender: self);
}
Example 2: Dialog with one input textField & two buttons
#IBAction func alert2(sender: UIButton) {
//Dialog with one input textField & two buttons
let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert);
//default input textField (no configuration...)
alert.addTextField(configurationHandler: nil);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
let fields = alert.textFields!;
print("Yes we can: "+fields[0].text!);
}));
present(alert, animated: true, completion: nil);
}
Example 3: One customized input textField & one button
#IBAction func alert3(sender: UIButton) {
// one input & one button
let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert);
//configured input textField
var field:UITextField?;// operator ? because it's been initialized later
alert.addTextField(configurationHandler:{(input:UITextField)in
input.placeholder="I am displayed, when there is no value ;-)";
input.clearButtonMode=UITextFieldViewMode.whileEditing;
field=input;//assign to outside variable(for later reference)
});
//alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later
func yesHandler(actionTarget: UIAlertAction){
print("YES -> !!");
//print text from 'field' which refer to relevant input now
print(field!.text!);//operator ! because it's Optional here
}
//event handler with predefined function
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler));
present(alert, animated: true, completion: nil);
}
Hope It helps, and good luck ;-)
An instance of the UIAlertController can be presented modally on screen just as any other UIViewController using the presentViewController:animated:completion: method. What makes the UIAlertController instance differentiate between working as an ActionSheet or as an AlertView is the style parameter you pass when creating it.
No more delegation
If you have used a UIActionSheet or UIAlertView, you know that the way to get a callback from it is for a class (in most cases the ViewController) to implement the UIActionSheetDelegate or UIAlertViewDelegate protocol. There are some open source projects that replaced this delegation pattern with block based callbacks, but the official APIs were never updated. UIAlertController does not use delegation. Instead, it has a collection of UIAlertAction items, that use closures (or blocks if you are using Objective-C) to handle user input.
For Action Sheet
#IBAction func showActionSheet(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .ActionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
//Code for launching the camera goes here
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
//Code for picking from camera roll goes here
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
For AlertView with Text Field
#IBAction func showAlert(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in
//Do some other stuff
}
actionSheetController.addAction(nextAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Some of the syntax has changed since the original responses. Here is some sample code that alerts the user if they are not signed in to iCloud.
CKContainer.default().accountStatus { (accountStatus, error) in
switch accountStatus {
case .available:
print("iCloud Available")
case .noAccount:
print("No iCloud account")
//simple alert dialog
let alert=UIAlertController(title: "Sign in to iCloud", message: "This application requires iClound. Sign in to your iCloud account to write records. On the Home screen, launch Settings, tap iCloud, and enter your Apple ID. Turn iCloud Drive on. If you don't have an iCloud account, tap Create a new Apple ID", preferredStyle: UIAlertControllerStyle.alert);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil));
//show it
self.present(alert, animated: true, completion: nil)
case .restricted:
print("iCloud restricted")
case .couldNotDetermine:
print("Unable to determine iCloud status")
}
}

Dismissal of UIAlertController (best practice)

When using UIAlertController like this:
var alert = UIAlertController(title: "Core Location",
message: "Location Services Disabled!",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
handler: nil))
self.navigationController.presentViewController(alert, animated: true,
completion: nil)
I noticed that the dismissal of the alert view is seemingly done automatically.
Shouldn't the dismissal of a presented ViewController be done by the presenting ViewController via a delegate call?
The dismissal is "included" in the presentViewController call. You do not need a delegate because you have the completion block. In this block you put what you would normally put into the delegate callback, except the call to dismiss the alert.
As far as "best practice" is concerned, I noted that in many APIs, Apple replaced delegate callbacks with completion blocks. Apple typically recommends using the block syntax. I surmise this could be partly because it helps keeping the related code sections together.
Is some Cases you may like to use this:
class MyAlertController : UIAlertController {
var dismissHandler : (()->())?
override func viewDidDisappear(_ animated: Bool) {
dismissHandler?()
super.viewDidDisappear(animated)
}
}
Usage:
let alert = MyAlertController(title: ...
let cancelButton = UIAlertAction(titl
alert.dismissHandler = { /*...do something */ }
alert.addAction(cancelButton)
...
There is an elegant way! Just write the action or function inside the alert controller's cancel action. (here the action style should be .cancel)
Code for Swift 3:
let Alert: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
let OkAction: UIAlertAction = UIAlertAction(title: “Ok”, style: .default) { ACTION in
//Will be called when tapping Ok
}
let CancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) {ACTION in
// Will be called when cancel tapped or when alert dismissed.
// Write your action/function here if you want to do something after alert got dismissed”
}
Alert.addAction(OkAction)
Alert.addAction(CancelAction)
present(Alert, animated: true, completion: nil)

Resources