在显示alertview时,键盘将自动出现在ios 8.3中

时间:2016-03-01 09:29:06

标签: ios objective-c keyboard

嗨,我是ios的新手,在我的应用程序中,当我点击它时,我添加了一个textfeild键盘出现,当我点击返回按钮时,我想显示alertview

当我点击alertview“确定”按钮时,我想隐藏键盘,但它没有隐藏请帮助我

我的代码: -

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
                                                    message:@"You must be connected to the internet to use this app."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [self performSelector:@selector(showAlertView) withObject:nil afterDelay:0.6];

    [alert show];

    return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSLog(@"Hello wolrd");
    [mainscrollview endEditing:YES];
}

2 个答案:

答案 0 :(得分:1)

试试这个

Direct UIView

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

NSLog(@"Hello wolrd");
[yourtextfield resignFirstResponder];
[self.view endEditing:YES];
}

<强>滚动型

创建一个tapGesture之类的

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    singleTap.cancelsTouchesInView = NO;
    singleTap.delegate = self;
    [yourScrollviewName addGestureRecognizer:singleTap];

//method like

 -(void)singleTap
 {
   [yourtextfield resignFirstResponder];
   [self.view endEditing:YES];

  }

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

NSLog(@"Hello wolrd");
 [self singleTap];
}

<强>更新

   -(BOOL)textFieldShouldReturn:(UITextField *)textField{




UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"No network connection textField"
                                                                          message:@"keyboard should not be open"
                                                                   preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                        style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction *action) {
                                                           [self singleTap];
                                                      }];

[alertController addAction:cancelAction];

[self presentViewController:alertController animated:YES completion:nil];
return YES;
}

答案 1 :(得分:0)

只需替换以下功能:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [maintextfeild resignFirstResponder];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
                                                        message:@"You must be connected to the internet to use this app."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        alert.delegate = self;

        [alert show];
    });


    return YES;
}