Objective-C设置集合的委托

时间:2013-06-24 19:24:02

标签: iphone objective-c

我正在尝试将委托设置为我的UITextfield集合,以便我可以在所有文本字段上调用resignFirstResponder。我试过的没有回应。

@property (nonatomic, weak) IBOutletCollection(UITextField) NSMutableArray *textFields; 

- (void)viewDidLoad
{
    [_textFields enumerateObjectsUsingBlock:^(UITextField *textfield, NSUInteger idx, BOOL *stop)
    {
        textfield.delegate=self;
    }];
}

- (BOOL) textFieldShouldReturn:(UITextField *)textFields
{
    NSLog(@"textFieldShouldReturn Fired :)");
    [_textFields enumerateObjectsUsingBlock:^(UITextField *textfield, NSUInteger idx, BOOL *stop)
    {
        [textfield resignFirstResponder];
    }];

    return YES;
}

1 个答案:

答案 0 :(得分:0)

您是否在.h?

中添加了UITextFieldDelegate
@interface MyVC : UIViewController <UITextFieldDelegate>

也许可以试试:

for (UITextField __strong *field in self.textFields)
   field.delegate = self;