I've a simple question: How can I make the message of an UIAlertController be selectable and copyable by the user?
The controller is initiated like so:
let alertController = UIAlertController(title: "Hello World", message: "Copy Me!", preferredStyle: .Alert)
and displayed like so:
presentViewController(alertController, animated: true, completion: nil)
Adam's correct that UIAlertController doesn't provide text selection functionality, so a traditional copy/paste solution isn't going to work. You could alternatively provide a button on your UIAlertController that copies a string to the pasteboard.
UIPasteboard.general.string = "Copy Me!"
It is not possible. UIAlertController has no such functionality. It was implemented with UILabel components, which don't support copying text. You are not allowed to subclass UIAlertController either. The only option is to implement your own controller instead.
Related
I can add textField to my alert as shown below using alert.addTextField()
let alert = UIAlertController(title: "Title", message: "Subtitle", preferredStyle: UIAlertController.Style.alert)
alert.addTextField()
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { _ in
print(alert.textFields?[0].text ?? "")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
However, if I have a custom textField, like CurrencyField as shared in https://stackoverflow.com/a/29783546/3286489, how could I add that to my alert (instead of just having a generic TextField)?
The Holy Path
The Apple-given API doesn't allow for you to use a custom subclass of UITextField. It does, however, allow you to customize the UITextField:
alert.addTextField { textField in
// customize text field
}
You'll have to try and port the functionality of your CurrencyView over, having only access to the APIs available by default on UITextField.
The Unholy Path
⚠️ Disclaimer: This is a Bad Idea. Using vendor APIs in ways they weren't intended to makes your code and application less stable.
Now that we've got that out of the way: you could also add a view directly to the default UITextField. You'd have to disable interaction/editing on the original UITextField, though, and make sure it only goes to your custom text field.
If you really wanted to go to the dark side, you could swizzle UITextField and force your CurrencyView to be initialized, but, once again, this is a Bad Idea.
I am new iOS programming and now am fascinated in using MaterialComponents which provide by google. Now i facing one problem in component named Dialog.
When the view has been pop up on screen when i touch outside that pop up view and then that view has been dismiss. I don't want that to happen in my app.
I don't want user to click outside popup view to dismiss that popup view. What i want i just want user to click on action button that i provide for user's choice then the view should be dismiss when click on that action button only.
Really glade that you help.
MDCAlertController is inherited from UIViewController.
So, in order to restrict user to click outside MDCAlertController you have to access its property named view and then superview?.subviews[0].isUserInteractionEnabled = false
I have completed one example using MDCAlertController
let alert = MDCAlertController(title: title, message: message)
alert.buttonTitleColor = UIColor(red:0.03, green:0.62, blue:0.09, alpha:1.0)
//MDCAlertControllerThemer.applyScheme(alertScheme, to: alert)
let okayAction = MDCAlertAction(title: "Okay") { (action) in
print("User click okay")
}
let cancelAction = MDCAlertAction(title: "Cancel", handler: nil)
alert.addAction(okayAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: {
// When the Dialog view has pop up on screen then just put this line of code when Dialog view has completed pop up.
alert.view.superview?.subviews[0].isUserInteractionEnabled = false
})
use this.
let alert = MDCAlertController(title: title, message: message)
alert.mdc_dialogPresentationController.dismissOnBackgroundTap = false
https://material.io/develop/ios/components/dialogs/api-docs/Categories/UIViewController_28MaterialDialogs_29.html
https://material.io/develop/ios/components/dialogs/api-docs/Classes/MDCDialogPresentationController.html#/c:objc(cs)MDCDialogPresentationController(py)dismissOnBackgroundTap
I was wondering if it was possible to have an alert with preloaded text in it. For example, if my app guessed something I would want the guess to appear in the alert text box but the user could delete the text and change the answer if they wanted to.
Thank you
Have you had a look at this page : How to add a TextField to UIAlertView in Swift ?
You could put textfields (or anything else you'd like) inside the alertview and so that the user could interact with it more deeply :)
Vincent
yes you can. You can create a custom alertView from a xib and you can input there an UITextField ,so the user can have the opportunity to change the text
Simply Create an alert controller leave the title and message empty and add a TextField to your alert that way you can preload your text to the TextField and user can also edit the text
Just add text field in alert controller.
let alertController = UIAlertController(title: "title", message: "please enter words", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (txtUsername) -> Void in
usernameTextField?.text = "preload"
usernameTextField?.placeholder = "placeholder"
}
self.presentViewController(alertController, animated: true, completion: nil)
I was under the impression that if the normal action is a destructive action and the other is a cancel action in their UIAlertController that the destructive one should be on the left and the cancel should be on the right.
If the normal action is not destructive, then the normal action should be on the right and the cancel should be on the left.
That said, I have the following:
var confirmLeaveAlert = UIAlertController(title: "Leave", message: "Are you sure you want to leave?", preferredStyle: .Alert)
let leaveAction = UIAlertAction(title: "Leave", style: .Destructive, handler: {
(alert: UIAlertAction!) in
//Handle leave
}
)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
confirmLeaveAlert.addAction(leaveAction)
confirmLeaveAlert.addAction(cancelAction)
self.presentViewController(confirmLeaveAlert, animated: true, completion: nil)
I was under the impression that if I add the leaveAction first, then the cancelAction that the leaveAction would be the button on the left. This was not the case. I tried adding the buttons in the opposite order as well and it also resulted in the buttons being added in the same order.
Am I wrong? Is there no way to achieve this?
My solution to this was to use the .Default style instead of .Cancel for the cancelAction.
Since iOS9 there is a preferredAction property on UIAlertController. It places action on right side. From docs:
When you specify a preferred action, the alert controller highlights the text of that action to give it emphasis. (If the alert also contains a cancel button, the preferred action receives the highlighting instead of the cancel button.)
The action object you assign to this property must have already been added to the alert controller’s list of actions. Assigning an object to this property before adding it with the addAction: method is a programmer error.
I have a function to display an UIAlertController. Below the title and the text, I would like to display an animation image per image. I have 13 images to loop.
func alertDownloadInProgress(text:String, sender:UIViewController) -> UIAlertController {
var alert = UIAlertController(title: "Alert", message: text, preferredStyle: UIAlertControllerStyle.Alert)
sender.presentViewController(alert, animated: true, completion: nil)
return alert
}
Is it possible to use an UIAlertController or I have to custom completely my pop up?
I don't know of a way to customize a UIAlertController like that; they only have three properties--title, message, preferredStyle, and textFields--none of which you can add a UIImageView to.
I would recommend creating a custom modal presentation controller. I actually just wrote an answer on how to do that. Of course you'll center your custom view instead of pinning it to the right, but that answer could at least help you get started with the wacky world of UIPresentationControllers.