【问题标题】:UIAlertViewController adding textfield delegateUIAlertViewController 添加文本字段委托
【发布时间】:2018-07-15 01:54:36
【问题描述】:

我已经在 UIAlertViewController 中实现了文本字段,但我想为该文本字段实现委托方法。

`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:从不兼容的类型“ApprovalDetailsViewController *const __strong”分配给“id _Nullable”

在 addtextfieldconfiguration 中,我想做我的委托方法,但它会出现一些错误 请哪位能举个例子。

【问题讨论】:

  • “它会出现一些错误”是什么意思?有错误信息吗?那么请将它们添加到您的问题中
  • 我会添加请检查
  • 您是否已将 UITextFieldDelegate 导入到您的 viewController 中?
  • [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) 通过这个我们显示文本字段数组知道我不知道如何为该文本字段提供委托方法

标签: ios objective-c iphone xcode uitextfield


【解决方案1】:

你可以像下面这样实现 -

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多