워크 플로우가 단순한 경우 매우 간단합니다. 이 메소드는 Menu ViewController라고 부르는 것에서 직접 다른 ViewController의 모든 레이블을 변경합니다.
의 당신이 다음과 같은 상황을 가정 해 봅시다 :
파란색 ViewController
는 FirstViewController 클래스이다. 초록색 ViewController는
SecondViewController
클래스입니다. 각 레이블은 해당 클래스의 헤더 파일에있는
firstVCLabel
및
secondVCLabel
속성에 의해 참조됩니다. 이 ViewController에는 "모달"버튼이 있습니다.이 버튼은 모달에 터치하여 위로 움직입니다.
이렇게 두 버튼 중 하나를 클릭하면 주황색 ViewController (ModalViewController
클래스)가 표시됩니다. 이 ViewController에는 changeLabel:
및 back:
이라는 IBActions 내부에 연결되는 두 개의 버튼 "Change Label"과 "Back"이 있습니다.여기 FirstViewController.h
은,
#import "ModalViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface ModalViewController()
@end
@implementation ModalViewController
// Action linked to the "Change Label" button
- (IBAction)changeLabel:(id)sender {
// Access the presenting ViewController, which is directly the TabBarController in this particular case
// The cast is simply to get rid of the warning
UITabBarController *tabBarController = (UITabBarController*)self.presentingViewController;
// Go through all the ViewControllers presented by the TabBarController
for (UIViewController *viewController in tabBarController.viewControllers) {
// You can handle each ViewController separately by looking at its class
if ([viewController isKindOfClass:[FirstViewController class]]) {
// Cast the ViewController to access its properties
FirstViewController *firstVC = (FirstViewController*)viewController;
// Update the label
firstVC.firstVCLabel.text = @"Updated first VC label from Modal";
} else if ([viewController isKindOfClass:[SecondViewController class]]) {
SecondViewController *secondVC = (SecondViewController*)viewController;
secondVC.secondVCLabel.text = @"Updated second VC label from Modal";
}
}
}
// Action linked to the "Back" button
- (IBAction)back:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
완성도를 위해서 :
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *firstVCLabel;
@end
그리고 SecondViewController.h
: 아무 관련이 없다
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *secondVCLabel;
@end
여기
은
ModalViewController
코드입니다 이러한 클래스의 구현에있어서의 코드