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/