如果您的工作流程也很簡單,這是一個非常簡單的解決方案。此方法直接從您所稱的Menu ViewController中更改不同ViewController中的所有標籤。
比方說,你有以下情況:
藍色ViewController
是FirstViewController類的。綠色ViewController是SecondViewController
類。每個標籤都由屬性firstVCLabel
和secondVCLabel
(在相應的類頭文件上)引用。這兩個ViewController都有一個「Modal」按鈕,它可以在屏幕上簡單地進行模式化。
因此,當你點擊這兩個按鈕中的任何一個時,橙色的ViewController(ModalViewController
類的)被呈現。這個ViewController有兩個按鈕,「更改標籤」和「後退」,這些按鈕被鏈接到IBAction內部,稱爲changeLabel:
和back:
。
下面是ModalViewController
代碼:
#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];
}
爲了完整起見,這裏有FirstViewController.h
:
#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
目前尚無相關代碼實現這些類。