2011-07-23 24 views
0

我有SendingController其推到堆棧NAV SendingDeatilsController(其中一個具有TableView中)。用戶應應TableView中選擇一個行(它由對號選中),我想通過此行的值(讓它會的NSString對象),將SendingController。UINavigationControllers:如何將值傳遞給堆棧中的更高(父級?)控制器?

我怎樣才能實現我的應用程序這種行爲? SendingDetailController(SDC的屬性parentController是指SC)的SendingController父項?

回答

1

如果要實現這一行爲,通過SendingDetailController上一個視圖控制器的引用。通過這種方式,詳細視圖控制器可以將消息發送到堆棧中的前一個消息。

在你SendingDetailController定義弱引用:

// in .h 
SendingController *sendingController; 
@property(assign) SendingController *sendingController; 

// in .m 
@synthesize sendingController; 

-(void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // retrieve the string and send the message 
    [sendingController didSelectString:theString]; 
} 

現在推SendingDetailController堆棧上不要忘記設置其屬性sendingController之前。

// .m 
// where you push the vc 
if(!sendingDetailController) { 
    sendingDetailController = [[SendingDetailController alloc] 
           initWithNibName:@"TheNIBName" 
             bundle:nil]; 
    sendingDetailController.sendingController = self; 
} 
[self.navigationController pushViewController:sendingDetailController 
            animated:YES]; 

並編寫接收字符串的方法。

-(void)didSelectString:(NSString *)aString { 
    // do anything with string 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

這應該可以完成這項工作。

+0

我應該釋放sendingController財產SendingDetailController的dealloc的? – LIAL

+0

@LIAL - 不,如您在屬性定義中所看到的那樣,該值只是分配的,而不是保留的。這是一個薄弱的參考。 – 2011-07-23 13:11:51

0

爲了便於不同UIViewControllers之間的異步通信,您可能需要查看NSNotificationNSNotificationCenter

有在網絡上大量的教程,這裏也有很好的答案在SO,可以告訴你如何做到這一點正好。