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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With