【问题标题】:Cannot get segue to work with viewDidLoad or viewDidAppear无法使 segue 与 viewDidLoad 或 viewDidAppear 一起工作
【发布时间】:2014-03-13 08:55:47
【问题描述】:

对于我的视图控制器的 viewDidLoad 方法实现,它要求用户访问地址簿。我需要这样做,以便如果用户点击警报视图窗口上的“不允许”按钮,那么我们会将它们转移到另一个视图控制器。

我的问题是 segue 永远不会发生并在控制台中显示此输出:

Warning: Attempt to present <MediaCaptureVC: 0x156788c0> on <AddFriendsViewController: 0x15670dd0> whose view is not in the window hierarchy!

我尝试了一些没有帮助的方法:

  1. 添加一个 viewDidAppear 方法实现,其中包含我的 segue 代码,并在我的 viewDidLoad 的 if 语句中调用 [self viewDidAppear]

  2. 将我的 segue 代码移动到 viewDidLoad 的最底部。

但是没有任何效果。即使视图在技术上正在加载,我也需要一种方法来触发 segue,因为如果他们拒绝访问他们的地址簿,那么他们绝对没有理由留在视图控制器上。

这是我的viewDidLoad 方法实现的开始,我要求访问地址簿,然后我的if(accessGranted == NO) 的if 语句包含我的[self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self]; 的segue 代码

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

[self.navigationController setNavigationBarHidden:YES];

self.currentUser = [PFUser currentUser];



self.tableView.dataSource = self;
self.tableView.delegate = self;


//Asks for access to Address Book.

ABAddressBookRef m_addressbook =  ABAddressBookCreateWithOptions(NULL, NULL);



ABAddressBookCopyArrayOfAllPeople(m_addressbook);


__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @autoreleasepool {
            // Write your code here...
            // Fetch data from SQLite DB
        }
    });

    ABAddressBookRequestAccessWithCompletion(m_addressbook, ^(bool granted, CFErrorRef error)

    {
        accessGranted = granted;

        NSLog(@"Has access been granted?: %hhd", accessGranted);


        if(accessGranted == NO) {

            [self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self];


        }

        NSLog(@"Has there been an error? %@", error);


        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

【问题讨论】:

  • 您是否覆盖了prepareForSegue:
  • 不,我没有,不确定我是否理解。我所做的只是“[self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self];"
  • 你试过presentViewController:animated:completion:吗?
  • 你已经在故事板中声明了segue identifier
  • 把你的代码写在viewDidApear,不要在viewDidLoad[self viewDidApear]

标签: ios iphone objective-c uiviewcontroller viewdidload


【解决方案1】:
  1. 确保(在您的故事板中)有一个从当前 ViewController 到 destinationVC 的已定义 segue,名为 addFriendsToMediaCaptureSegue。
  2. 你能试着移动你的 performSegueWithIdentifier: 像这样

    dispatch_async(dispatch_get_main_queue(), ^{ [self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self]; });

【讨论】:

  • 您可以尝试在您的 VC 中添加一个按钮,并在单击此按钮时执行 segue 吗?只是看看你的错误是否与代码在哪里有关。
【解决方案2】:

在你的viewDidLoad:

double delayInSeconds = 0.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    [self presentViewController:vc animated:YES completion:nil];
});;

【讨论】:

  • 这会在控制台中引发异常:“在捆绑包 NSBundle 中找不到名为 'MainStoryboard' 的故事板”我什至尝试将其更改为“Main.storyboard”,就像它在我的项目导航器中显示的那样。
  • 尝试使用“Main”而不是“Main.stroyboard”
  • 你不必写Main.storyboard,如果你的bundle中有Main.Storyboard,只写Main
  • 我终于让你的代码完美运行,但它仍然给出了与我原来的问题相同的错误。
  • 编辑了答案现在检查一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 2015-01-25
相关资源
最近更新 更多