目前的视图控制器无法使用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
答案 0 :(得分:11)
确保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];
}
答案 1 :(得分:3)
有时主循环会出现错误,尝试以这种方式出现
RegistrationViewController *viewController = [[UIStoryboard storyboardWithName:@"Registration" bundle:nil] instantiateViewControllerWithIdentifier:@"RegistrationViewController"];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:viewController animated:YES completion:nil];
});
答案 2 :(得分:0)
尝试
中的viewDidAppear -(void)viewDidAppear:(BOOL)animated{
[self presentViewController:viewController animated:YES completion:nil];
}
答案 3 :(得分:-1)
请尝试以下代码:
RegistrationViewController * viewController = [[self.storyboard instantiateViewControllerWithIdentifier:@"RegistrationViewController"];
[self presentViewController:viewController animated:YES completion:nil];