2013-04-11 148 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()當你完成。