ios - UIAlertViewController 添加文本字段委托(delegate)

标签 ios objective-c iphone xcode uitextfield

I have implemented textfield in UIAlertViewController but I want implement delegate methods to that 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 : Assigning to 'id _Nullable' from incompatible type 'ApprovalDetailsViewController *const __strong'

在 addtextfieldconfiguration 中,我想执行我的委托(delegate)方法,但它会出现一些错误 请谁能举个例子。

最佳答案

你可以像下面这样实现-

-(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__);

}

关于ios - UIAlertViewController 添加文本字段委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48618680/

相关文章:

ios - UIImage 下载返回 nil 并导致应用程序崩溃(Swift)

ios - 即使用户很久以前离线,XMPP Last Seen 总是返回 '0' 秒

ios - Xcode 6 Save for Enterprise Deployment 不再为 ipa 创建 plist?

ios - AWS iOS SDK AWSServiceManager 多种服务配置

iphone - 没有边框的圆角矩形 UIButton

ios - Q :Controller present 2 second ,如何让它看起来只有一个转换?

iphone - 我可以在 iPhone 应用程序中以编程方式发送电子邮件吗?

objective-c - 在 NSView 上使用 NSPoint 鼠标跟踪

c++ - 使用 AudioFileServices 在 iOS 6 中进行颗粒合成

iphone - 使用 "inserted"行初始化 UITableView