2014-10-01 61 views
1

我對Xcode比較陌生,並且試圖通過搜索找到答案,但沒有運氣。如何更改一個UILabel從另一個View Controller查看控制器?

我的應用程序有5個視圖控制器,V1到V5,它們嵌入在一個Tab Bar Controller中。每個視圖控制器都有一個segue和一個Setup菜單視圖控制器。該菜單更改了視圖控制器上的一些標籤。我使用委託來確保當您離開菜單時,調用菜單的視圖控制器會使用新設置進行更新。但是,這允許我只修改View Controller上稱爲菜單控制器的標籤,而不是其他4個標籤。

我工作形成故事板。是否有一種簡單的方法來設置V1,V2,V3,V4和V5上的UILabels(反之亦然),或者甚至更好的方法是在菜單視圖控制器中設置V1到V5上的標籤(該標籤沒有嵌入到標籤欄中控制器)?

我已經看到一些可以幫助here的東西,但是這對於我想要的來說似乎相當複雜。我需要的標籤更改非常簡單,並且都是預定義的。每次切換選項卡式應用程序中的選項卡時,是否都有方法被調用?類似於ViewDidLoad?

回答

0

我會做的是子選項卡欄控制器,並將其設置爲菜單視圖控制器的代表。從那裏,當標籤應該改變時,您可以更新,然後與5個選項卡進行通信並更新標籤。

或者,您可以使用NSNotifications讓所有5個視圖控制器知道設置何時更改。

最後,您可以將菜單設置添加到單例中,並讓所有視圖控制器觀察可以更改的各種屬性。

我需要的標籤更改非常簡單,都是預定義的。每次切換選項卡式應用程序中的選項卡時,是否都有方法被調用?類似於ViewDidLoad?

關於這個問題,你正在尋找的方法是viewWillAppear:viewDidAppear

0

如果您的工作流程也很簡單,這是一個非常簡單的解決方案。此方法直接從您所稱的Menu ViewController中更改不同ViewController中的所有標籤。

比方說,你有以下情況:

Storyboard

藍色ViewController是FirstViewController類的。綠色ViewController是SecondViewController類。每個標籤都由屬性firstVCLabelsecondVCLabel(在相應的類頭文件上)引用。這兩個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 

目前尚無相關代碼實現這些類。

3

這聽起來像是NSNotificationCenter的好時光。你將有你的MenuViewController產生與應該在其他視圖控制器更新新數據的通知:

// User has updated Menu values 
[[NSNotificationCenter defaultCenter] postNotificationName:@"MenuDataDidChangeStuffForLabels" object:self userInfo:@{@"newLabelValue" : labelText}]; 

在你的V1,V2等可以添加訂閱使用此代碼,這些通知您的viewDidLoad方法:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Subscribe to NSNotifications named "MenuDataDidChangeStuffForLabels" 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabelText) name:@"MenuDataDidChangeStuffForLabels" object:nil]; 
} 

,訂閱使用該代碼將調用updateLabelText方法隨時使用該名稱的通知刊登的MenuViewController任何對象。從該方法中,您可以獲取新的標籤值並將其分配給您的標籤。

- (void)updateLabelText:(NSNotification *)notification { 
    NSString *newText = notification.userInfo[@"newLabelValue"]; 
    myLabel.text = newText; 
} 
0

非常感謝很多傢伙,我對你的快速反應印象深刻。在這種特殊情況下,viewWillAppear中的伎倆:

- (void)viewWillAppear:(BOOL)animated 
{ [self AdaptLabels]; 
    NSLog(@"View will appear."); 
} 

每次選擇一個新的標籤頁時,它會更新標籤,在新的視圖,根據菜單設置一個全局變量,它們出現之前。非常快速和乾淨。感謝大家!

相關問題