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)
Related
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 am getting a very strange error. I think the compiler is trying to tell me that it can't segue to another view controller until it is done executing all the code in my current view controller but I am not sure.
I'm literally getting input by using an alert box (i.e. calling a function called generateTextField).
Then when I am done I'm saying "Hey I want you to go to another view controller" - but the the compiler instead tells me "Hey I don't think so".
Here is my error:
Warning: Attempt to present HairStyle1ViewController: 0x7...> on browseBarbersViewController: 0x7...> which is already presenting
Warning: Attempt to present HairStyle1ViewController: 0x7..> on browseBarbersViewController: 0x7...> which is already presenting
#IBAction func AddNewStyleButtonClicked(sender: AnyObject)
{
// Get the "hairstyle name" from the user
generateTextField();
// OK We are done with that function, now transition to the
// next screen
performSegueWithIdentifier("HairStyle1", sender: self);
}
// Generate a text field for user input (i.e. call the alert function)
func generateTextField()
{
//1. Create the alert controller.
var tempStyle = "";
var alert = UIAlertController(title: "Add a New Style", message: "Enter the name of the new hairstyle below", preferredStyle: .Alert);
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.placeholder = "Your New Hairstyle Goes Here..";
})
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
tempStyle = textField.text!;
print("New Style Added is: " + tempStyle);
HairStyle = tempStyle;
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
}
It's also weird that when I take out the generateTextField() function it performs the segue perfectly. I'm very confused.
Wow, I figured it out. I had to instead, segue in the body of the alert function.
I fixed this by adding
self.performSegueWithIdentifier("HairStyle1", sender: self);
after the
HairStyle = tempStyle; line
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.
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.
I want do display an alert message and I am using iOS SDK 8.1 with XCode 6.1. I know that UIAlertView is deprecated; however, my app has to support also iOS 7 and I have to use UIAlertView if the app is running on an iOS 7 device. This alert view has a text field and two buttons where one of them is default cancel button. The other button should be disabled as long as text field is empty.
Here is my code:
class MyViewController : UIViewController, UIAlertViewDelegate {
var addRecipientAlertView:UIAlertView?
// Irrelevant code here
func performSomething(someValue:String) {
addRecipientAlertView = UIAlertView(title: "Title", message: "Enter full name of user, email of user or a free-form text", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Add Recipient")
addRecipientAlertView!.alertViewStyle = UIAlertViewStyle.PlainTextInput
addRecipientAlertView!.accessibilityValue = someValue
// Text Field Settings
let textField:UITextField = addRecipientAlertView!.textFieldAtIndex(0)!
textField.placeholder = "Full Name, Email or Any Text"
textField.keyboardType = UIKeyboardType.EmailAddress
textField.clearButtonMode = UITextFieldViewMode.Always
addRecipientAlertView!.show()
}
}
func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool {
return false
}
The problem is; whatever I have tried, the first other button was not disabled anyway. Finally I gave up trying to check the text of my text field and I have implemented the alertViewShouldEnableFirstOtherButton delegate method such that it always return false. However, the result has not changed and both of the buttons (named "Cancel" and "Add Recipient" in this example) are still enabled. What am I missing?
I had the same problem and I guess it's a bug in the SDK. The only working solution that I managed to come up with with was to implement an Objective-C class that showed the alert and served as its delegate.