2013-04-11 38 views
4
- (BOOL)peoplePickerNavigationController: 
(ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson:(ABRecordRef)person { 

     DefaultContactSelectViewController *view = [[self storyboard] instantiateViewControllerWithIdentifier:@"DefaultContactView"]; 
     view.recordID = recordID; 
     view.phones = phones; 
     view.emails = emails; 
     view.first_name = first_name; 
     view.last_name = last_name; 
     view.delegate = self; 

     [peoplePicker pushViewController:view animated:YES]; 
    return NO; 
} 

在上面的代码示例中,我在选择联系人后推送自定义联系人视图控制器。问题是如果从搜索结果中选择联系人,然后用户单击回到联系人选择器,搜索结果将被清除。ABPeoplePickerNavigationController shouldContinueAfterSelectingPerson返回搜索结果

如果上面的代码返回YES,则不会发生此问题,但是它会推送默认的联系人视图,这不是我想要的。

如果您知道如何解决此问题,请提前致谢。

回答

2

您应该编写一个自定义的PeoplePickerViewController,因为您永远无法控制Apple的默认控制器。

无论如何,你目前的问题,这里就是你需要做什么:

声明(根据如果您正在使用ARC或不使用合适的声明 - 我假设没有ARC)三个新属性

@property (nonatomic, assign) ABPeoplePickerNavigationController *peoplePicker; 
@property (nonatomic, assign) UIViewController *peoplePickerRootViewController; 
@property (nonatomic, copy) NSString *currentSearchString; 

现在,当你显示人们选择器,添加这些行:

// save people picker when displaying 
self.peoplePicker = [[[ABPeoplePickerNavigationController alloc] init] autorelease]; 

// save it's top view controller 
self.peoplePickerRootViewController = self.peoplePicker.topViewController; 

// need to see when view controller is shown/hidden - viewWillAppear:/viewWillDisappear: won't work so don't bother with it. 
self.peoplePicker.delegate = self; 

现在,我们可以节省搜索字符串只是推人视图之前:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{ 
    self.currentSearchString = nil; 
    if ([self.peoplePickerRootViewController.searchDisplayController isActive]) 
    { 
     self.currentSearchString = self.peoplePickerRootViewController.searchDisplayController.searchBar.text; 
    } 
    // other stuff... 

显然,在这个类中实现UINavigationControllerDelegate。当根视图回到视图中时,我们将强制显示搜索结果视图。下面是navigationController:willShowViewController:animated:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{ 
    if (navigationController == self.peoplePicker) 
    { 
     if (viewController == self.peoplePickerRootViewController) 
     { 
      if (self.currentSearchString) 
      { 
       [self.peoplePickerRootViewController.searchDisplayController setActive: YES]; 
       self.peoplePickerRootViewController.searchDisplayController.searchBar.text = self.currentSearchString; 
       [self.peoplePickerRootViewController.searchDisplayController.searchBar becomeFirstResponder]; 
      } 
      self.currentSearchString = nil; 
     } 
    } 
} 

实施别忘了如果不使用ARC在dealloc中释放currentSearchString。

小警告:当ABPeoplePickerNavigationController尝试隐藏搜索结果视图时,您选择一个人时会有轻微闪烁。

+0

这工作,谢谢。我也保存了搜索结果的滚动位置,并添加:[self.peoplePickerRootViewController.searchDisplayController.searchResultsTableView setContentOffset:scrollPosition animated:NO]; tonavigationController“willShowViewController”,但设置滚动似乎只能在“didShowViewController”中工作,这并不理想。任何想法为什么它不会在willShowViewController中设置滚动? – 2013-05-07 02:41:34

0

好吧,我有一个类似的问题。我认为你正在使用ARC?

如果让我保存和整体ABRecordRef传递给我的其他视图,然后不得不使用保留人物对象:

CFRetain(m_MyContact object); 

不要忘了,然后在对象上使用CFRelease()当你完成。