当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - prepareForSegue is passing value by Reference to Destination view Controller

问题描述:

I am using storyboard in one of my application and while passing a property from SourceViewController to DestinationViewController in prepareForSegue method the property being passed is passed by reference instead of passed by value. Which is causing some trouble for me as in Destination View Controller if user fiddles with the values and taps on back then I get an updated value in my SourceViewController which I do not want.

Please see the following to get more information on the issue

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{if([[segue identifier] isEqualToString:@"DestinationView"])

{

DestinationViewController *destinationViewController = [segue destinationViewController];

[destinationViewController setActivity:activity];

//Here the value of activity is passed by reference. I have verified the same by checking the address of activity variable in Variable pane of Xcode in Source and destination view controller

}

Now I am not sure how to only pass the value and not the reference in the above case.

网友答案:

try to use a copy of it

    [destinationViewController setActivity:[activity copy]];

Implement its delegate copywithZone if needed

- (id)copyWithZone:(NSZone *)zone
分享给朋友:
您可能感兴趣的文章:
随机阅读: