![](/img/trans.png)
[英]Dismissing a View Controller, then presenting an Alert Controller
[英]Alert view not dismissing on 8.4
情境
1)我開始編輯鍵盤來了。
2)然后我觸摸一個按鈕
3)在botton touch上,我添加了AlertView,然后添加我已經辭職了第一響應者
4)單擊AlertView OK按鈕,我會彈出viewController
5)彈出之后,鍵盤會出現在該屏幕上一段時間並關閉。
6)應該在同一控制器上而不是在先前的控制器上將其關閉
代碼-
- (IBAction)cancelSkipButtonTouchUpInside:(id)sender {
[self.textMobileNumber resignFirstResponder];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Entered number will not be saved" delegate:self cancelButtonTitle:nil otherButtonTitles: @"OK",@"Cancel", nil];
alertView.tag = ktagYourNumberWillNotBeSaved;
[alertView show];
alertView = nil ;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
case ktagYourNumberWillNotBeSaved:
{
[self.navigationController popToViewController:self animated:YES];
}
}
在這里,您被添加了self
這意味着您的導航堆棧調用了相同的ViewController,請避免選擇第1號和第2號
更改
[self.navigationController popToViewController:self animated:YES];
進入
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
否則選擇第二
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[ViewController2 class]]) // ViewController2 --> call your view controller where you want to pop
{
[self.navigationController popToViewController:vc animated:YES];
}
}
其他選擇3
[self.navigationController popViewControllerAnimated:YES]
您有兩種選擇
1)你可以改變
[self.navigationController popToViewController:self animated:YES];
進入
[self.navigationController popViewControllerAnimated:YES];
2)
for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[ViewController class]]) {
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
如果您只需要在點擊調用cancelSkipButtonTouchUpInside的按鈕時關閉鍵盤,請在其中使用以下行:
[self.view endEditing:YES];
我已經通過使用UIAlertController而不是UIAlertView解決了此問題,因為iOS 8已棄用
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *alertActionOk = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", @"Ok action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.navigationController popToViewController:self animated:YES];
}];
UIAlertAction *alertActionCancel = [UIAlertAction actionWithTitle:NSLocalizedString(CANCEL_ALERT_BUTTON, @"Cancel action") style:UIAlertActionStyleDefault handler:nil];
}
[alertController addAction:alertActionOk];
[alertController addAction:alertActionCancel];
[self presentViewController:alertController animated:YES completion:nil];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.