2012-01-09 64 views
2

我需要在presentMoalViewController之後訪問parentViewController。 這是我的方法,我在firstViewController調用查看secondViewController:如何在presentModalViewController之後訪問parentViewController?

- (void)viewData { 

    SecondViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"select_data"]; 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl]; 
    navigationController.navigationBar.barStyle = UIBarStyleBlack; 

    UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(saveData)] autorelease]; 
    viewCtrl.navigationItem.rightBarButtonItem = salvaButton; 

    UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:@"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:@selector(backView)] autorelease]; 
    viewCtrl.navigationItem.leftBarButtonItem = annullaButton; 

    [self presentModalViewController:navigationController animated:YES]; 

} 

當我點擊saveButton,我嘗試進入parentViewController以這種方式,但它不工作:

- (void) saveData { 

    FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController]; 
    parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"]; 
    [parentView performSelectorInBackground:@selector(myMethod) withObject:nil]; 

    [self dismissModalViewControllerAnimated:YES]; 

} 
+0

當myMethod調用你必須做的事情時,我沒有得到? – 2012-01-09 12:22:43

+0

我必須重新加載我的tableView – Dany 2012-01-09 12:33:20

回答

0

對於navigationController你可以聲明一個屬性稱爲像myController,那麼你必須開始你的navigationController這樣的:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl]; 
navigationController.navigationBar.barStyle = UIBarStyleBlack; 
navigationController.myController = self; 

UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(saveData)] autorelease]; 
viewCtrl.navigationItem.rightBarButtonItem = salvaButton; 

UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:@"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:@selector(backView)] autorelease]; 
viewCtrl.navigationItem.leftBarButtonItem = annullaButton; 

[self presentModalViewController:navigationController animated:YES]; 

在你parentViewController類聲明的方法

- (void) saveData { 

FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController]; 
parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"]; 
[parentView performSelectorInBackground:@selector(myMethod) withObject:nil]; 

[self dismissModalViewControllerAnimated:YES]; 
} 

在你navigationController那麼你可以調用[myController saveData]; 這應該工作,如果你有任何問題,請隨時問!

+0

我不明白如何聲明myController – Dany 2012-01-09 13:18:30

+0

問題是,[自導航控制器] presentsViewController]返回UITabBarViewController – Dany 2012-01-09 13:19:47

1

如果您在第二個viewController中使用第一個viewController可以將其自身設置爲的委託/協議,會更好。你可以在這裏找到更多的信息Pass data back to the previous controller或只是做一個搜索。這種設計模式幾乎可以在iOS編程中使用。

最大的好處是,你的第二個viewController變得獨立於任何呈現它的人,並且可以很容易地重用。技術術語將是「脫鉤」。

0

您可以訪問使用以下各項

- (void) saveData { 
NSMutableArray *activeControllerArray = [self.navigationController.viewControllers mutableCopy]; 
FirstViewController *parentView; 
for(int i = 0, i <[activeControllerArray count], i++) { 
    if([[activeControllerArray objectAtIndex:i] isKindOfClass:[FirstViewController class]) { 
     parentView= [activeViewController objectAtIndex:i]; 
     break; 
    } 
} 

parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"]; 
[parentView performSelectorInBackground:@selector(myMethod) withObject:nil]; 

[self dismissModalViewControllerAnimated:YES]; 

} 
+0

感謝您的迴應,但activeControllerArray只包含secondViewController :( – Dany 2012-01-10 10:10:35

1

上次我這樣做我用您的通知父視圖控制器:

[[NSNotificationCenter defaultCenter] postNotificationName:@"saveData" object:dataString]; 
在父VC視圖didload

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveDataNow:) name:@"saveData" object:nil]; 
相關問題