如何禁用alertview';iPhone中的s按钮?
我有一个警报视图,有两个按钮“OK”和“Cancel”,还有一个文本字段。 现在我想禁用“OK”按钮,直到用户在textfield中输入一些文本。 我该怎么做?如何禁用alertview';iPhone中的s按钮?,iphone,uialertview,Iphone,Uialertview,我有一个警报视图,有两个按钮“OK”和“Cancel”,还有一个文本字段。 现在我想禁用“OK”按钮,直到用户在textfield中输入一些文本。 我该怎么做? 提前感谢您可以为“确定”和“取消”创建两个按钮。然后将这两个按钮作为子视图添加到UIAlertView中。现在,通过检查文本字段中的文本(文本长度),您可以执行“启用”和“禁用”操作。在不了解应用程序上下文的情况下,以下内容可能不适用-但您是否阅读了?听起来,如果UIAlertView经常向用户显示,那么您最好找到它的替代品。与您的问题
提前感谢您可以为“确定”和“取消”创建两个按钮。然后将这两个按钮作为子视图添加到UIAlertView中。现在,通过检查文本字段中的文本(文本长度),您可以执行“启用”和“禁用”操作。在不了解应用程序上下文的情况下,以下内容可能不适用-但您是否阅读了?听起来,如果UIAlertView经常向用户显示,那么您最好找到它的替代品。与您的问题并不相关,但如果您不希望您的应用被拒绝,请不要修改默认的UIAlertView。如果我没记错的话,您正在将文本字段添加到alertview,是吗?类似于登录视图。您应该创建自己的视图 因此,关于您的问题,请创建视图,设置已禁用的按钮,并委派UITextFields。什么时候
- (void)textFieldDidBeginEditing:(UITextField *)textField;
调用时,启用这些按钮。更新2:对于Swift 5.1
<#your alert controller#>.addTextField {(tf) in
//... set your tf characteristics i.e .keyboardType here
NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification,
object: tf,
queue: OperationQueue.main) { _ in
//enable or disable the selected action depending on whether the textField text is empty
<#your alert controller#>.actions[0].isEnabled = !tf.text!.isEmpty
}
}
更新:苹果公司已弃用UIAlertView而代之以UIAlertController之后,iOS 8开始运行。不再有对alertview shouldenablefirstotherbutton:
因此,您可以通过UITextFieldTextDidChangeNotification
使用将文本视图添加到警报中
- (void)addTextFieldWithConfigurationHandler:(void(^)(UITextField*textField))configurationHandler
-(void)textFieldHasText:(NSNotification*)notification{
//inside the notification is the object property which is the textField
//we cast the object to a UITextField*
if([[(UITextField*)notification.object text] length] == 0){
//The UIAlertController has actions which are its buttons.
//You can get all the actions "buttons" from the `actions` array
//we have just one so its at index 0
[<#your alert#>.actions[0] setEnabled:NO];
}
else{
[<#your alert#>.actions[0] setEnabled:YES];
}
}
-(void)textfieldhestxt:(NSNotification*)通知{
//通知内部是对象属性,它是文本字段
//我们将对象强制转换为UITextField*
如果([[(UITextField*)notification.object text]长度]==0){
//UIAlertController具有作为其按钮的操作。
//您可以从'actions'数组中获取所有操作“按钮”
//我们只有一个,所以它的索引为0
[.actions[0]设置启用:否];
}
否则{
[.actions[0]设置启用:是];
}
}
完成后别忘了删除你的观察者我想通过添加这个来扩展Ryan Forsyth的答案。如果添加默认样式的UIAlertView,如果尝试访问不存在的文本字段,则可能会出现超出范围的异常,因此请先检查视图样式
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView
{
if(alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput ||
alertView.alertViewStyle == UIAlertViewStylePlainTextInput ||
alertView.alertViewStyle == UIAlertViewStyleSecureTextInput)
{
NSString* text = [[alertView textFieldAtIndex:0] text];
return ([text length] > 0);
}
else if (alertView.alertViewStyle == UIAlertViewStyleDefault)
return true;
else
return false;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//in here we want to listen for the "UITextFieldTextDidChangeNotification"
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldHasText:)
name:UITextFieldTextDidChangeNotification
object:textField];
}
-(void)textFieldHasText:(NSNotification*)notification{
//inside the notification is the object property which is the textField
//we cast the object to a UITextField*
if([[(UITextField*)notification.object text] length] == 0){
//The UIAlertController has actions which are its buttons.
//You can get all the actions "buttons" from the `actions` array
//we have just one so its at index 0
[<#your alert#>.actions[0] setEnabled:NO];
}
else{
[<#your alert#>.actions[0] setEnabled:YES];
}
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView
{
if(alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput ||
alertView.alertViewStyle == UIAlertViewStylePlainTextInput ||
alertView.alertViewStyle == UIAlertViewStyleSecureTextInput)
{
NSString* text = [[alertView textFieldAtIndex:0] text];
return ([text length] > 0);
}
else if (alertView.alertViewStyle == UIAlertViewStyleDefault)
return true;
else
return false;
}