Ios UIAlertViewController添加文本字段委托
我已经在UIAlertViewController中实现了textfield,但我希望实现对该textfields的委托方法 ERoor:从不兼容的类型“ApprovalDetailsViewController*const\u strong”分配给“id\u Nullable” 在addtextfieldconfiguration中,我想执行委托方法,但它会出现一些错误Ios UIAlertViewController添加文本字段委托,ios,objective-c,iphone,xcode,uitextfield,Ios,Objective C,Iphone,Xcode,Uitextfield,我已经在UIAlertViewController中实现了textfield,但我希望实现对该textfields的委托方法 ERoor:从不兼容的类型“ApprovalDetailsViewController*const\u strong”分配给“id\u Nullable” 在addtextfieldconfiguration中,我想执行委托方法,但它会出现一些错误 请任何人举个例子。您可以像下面这样实现- -(void)showAlert { UIAlertController
请任何人举个例子。您可以像下面这样实现-
-(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__);
}
您可以像下面这样实现-
-(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__);
}
你说的“会有一些错误”是什么意思?有错误信息吗?然后请将它们添加到您的问题中我将添加请检查您是否已将UITextFieldDelegate导入到您的viewController?[alert addTextFieldWithConfigurationHandler:^(UITextField*_nonNullTextField)通过这一点,我们显示了文本字段数组知道我不知道如何为该文本字段提供委托方法您所说的“将出现一些错误”是什么意思?是否有错误消息?然后请将它们添加到您的问题中我将添加请检查是否已将UITextFieldDelegate导入到您的viewController?[alert addTextFieldWithConfigurationHandler:^(UITextField*_nonNullTextField)由此,我们显示了文本字段数组,我不知道如何为该文本字段提供委托方法