2013-01-10 49 views
2

我試圖在已經推入的視圖控制器上像下面一樣推送視圖控制器。然而,不知何故,當我試圖推動newPersonViewController時,這個新控制器不會被推入堆棧。我不知道爲什麼Xcode抱怨我試圖添加一個不屬於層次結構的新控制器。我是否錯誤地提供了ABPeoplePickerViewController?這是newPersonViewController無法推入堆棧的原因嗎?從控制檯iOS如何在已經堆疊的ViewController上調用presentViewController

消息:

警告:試圖提出有關誰的觀點是不是在窗口層次!

的代碼:

- (void)addPerson 
{ 
    ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init]; 
    newPersonVC.newPersonViewDelegate = self; 
    UINavigationController *uiNav = [[UINavigationController alloc] initWithRootViewController:newPersonVC]; 

    [self presentViewController:uiNav animated:YES completion:nil]; 
} 

- (void)showPeoplePicker 
{ 
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; 
    picker.peoplePickerDelegate = self; 

    [self presentViewController:picker animated:YES completion:nil]; 
    [picker.topViewController setEditing:YES]; 
    picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)]; 
    picker.topViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(peoplePickerCancel)]; 
} 
+0

的問題的根源是'的存在[自presentViewController:...]''中因爲addPerson' 'self'不是當前的視圖控制器。只要改變這是新的財產,你就完成了!對不起,用編輯來折磨你! – Rob

回答

1

的問題是,addPerson正試圖從視圖控制器,這不是電流控制器模態呈現。 self(您的原始視圖控制器)不是主動控制器。 ABPeoplePickerNavigationController是。

因此,首先,定義一個類屬性來跟蹤picker的:

@property (nonatomic, weak) ABPeoplePickerNavigationController *picker; 

然後,

  • 修改showPeoplePicker設置該屬性;和
  • 修改addPerson使用該屬性:

因此:

- (void)addPerson 
{ 
    ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init]; 
    newPersonVC.newPersonViewDelegate = self; 
    UINavigationController *uiNav = [[UINavigationController alloc] initWithRootViewController:newPersonVC]; 

    [self.picker presentViewController:uiNav animated:YES completion:nil]; 
} 

- (void)showPeoplePicker 
{ 
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; 
    picker.peoplePickerDelegate = self; 

    [self presentViewController:picker animated:YES completion:nil]; 
    [picker.topViewController setEditing:YES]; 
    picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)]; 
    picker.topViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(peoplePickerCancel)]; 

    self.picker = picker; 
} 

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person 
{ 
    [newPersonView dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

Hi Rob,非常感謝!現在正在工作。我也在考慮選擇錯誤的主動視圖控制器,但不知道如何解決它。早先我嘗試: '[self.presentingViewController presentViewController:uiNav動畫:是完成:無];' 和 '[self.presentingViewController.navigationViewController presentViewController:uiNav動畫:YES完成:無];' 爲好,但它沒有工作。所以,我認爲調用'self.presentingViewController'會給我當前顯示的視圖控制器。 – Kann

相關問題