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屬性,但是沒有成功。第二個頁面什么也沒有顯示。

總結原因應該是第二個頁面在viewDidLoad的時候已經生成好了,所以修改沒有起作用。所以直接傳模型數據,在第二個頁面的viewDidLoad的時候自己加載設置數據。

-(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方法

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

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM