iOS 編輯頁面的實現方法,從一個頁面跳轉(push)到下一頁面時怎麼傳遞數據

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //這是一個包含text屬性的模型
    PGAddText *model = self.data[indexPath.row];
    //建立第二個控制器
    PGAddTextViewController *text = [[PGAddTextViewController alloc]init]; 
    //打算設置第二個頁面的textView的text屬性
    text.textView.text = model.text;
    [self.navigationController pushViewController:text animated:YES];
}

這是頁面一本來寫的代碼,打算設置第二個頁面的textView的text屬性,可是沒有成功。第二個頁面什麼也沒有顯示。markdown

總結緣由應該是第二個頁面在viewDidLoad的時候已經生成好了,因此修改沒有起做用。因此直接傳模型數據,在第二個頁面的viewDidLoad的時候本身加載設置數據。ui

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //這是一個包含text屬性的模型
    PGAddText *model = self.data[indexPath.row];
    //建立第二個控制器
    PGAddTextViewController *text = [[PGAddTextViewController alloc]init]; 
    //直接傳模型數據,在第二個頁面的viewDidLoad的時候本身加載設置數據 
    text.data = model;
    [self.navigationController pushViewController:text animated:YES];
}

第二個頁面的viewDidLoad方法spa

- (void)viewDidLoad {
    [super viewDidLoad];
    //設置數據
    self.textView.text = self.data.text;

}
相關文章
相關標籤/搜索