且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在导航堆栈中将模型对象从一个视图控制器传递到另一个视图控制器

更新时间:2023-11-27 19:07:34

在第一个控制器中创建一个可变数组属性,并将该数组和一个索引传递给第二个控制器.

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

FirstController.h

FirstController.h

   @property (nonatomic,retain)     NSMutableArray *myStrings;

FirstController.m

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

SecondController.h

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

SecondController.m

SecondController.m

  @synthesize theStrings;
  @synthesize theIndex;

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