【发布时间】: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!
我尝试了一些没有帮助的方法:
添加一个
viewDidAppear方法实现,其中包含我的 segue 代码,并在我的 viewDidLoad 的 if 语句中调用[self viewDidAppear]。将我的 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