delegation pattern을 사용해야합니다. 당신은 제어기 A에서 대기 작업을해야하지만, 대신에 B에서 변경된 값에 응답의 조치는 B에 의해 트리거 될 때 적절한
ViewControllerB.h
// Create delegate protocol and property
@protocol ViewControllerBDelegate <NSObject>
- (void)switchPressed:(BOOL)switchStatus;
@end
@interface ViewControllerB : NSObject
@property (nonatomic,weak) id<ViewControllerBDelegate> delegate;
@end
ViewControllerB.m
// When switch is tapped, call delegate method if it is implemented by delegate object
- (IBAction)flip: (id) sender {
UISwitch *onoff = (UISwitch *) sender;
if ([self.delegate respondsToSelector:@selector(switchPressed:)]) {
[self.delegate switchPressed:onoff.on];
}
}
ViewControllerA.h
// Conform to ViewControllerB protocol
#import ViewControllerB.h
@interface ViewControllerA : NSObject,ViewControllerBDelegate
ViewControllerA.m
// Set self (VC A) as VC B's delegate
- (void)ViewDidLoadOrSomeOtherFunction {
ViewControllerB *vcB = [[ViewControllerB alloc] init];
vcB setDelegate = self;
}
// Implement delegate method
- (void)switchPressed:(BOOL)switchStatus {
if (switchStatus) {
// Make changes on VC A
}
}