在iOS 9中,Present View Controller无法正常工作。

5

当前视图控制器在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

你为什么出现了两次时间? - vien vu
是的,它返回实例@ProblemSlover。 - Aarti Oza
1
@Florence 你能把你的完整代码放到Pastebin上吗? - ProblemSlover
1
我想知道为什么它不起作用。你想要团队查看还是发送源代码? :P @Florence - Teja Nandamuri
1
@Florence 我怀疑控制器没有被初始化。请再次设置调试点在你的操作方法内,确保视图控制器实例已经被初始化,并且通过以下方式将其转换为 RegistrationViewController 的实例: (RegistrationViewController*)[[UIStoryboard storyboardWithName:@.... - ProblemSlover
显示剩余23条评论
3个回答

13

确保 presentedViewController 为 nil。如果 presentedViewController 不是 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];

}

在呈现视图控制器时,你有收到任何警告吗? - Surya Subenthiran

1
尝试在viewDidAppear中查看。
  -(void)viewDidAppear:(BOOL)animated{ 
         [self presentViewController:viewController animated:YES completion:nil];
    }

2
这样的方法中,总是要调用super。 - Nat

-1

尝试以下代码:

RegistrationViewController * viewController = [[self.storyboard instantiateViewControllerWithIdentifier:@"RegistrationViewController"];

[self presentViewController:viewController animated:YES completion:nil];

我认为你正在呈现已经存在的同一个视图控制器。 - Balaji Kondalrayal
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Aarti Oza
不必要求登录和注册视图控制器在同一个故事板中。@balaji - Ratul Sharker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接