Passing model objects from one view controller to another in a navigation stack

前端 未结 5 1316
刺人心
刺人心 2021-01-24 04:58

I have two UITableViewControllers. One displays a list of names and on tapping any cell will push the second TableViewController which enables the user to edit the name in a UIT

5条回答
  •  渐次进展
    2021-01-24 05:45

    Create a mutable array property in the first controller, and pass that array and an index to the second controller.

    FirstController.h

       @property (nonatomic,retain)     NSMutableArray *myStrings;
    

    FirstController.m

       @synthesize myStrings;
    
       init {
             self.myStrings = [NSMutableArray arrayWithCapacity:8];
       }
    
    
       didSelectRowAtIndexPath {
    
         SecondVC *vc = [[SecondVC new];
         [self.theStrings addObject:@"Original String"]; // or replaceAtIndex: indexPath.row
         vc.theStrings = self.myStrings;
         vc.theIndex   = indexPath.row;
         //push detail vc.
       }
    

    SecondController.h

      @property (nonatomic, retain) NSMutableArray *theStrings;
      @property (nonatomic        ) int             theIndex;
    

    SecondController.m

      @synthesize theStrings;
      @synthesize theIndex;
    
      doneEditingMethod {
           [self.theStrings replaceObjectAtIndex: self.theIndex withObject: myNewString];
       }
    

提交回复
热议问题