无法使用segue与viewDidLoad或viewDidAppear一起使用

时间:2014-03-13 08:55:48

标签: ios iphone objective-c uiviewcontroller viewdidload

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

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

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

我尝试了几件没有帮助的事情:

  1. 使用我的segue代码添加viewDidAppear方法实现,并在我的viewDidLoad&#39; s if语句中调用[self viewDidAppear]

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

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

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

    - (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);
    

2 个答案:

答案 0 :(得分:0)

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];
});;

答案 1 :(得分:0)

  1. 一定要(在你的storyBoard中),从当前的ViewController到destinationVC定义一个名为addFriendsToMediaCaptureSegue的segue。
  2. 您可以尝试移动您的performSegueWithIdentifier:像这样

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