1

いくつかの属性といくつかのメソッドを持つ Layer というオブジェクトがあります。

Layer を 2 番目の View Controller に渡す必要があります。

SecondVC *view = [self.storyboard instantiateViewControllerWithIdentifier:@"2VC"];
view.Layer = [[Layer alloc] initWithMapLayer:self.Layer];
view.delegate = self;

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:navController animated:YES];

SecondVC では、属性を変更することがあります。次に、デリゲートを介して Layer オブジェクトを返します。

-(void)done
{
    [self.delegate returnLayer:self.layer];

    [self dismissModalViewControllerAnimated:YES];
}

今私の問題は、最初のView ControllerのLayerオブジェクトにポインターを渡していることです。2番目のView ControllerでLayerを更新すると、最初のView ControllerのLayerオブジェクトも更新されます。

このため、変更されているかどうかわかりません (変更されている場合は、コードを実行する必要があります)。

Layer オブジェクトのコピーを作成し、最初のビュー コントローラの Layer オブジェクトへのポインタの代わりにそれを渡すにはどうすればよいですか?

編集:

私は2番目のinitメソッドを使用してみました:

-(id)initWithLayer:(Layer *)Layer
{
    if (self = [super init])
    {
        self.call = [[FunctionCall alloc] init];        
        self.HUD = [[MBProgressHUD alloc] init];

        self.Layers = [[NSMutableDictionary dictionaryWithDictionary:Layer.Layers] copy];
        self.nameList = [[NSArray arrayWithArray:Layer.nameList] copy];
    }
    return self;
}

うまくいきませんでした。

EDIT2:

試した

Layer *copyLayer = [self.myLayer copy];

layerView.myLayer = copyLayer;

エラーが発生しました

-[layer copyWithZone:]: unrecognized selector sent to instance 0xfc72c40
2012-06-12 11:15:28.584 Landscout[8866:1fb07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Layer copyWithZone:]: unrecognized selector sent to instance 0xfc72c40'

解決済み:

initWithLayer メソッドにディープ コピーを追加しました。

for (id key in layer.layers)
{
    [newLayers setValue:[[layer.layers objectForKey:key] mutableCopy] forKey:[key mutableCopy]];
}

for (id name in layer.nameList)
{
    [newList addObject:[name mutableCopy]];
}

これにより、Layer オブジェクトのコピーが得られます

4

1 に答える 1

6

copyオブジェクトに関数を実装する必要があります

Layer.m で

- (id)copy
{
    Layer *layerCopy = [[Layer alloc] init];

    //YOu may need to copy these values too, this is a shallow copy
    //If YourValues and someOtherValue are only primitives then this would be ok
    //If they are objects you will need to implement copy to these objects too
    layerCopy.YourValues = self.YourValues;
    layerCopy.someOtherValue = self.someOtherValue;

    return layerCopy;
}

今あなたの呼び出し関数で

//instead of passing self.Layer pass [self.Layer copy]
view.Layer = [[Layer alloc] initWithMapLayer:[self.Layer copy]];
于 2012-06-12T16:00:34.703 に答える