This question already has answers here:
Swift default AlertViewController breaking constraints
(12 answers)
Closed 3 years ago.
I'm getting this warning every time an UIAlertController action sheet is presented in a simulated phone. The code is nothing fancy.
let action1 = UIAlertAction(title: "Action 1", style: .default) { _ in }
let action2 = UIAlertAction(title: "Action 2", style: .default) { _ in }
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .actionSheet)
alert.addAction(action1)
alert.addAction(action2)
alert.addAction(cancel)
self.present(alert, animated: true)
I'm running Xcode 10.2 (10E125), Swift 5. The constraint seems out of my control but please advise if I'm doing something wrong.
Screenshot of IB showing the icons for constraint-manipulating menus. You can also click on the constraints themselves to edit or delete them.
Check out the various drop-down menus.
Related
This question already has answers here:
Swift writing a function that takes self as an input
(1 answer)
How to create uialertcontroller in global swift
(12 answers)
Closed 5 years ago.
I have a few four lines blocks as below in my app to decide flow based on user's Yes or No action
let alert = UIAlertController(title: "Delete", message: "Really delete?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .destructive, handler: {(action) in self.deleteHistoryRecord(forRecordID: self.shistIDs[indexPath.row])}))
alert.addAction(UIAlertAction(title: "No", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
I'd like to create global app method/function with first three lines and returning Yes/No flag back. What's the best way to manage hander closure to call proper method in caller?
In order to have a global function able to show the alert, you might use an extension like this one:
extension UIViewController {
func showAlert(_ completion:#escaping ()->()) {
let alert = UIAlertController(title: "Delete", message: "Really delete?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .destructive, handler: { (action) in
completion()
}))
alert.addAction(UIAlertAction(title: "No", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
and then whenever you need to show the alert you just call showAlert passing the completion you would like to be executed (in case of Yes); eg: from your ViewController you might use this:
self.showAlert {
self.deleteHistoryRecord(forRecordID: self.shistIDs[indexPath.row])
}
This question already has answers here:
Unable to choose order of buttons in UIAlertController
(2 answers)
Closed 5 years ago.
I wanna show alert window by pressing on button. Usual thing. But I'm confused that I try to show button "Awesome" at first, but always "Cancel" button stay the first. How can I fix it?
let alert = UIAlertController(title: "Hello world", message: "Testing", preferredStyle: .alert)
let action = UIAlertAction(title: "Awesome", style: .default){(_) in print("Awesome")}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addTextField(configurationHandler: nil)
alert.addAction(action)
alert.addAction(cancel)
alert.actions.forEach( { (action) in print( action.title! ) } )
present(alert, animated: true, completion: nil)
Irregular order of buttons
Changing the order will not work as default position of cancel button is left.
Change the action style for cancel button type to UIAlertActionStyleDefault instead of UIAlertActionStyleCancel.
Try like this, it will work for you.
let alertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert)
let destructiveAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "Awesome", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
}
alertController.addAction(destructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
I'm trying to make an app where you can click to book a reservation. When you click on the button of the reservation you want, an UIAlert will show up and ask if you want to confirm the action. If so, then another UIAlert will show up and confirm your reservation.
I'm having difficulties to understand how to make this series os UIAlerts. Here's the code.
#IBAction func garageinn(_ sender: Any) {
var refreshAlert = UIAlertView()
refreshAlert.title = "Book a vacancy?"
refreshAlert.message = "A vacancy will be reserved at Garage Inn."
refreshAlert.addButton(withTitle: "Cancel")
refreshAlert.addButton(withTitle: "Ok")
refreshAlert.show()
}
You should use UIAlertController as stated for some in the comments.
let alert : UIAlertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
And add an actions (buttons) as required
let accept : UIAlertAction = UIAlertAction(title: "Accept", style: UIAlertActionStyle.default, handler: {
// -code to process- and -create new alert-
})
let cancel : UIAlertAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
As shown you must create the code to process the response in the handler, including creating the new alert.
I'm new to Xcode and Swift. I'm trying to set up an alert (which I have done - code below). I was wondering if someone can help me out. What I would like to do is reset two different variables to 0 if "Yes" is tapped.
Your help is greatly apprecieated.
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Confirm Point Reset", message: "Are You Sure?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Yes", style: .Default, handler: nil)
let secondAction = UIAlertAction(title: "No", style: .Default, handler: nil)
alertController.addAction(firstAction)
alertController.addAction(secondAction)
presentViewController(alertController, animated: true, completion: nil)
}
You need to add the completion handler and update the values in that closure. The completion handler is the code that is executed when the action occurs (when the button is pressed).
let firstAction = UIAlertAction(title: "Yes", style: .Default) { action in
self.foo = 0
}
This question already has answers here:
UIActionSheet in swift puts Cancel button top in iOS 7
(2 answers)
Closed 7 years ago.
I ran my app on iPhone5s iOS7. The indexes of buttons are wrong, cancleButton becomes the first button. I am confused that when I tap the background, it opens the album. I don't know why. So strange.
图片在这里
This is my code.
func actionsheetInIOS8Early() {
let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "cancle", destructiveButtonTitle: nil)
actionSheet.addButtonWithTitle("album")
if UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.Camera) {
actionSheet.addButtonWithTitle("camera")
}
actionSheet.showInView(self.view)
}
When you add
let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "cancle", destructiveButtonTitle: nil)
this line it will add Cancel button to index 0 and then after it will add your buttons
actionSheet.addButtonWithTitle("album")
if UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.Camera) {
actionSheet.addButtonWithTitle("camera")
}
Album button at index 1 & Camera at index 2
So If you want Cancel button at last index then you need to add like
actionSheet.addButtonWithTitle("Cancel")
after adding your button for Album and Camera.
Try this
let optionMenu = UIAlertController(title: "Title", message: "message", preferredStyle: .ActionSheet);
if UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.Camera){
let cameraAction = UIAlertAction(title: "Camera", style: .Default, handler: {
(alert: UIAlertAction) -> Void in
// do something
})
optionMenu.addAction(cameraAction);
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
(alert: UIAlertAction) -> Void in
print("Cancelled")
})
optionMenu.addAction(cancelAction);
self.presentViewController(optionMenu, animated: true, completion: nil);
If u add cancel button as above, cancel button will shown at the end of the AlertController