简体   繁体   English

iOS 8:ABPeoplePickerNavigationController在实现人员选择器委托方法时被解雇

[英]ios 8 : ABPeoplePickerNavigationController dismiss on implementing people picker delegate methods

This is strange behavior noticed while accessing contact details from address book in ios 8. My scenario is simple 这是从ios 8中的通讯录访问联系人详细信息时发现的奇怪行为。我的情况很简单

  1. Show contacts table 显示联系人表
  2. select a row that will invoke didSelectPerson method 选择将调用didSelectPerson方法的行
  3. in didSelectPerson method 在didSelectPerson方法中
  4. push SecondViewController 推送SecondViewController

     - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person; { SecondViewController *detailVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; [detailVC.view setBackgroundColor: [UIColor redColor]]; // [peoplePicker.navigationController pushViewController:detailVC animated:YES]; [peoplePicker pushViewController:detailVC animated:YES]; } 

but what happens is ABPeoplePickerNavigationController dismiss. 但是发生的是ABPeoplePickerNavigationController关闭。 Please enlighten me on this. 请对此给予启发。

I don't know the philosophy thing what happens under the hood of the " didSelectPerson " method, me was facing the same problem today. 我不知道在“ didSelectPerson ”方法的幕后发生了什么,今天我也面临着同样的问题。 I found a simple solution to overcome this issue, i override the " -(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion " method of the " ABPeoplePickerNavigationController ". 我找到了解决此问题的简单解决方案,我重写了“ ABPeoplePickerNavigationController ”的“ -(void)dismissViewControllerAnimated:(BOOL)flag补全:(void(^)(void))completion ”方法。 Then implement it like somewhat following 然后像下面这样实现它

    bool dismissedEnabled;
   -(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
   {
     if (dismissedEnabled) {
       [super dismissViewControllerAnimated:flag completion:completion];
     }
   }

then inside the " didSelectPerson " i have done the following 然后在“ didSelectPerson ”里面,我做了以下

   viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];

    dismissedEnabled = false;
    [self presentViewController:viewController animated:YES completion:nil];

this works for me, hope you guys overcome it too :) 这对我有用,希望你们也克服它:)

It automatically dismisses if you select a contact with a single email address for example. 例如,如果您选择一个电子邮件地址的联系人,它将自动关闭。 If a contact has more than one email, you must specify a predicate that will force the ABPeoplePickerNavigationController to push a ABPersonViewController on the stack. 如果联系人有多个电子邮件,则必须指定一个谓词,该谓词将强制ABPeoplePickerNavigationController将ABPersonViewController推入堆栈。

if ([picker respondsToSelector:@selector(setPredicateForSelectionOfPerson:)])
    {
        // The people picker will select a person that has exactly one email address and call peoplePickerNavigationController:didSelectPerson:,
        // otherwise the people picker will present an ABPersonViewController for the user to pick one of the email addresses.
        picker.predicateForSelectionOfPerson = [NSPredicate predicateWithFormat:@"emailAddresses.@count = 1"];
    }

I believe the default behavior in iOS 8 is that the ABPeoplePickerNavigationController is automatically dismissed when didSelectPerson is called. 我相信iOS 8中的默认行为是,在调用didSelectPerson时,会自动关闭ABPeoplePickerNavigationController

The reason that the SecondViewController is not displayed (I'm inferring that this is the problem symptom) is because you are trying to push the SecondViewController while the ABPeoplePickerNavigationController is being dismissed. 未显示SecondViewController的原因(我推断这是问题症状),是因为您正在尝试在SecondViewController ABPeoplePickerNavigationController同时推动SecondViewController This overlapping animation is a problem that the iOS core view management/animation system tries to avoid. 这种重叠的动画是iOS核心视图管理/动画系统试图避免的问题。

You may get a warning in the console when this happens. 发生这种情况时,您可能会在控制台中收到警告。

@Ratul's solution works because it circumvents this default behavior. @Ratul的解决方案之所以有效,是因为它规避了这种默认行为。

In my scenario, my code sleeps a second before presenting a UIAlertController from within didSelectPerson . 在我的场景中,我的代码在从didSelectPerson内部呈现UIAlertController之前睡了一秒钟。 This is a hack that depends on the ABPeoplePickerNavigationController dismissal animation taking less than a second. 这是一个依赖于ABPeoplePickerNavigationController解雇动画的过程,耗时不到一秒钟。 For me, if this alert is not displayed, nobody would even notice this was a problem. 对我而言,如果未显示此警报,则没人会注意到这是一个问题。

If you want something more robust, you may want to override viewDidAppear to handle this special case (using a flag in your presenting view controller). 如果您想要更强大的功能,则可能需要重写viewDidAppear来处理这种特殊情况(使用呈现的视图控制器中的标志)。 But this gets a bit clumsy as well. 但这也有些笨拙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM