在iOS中输入空的AlertView文本字段时,不应解除AlertViewController

在iOS中输入空的AlertView文本字段时,不应解除AlertViewController,ios,objective-c,swift,xcode,ios9,Ios,Objective C,Swift,Xcode,Ios9,我在AlertViewController中添加了一个文本字段。我不在文本字段中键入任何内容,并输入OK按钮,这意味着关闭警报视图。如何检查警报文本字段长度是否为零,以便禁用警报按钮操作。请帮我…试试这个代码 // // ViewController.m // AlertControllerDemo // // Created by Nilesh on 8/10/16. // Copyright © 2016 Nilesh. All rights reserved. // #impor

我在AlertViewController中添加了一个文本字段。我不在文本字段中键入任何内容,并输入OK按钮,这意味着关闭警报视图。如何检查警报文本字段长度是否为零,以便禁用警报按钮操作。请帮我…

试试这个代码

//
//  ViewController.m
//  AlertControllerDemo
//
//  Created by Nilesh on 8/10/16.
//  Copyright © 2016 Nilesh. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property(nonatomic, strong)UIAlertAction *okAction;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)showAlertButtonAction:(id)sender {

    self.okAction = [UIAlertAction actionWithTitle:@"Okay"
                                             style:UIAlertActionStyleDefault
                                           handler:nil];
    self.okAction.enabled = NO;

    UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil
                                                                        message:@"Please Enter your text"
                                                                 preferredStyle:UIAlertControllerStyleAlert];

    [controller addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.delegate = self;
    }];

    [controller addAction:self.okAction];
    [self presentViewController:controller animated:YES completion:nil];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    [self.okAction setEnabled:(finalString.length >= 1)];
    return YES;
}
@end
//
//ViewController.m
//AlertControllerDemo
//
//由Nilesh于2016年8月10日创建。
//版权所有©2016 Nilesh。版权所有。
//
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,强)UIAlertAction*OkaAction;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)showAlertButtonAction:(id)发件人{
self.okAction=[UIAlertAction actionWithTitle:@“好的”
样式:UIAlertActionStyleDefault
处理程序:无];
self.okAction.enabled=否;
UIAlertController*控制器=[UIAlertController alertControllerWithTitle:nil
信息:@“请输入您的文字”
首选样式:UIAlertControllerStyleAlert];
[控制器addTextFieldWithConfigurationHandler:^(UITextField*textField){
textField.delegate=self;
}];
[控制器addAction:self.okAction];
[自我呈现视图控制器:控制器已设置动画:是完成:无];
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
NSString*finalString=[textField.text stringByReplacingCharactersInRange:range with string:string];
[self.okAction setEnabled:(finalString.length>=1)];
返回YES;
}
@结束

试试这段代码

//
//  ViewController.m
//  AlertControllerDemo
//
//  Created by Nilesh on 8/10/16.
//  Copyright © 2016 Nilesh. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property(nonatomic, strong)UIAlertAction *okAction;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)showAlertButtonAction:(id)sender {

    self.okAction = [UIAlertAction actionWithTitle:@"Okay"
                                             style:UIAlertActionStyleDefault
                                           handler:nil];
    self.okAction.enabled = NO;

    UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil
                                                                        message:@"Please Enter your text"
                                                                 preferredStyle:UIAlertControllerStyleAlert];

    [controller addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.delegate = self;
    }];

    [controller addAction:self.okAction];
    [self presentViewController:controller animated:YES completion:nil];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    [self.okAction setEnabled:(finalString.length >= 1)];
    return YES;
}
@end
//
//ViewController.m
//AlertControllerDemo
//
//由Nilesh于2016年8月10日创建。
//版权所有©2016 Nilesh。版权所有。
//
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,强)UIAlertAction*OkaAction;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)showAlertButtonAction:(id)发件人{
self.okAction=[UIAlertAction actionWithTitle:@“好的”
样式:UIAlertActionStyleDefault
处理程序:无];
self.okAction.enabled=否;
UIAlertController*控制器=[UIAlertController alertControllerWithTitle:nil
信息:@“请输入您的文字”
首选样式:UIAlertControllerStyleAlert];
[控制器addTextFieldWithConfigurationHandler:^(UITextField*textField){
textField.delegate=self;
}];
[控制器addAction:self.okAction];
[自我呈现视图控制器:控制器已设置动画:是完成:无];
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
NSString*finalString=[textField.text stringByReplacingCharactersInRange:range with string:string];
[self.okAction setEnabled:(finalString.length>=1)];
返回YES;
}
@结束

到目前为止,您尝试了什么?也许您非常接近解决方案,我们将提供帮助。最初,您可以禁用按钮并使用UITextFieldDelegate方法public func textField(textField:UITextField,shouldChangeCharactersRange:NSRange,replacementString:string)->Bool{}您可以禁用/启用按钮的可能重复项如何标记objective-c和swift?你用的是哪一个?也看看盖伊·卡隆的答案。。。可能会有帮助到目前为止你都试过什么?也许您非常接近解决方案,我们将提供帮助。最初,您可以禁用按钮并使用UITextFieldDelegate方法public func textField(textField:UITextField,shouldChangeCharactersRange:NSRange,replacementString:string)->Bool{}您可以禁用/启用按钮的可能重复项如何标记objective-c和swift?你用的是哪一个?也看看盖伊·卡隆的答案。。。也许会有帮助