[英]Navigate to another View after OK Alert Button click without using segue
單擊確定按鈕時,我希望不使用segue將用戶重定向到另一個控制器。
// here is my alert
[[[UIAlertView alloc] initWithTitle:nil message:@"Profile details updated successfully." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show];
有什么想法請分享。
您可以嘗試此代碼,它適合您
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@""
message:@"Profile details updated successfully."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//put your navigation code here
// *** THIS IS WHERE YOU NAVIGATE TO LOGIN
[self presentViewController:alert animated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Put code for cancel here
}];
[alert addAction:ok];
[alert addAction:cancel];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0) //**Here is your ok button index**
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
LoginViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
}
由於以上方法在ios 8中 已棄用 ,因此您需要使用alertcontroller代替alertview ,如下所示
UIAlertController * alertController= [UIAlertController
alertControllerWithTitle:@"App name"
message:@"Update success"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
LoginViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
試試下面的代碼,
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"];
[self.navigationController pushViewController:controller animated:YES];
實現alertview委托:
@interface firstviewcontroller : UIViewController<UIAlertViewDelegate>{
}
設置您的代碼:AlertView中的委托= self;
-[[[UIAlertView alloc] initWithTitle:nil message:@"Profile details updated successfully." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show];
Alertview的委托方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.