ios - 未调用 UIAlertView 子类中的 alertViewShouldEnableFirstOtherButton

标签 ios iphone objective-c uialertview

我创建了一个 UIAlertView 子类。它实现了两个 UIAlertViewDelegate 方法:alertView:clickedButtonAtIndex: 在预期时被调用,但是 alertViewShouldEnableFirstOtherButton: 从未被调用。

查看相关帖子并添加:

[textField sendActionsForControlEvents:UIControlEventEditingChanged];

但没有结果。我似乎别无选择。我在这里缺少什么?

@implementation MyAlertView

+ (MyAlertView*)showAlertForJson:(NSDictionary*)json delegate:(id<F2WebAlertDelegate>)delegate
{
    MyAlertView* view = [[MyAlertView alloc] initWithJson:json delegate:delegate];

    return view;
}


- (instancetype)initWithJson:(NSDictionary*)json delegate:(id<MyWebAlertDelegate>)delegate
{
    if (self = [super initWithTitle:json[@"title"]
                            message:json[@"message"]
                           delegate:self
                  cancelButtonTitle:json[@"cancelTitle"]
                  otherButtonTitles:nil])
    {
        _json            = json;
        self.webDelegate = delegate;

        for (NSString* title in json[@"otherTitles"])
        {
            [self addButtonWithTitle:title];
        }

        [self initInput:json[@"input"]];

        [self show];
    }

    return self;
}


- (void)initInput:(NSDictionary*)json
{
    if (json == nil)
    {
        return;
    }

    [json[@"style"] isEqualToString:@"plain"]       ? self.alertViewStyle = UIAlertViewStylePlainTextInput        : 0;
    ...

    [self initTextField:[self textFieldAtIndex:0] withJson:json[@"field0"]];
    if (self.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput)
    {
        [self initTextField:[self textFieldAtIndex:1] withJson:json[@"field1"]];
    }
}


- (void)initTextField:(UITextField*)textField withJson:(NSDictionary*)json
{
    [textField sendActionsForControlEvents:UIControlEventEditingChanged];

    if (textField == nil || json == nil)
    {
        return;
    }

    [json[@"keyboard"] isEqualToString:@"ascii"]       ? (textField.keyboardType = UIKeyboardTypeASCIICapable)          : 0;
    ...
}


#pragma mark - Alert View Delegate

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    ...
}


- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView
{        
    return YES;
}

@end

最佳答案

事实证明,正如委托(delegate)方法的名称所示,您必须为 UIAlertViewinitWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles 指定一个列表 otherButtonTitles :

因此,以后使用 addButtonWithTitle: 添加的按钮不算在内。

关于ios - 未调用 UIAlertView 子类中的 alertViewShouldEnableFirstOtherButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20499465/

相关文章:

objective-c - NSArray 或 NSDictionary 用于存储在 UserDefaults 中?

ios - objective-c : Get ISO Formated Date from String '2015-11-25T00:00:00.000Z'

iphone - 使用NSPredicate将数组与另一个数组进行比较

iOS:检测包含多个图像的 UIImageView 中的图像触摸

ios - 隐藏单个 UITableViewCell 分隔符

ios - 如果您的应用程序被 iOS 系统终止,它会生成崩溃报告吗?

从图库中选择的应用程序中的 iphone 存储图像

iphone - 从 NSTimer 访问 NSArray 间隔 = EXC_BAD_ACCESS

ios - 无法将 Parse.com 中存储的 NSData 转换为 NSString?

iphone - UIPopoverController 和内存管理