UIAlertViewController添加文本字段委托

时间:2018-02-05 08:49:15

标签: ios objective-c iphone xcode uitextfield

  

我在UIAlertViewController中实现了textfield,但是我希望实现委托方法到该textfields。

`UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Total Value"  message:@""  preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
     [alert dismissViewControllerAnimated:YES completion:nil];
}];
[ok setEnabled:NO];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
 {
     textField.placeholder = @"TotalValue";
     textField.enabled = YES;
     [[alert textFields][0] delegate];
     [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidChangeNotification object:textField queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
      {
          textField.text.length >0 ? [ok setEnabled:YES]: [ok setEnabled:NO];
      }];
     [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:textField queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
      {
          [self updateTotalValue:textField.text];
      }];
 }];
[alert addAction:ok];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                         {
                             [alert dismissViewControllerAnimated:YES completion:nil];
                         }];
[alert addAction:cancel];
[self.navigationController presentViewController:alert animated:YES completion:nil];`UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Total Value"  message:@""  preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
     [alert dismissViewControllerAnimated:YES completion:nil];
}];
[ok setEnabled:NO];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
 {
     textField.placeholder = @"TotalValue";
     textField.enabled = YES;
     [[alert textFields][0] delegate];
     [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidChangeNotification object:textField queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
      {
          textField.text.length >0 ? [ok setEnabled:YES]: [ok setEnabled:NO];
      }];
     [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:textField queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
      {
          [self updateTotalValue:textField.text];
      }];
 }];
[alert addAction:ok];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                         {
                             [alert dismissViewControllerAnimated:YES completion:nil];
                         }];
[alert addAction:cancel];
[self.navigationController presentViewController:alert animated:YES completion:nil];
  

ERoor:分配给' id _Nullable'来自不兼容的类型' ApprovalDetailsViewController * const __strong'

在addtextfieldconfiguration中我想做我的委托方法,但它会有一些错误      请任何人举个例子。

1 个答案:

答案 0 :(得分:3)

您可以按照以下方式实施 -

-(void)showAlert {

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Total Value"  message:@""  preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                         {
                             [alert dismissViewControllerAnimated:YES completion:nil];
                         }];
    [ok setEnabled:NO];

    __weak typeof(self) weakSelf = self;

    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
     {
         __strong typeof(self) strongSelf = weakSelf;

         textField.placeholder = @"TotalValue";
         textField.enabled = YES;
         textField.delegate = strongSelf;
         [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidChangeNotification object:textField queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
          {
              textField.text.length >0 ? [ok setEnabled:YES]: [ok setEnabled:NO];
          }];
         [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:textField queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
          {
              [strongSelf upadteTotalValue:textField.text];
          }];
     }];
    [alert addAction:ok];




    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                             {
                                 [alert dismissViewControllerAnimated:YES completion:nil];
                             }];
    [alert addAction:cancel];


    [self.navigationController presentViewController:alert animated:YES completion:nil];
}
-(void)upadteTotalValue:(NSString *)text {
    NSLog(@"%s",__FUNCTION__);
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"%s",__FUNCTION__);
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"%s",__FUNCTION__);

}