OC UI學習中的筆記

1、當一個控制器上,有多個segue時,且在方法中定義了一個類爲另一個類的代理時,在prepare方法中需要判斷目標控制器爲哪個,並分段執行代碼,否則程序將出錯

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  YHedditViewCOntroller *eddit = segue.destinationViewController
  eddit.delegate  = self
}
調整添加後的代碼

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    //取得控制器
    UIViewController *vc = segue.destinationViewController;
    //判斷目標控制器的類型
    if ([vc isKindOfClass:[addViewController class]]) {
        addViewController *addVc = (addViewController *)vc;
        //設置代理
        addVc.delegate = self;
    }else if([vc isKindOfClass:[editViewController class]]){
        editViewController *edit = (editViewController *)vc;
        
        //獲取cell當前選中的行的行號
        NSInteger didselectrow = [self.tableView indexPathForSelectedRow].row;
        DataModel *data = self.ContactData[didselectrow];
        
        edit.data = data;
    }
    
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章