我可以通过NSObject
将数据从表传递到其他表吗?
示例:
第一张表发送数据
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Data *g = [[Data alloc]init];
[g setSelection1:(int)path.row];
}
NSLog
test selecttion1 value = path.row;
当我使用第二个表时,我导入数据并分配它,但我在selection1中的值丢失了。
总是0;
答案 0 :(得分:0)
您的Data对象不会为您提供服务,因为您尚未将其传递给目标视图控制器。
你可以这样做
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Data *g = [[Data alloc]init];
[g setSelection1:(int)path.row];
MyTargetViewController *targetVC = segue.destinationViewController;
[targetVC setData:g];
}
这里将“MyTargetViewController”替换为您的实际目标视图控制器。
答案 1 :(得分:0)
试试这个:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"<YOUR_SEQUE_IDENTIFIER_GOES_HERE>"])
{
<YOUR_CONTROLLER> *destController = (<YOUR_CONTROLLER> *)
segue.destinationViewController;
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Data *g = [[Data alloc] init];
//if you would like to re-use this data source, I would suggest making this as a
//property within this class
[g setSelection1:(int)path.row];
[destController setMyData:g];
}
}
在第二个表格中,声明一个属性:
@property (strong, nonatomic) Data *myData;