2012-07-17 59 views
0

設置一個的NSString我有一個使用主模板細節,主視圖通過一個NSString的id到一個子視圖的子視圖在一個TextView用戶可以編輯顯示IOS應用程序。我想要做的是,當按下按鈕從子視圖返回到主視圖時,將傳入的NSString的文本設置爲用戶將其更改爲的任何內容。這可能嗎?從傳入ID

回答

2

我的答案是不傳遞一個字符串到詳細信息視圖。傳遞一個模型。主機和細節之間的數據必須有一些覆蓋模型。通過該模型,更新的詳細型號,那麼當你去回主,在模型中的更改會自動反映在主。

下面是一個例子假設主包含的對應於的tableView的細胞模型中的數組。然後,在詳細信息視圖,當你改變self.model.myString母版視圖也將被更新。

@interface MySimpleModel 
@property (copy, nonatomic) NSString *myString; 
@end 

...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"To Detail From Master"]) 
    { 
     // Get which indexPath of the cell in the master to use. 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)cell]; 

     // Get reference to the destination view controller 
     DetailViewController *vc = [segue destinationViewController]; 

     // Pass any objects to the view controller here, like... 
     vc.model = (MySimpleModel *)[self.models objectAtIndex:indexPath.row]; 
    } 
} 
+0

林相當新的Objective-C中,通過模型,你的意思類?或者是不同的東西? – Whyrusleeping 2012-07-17 04:26:37

+0

@whyrusleeping你可能想要了解MVC模式(http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) - 它在iOS中幾乎被強制執行,所以你現在應該熟悉它。是的,他的意思是你應該傳遞一個類,而不僅僅是字符串。 – RonLugge 2012-07-17 04:29:00