当前视图控制器不使用ios 9
当前视图控制器不能与ios 9一起使用,当我按下按钮时它没有重定向到当前视图控制器。
为什么会这样?
我曾尝试过以下代码
RegistrationViewController * viewController =[[UIStoryboard storyboardWithName:@"Registration" bundle:nil] instantiateViewControllerWithIdentifier:@"RegistrationViewController"]; [self presentViewController:viewController animated:YES completion:nil];
我也试过下面的代码
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Registration" bundle:nil]; RegistrationViewController *add = [storyboard instantiateViewControllerWithIdentifier:@"RegistrationViewController"]; [self presentViewController:add animated:YES completion:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:add animated:YES completion:nil]; }); }];
编辑
我的完整代码在这里。 我正在使用Xcode 7并使用ios 9运行它
#import "LoginViewController.h" #import "RegistrationViewController.h" @interface LoginViewController () @end @implementation LoginViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)actionRegistrationClicked:(id)sender { RegistrationViewController * viewController =[[UIStoryboard storyboardWithName:@"Registration" bundle:nil] instantiateViewControllerWithIdentifier:@"RegistrationViewController"]; [self presentViewController:viewController animated:YES completion:nil]; } @end
注册视图控制器
#import "RegistrationViewController.h" @interface RegistrationViewController () @end @implementation RegistrationViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
确保presentViewController为零。 如果presentViewController不是nil,则将代码更改为以下内容。
RegistrationViewController * viewController =[[UIStoryboard storyboardWithName:@"Registration" bundle:nil] instantiateViewControllerWithIdentifier:@"RegistrationViewController"]; if ([self presentedViewController]) { [[self presentedViewController] dismissViewControllerAnimated:NO completion:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:viewController animated:YES completion:nil]; }); }]; } else { [self presentViewController:viewController animated:YES completion:nil]; }
有时主循环会出现错误,尝试以这种方式呈现
RegistrationViewController *viewController = [[UIStoryboard storyboardWithName:@"Registration" bundle:nil] instantiateViewControllerWithIdentifier:@"RegistrationViewController"]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:viewController animated:YES completion:nil]; });
尝试viewDidAppear in
-(void)viewDidAppear:(BOOL)animated{ [self presentViewController:viewController animated:YES completion:nil]; }
请尝试以下代码:
RegistrationViewController * viewController = [[self.storyboard instantiateViewControllerWithIdentifier:@"RegistrationViewController"]; [self presentViewController:viewController animated:YES completion:nil];