简体   繁体   中英

Change value of one view controllers NSSdictonary from other view controller using custom delegate in ios

I have a dictonary in one view controller with different keys.i have to change the value of one of the key of this dictonary from other view controller after some method. How can i do this using custom deleagate.

Add a protocol with a method for Your needs. Make the class that holds the dictionary to conform to the protocol . Implement the method in question and edit Your dict in it. In the other class, add a @property (weak, nonatomic) id <MyProtocol> delegate or some other appropriate name. When needed call the protocol via the stored delegate .

@protocol MyProtocol <NSObject>
  - (void)callback;
@end

@interface MyClass : NSObject <MyProtocol>
@property (nonatomic, copy) NSMutableDictionary *myDictionary;
@end

@implementation MyClass
// class methods

  -(void)callback {
    // edit dict here
  }

@end

@interface MySecondClass : NSObject
@property (weak, nonatomic) id<MyProtocol> delegate;
@end

@implementation MySecondClass
// class methods

- (void)someMethod {
  // some logic
  if ([self.delegate respondsToSelector:@selector(callback)]) {
    [self.delegate callback];
  }  
}
@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM