2014-10-01 9 views
1

저는 Xcode에 비교적 익숙하지 않고 검색하지 않고 답변을 찾으려고했습니다.다른보기 컨트롤러에서 UILabel 하나의보기 컨트롤러를 변경하는 방법?

내 앱에는 하나의 탭 표시 줄 컨트롤러에 포함 된 5 개보기 컨트롤러 V1 ~ V5가 있습니다. 각 View Controller에는 Segue to One과 동일한 Setup Menu View Controller가 있습니다. 메뉴는보기 컨트롤러에서 일부 레이블을 변경합니다. 대리자를 사용하여 메뉴를 호출 할 때 View Controller가 새 설정으로 업데이트되는지 확인합니다. 그러나 이것으로 메뉴 컨트롤러를 호출 한 View Controller의 레이블 만 수정할 수 있으며 다른 4 개의 어댑터는 수정할 수 없습니다.

저는 스토리 보드 형태로 작업합니다. V2, V3, V4 및 V5의 UILabels을 V1 (또는 그 반대로)에 설정하거나 더 좋게 설정하려면 메뉴보기 컨트롤러 (Tab Bar에 포함되지 않음)에서 V1에서 V5까지 레이블을 설정하십시오 제어 장치)?

나는 here을 도울 수있는 것을 보았습니다. 그러나 이것은 내가 원하는 것에 다소 복잡해 보입니다. 필요한 레이블 변경은 매우 간단하며 모두 미리 정의되어 있습니다. 탭 응용 프로그램에서 탭을 전환 할 때마다 호출되는 메서드가 있습니까? ViewDidLoad와 비슷합니까?

답변

0

내가 할 수있는 것은 탭 모음 컨트롤러를 서브 클래스로 만들고이를 메뉴보기 컨트롤러의 대리자로 설정하는 것입니다. 거기에서 레이블이 변경되고 5 탭과 통신하고 레이블을 업데이트해야 할 때 업데이트 할 수 있습니다.

또는 NSNotifications를 사용하여 설정이 변경되면 모든 5 개의보기 컨트롤러에 알릴 수 있습니다.

마지막으로 메뉴 설정을 싱글 톤에 추가하고 모든보기 컨트롤러가 변경할 수있는 다양한 속성을 관찰하도록 할 수 있습니다.

레이블 변경은 매우 간단하며 모두 미리 정의되어 있습니다. 탭 응용 프로그램에서 탭을 전환 할 때마다 호출되는 메서드가 있습니까? ViewDidLoad와 비슷합니까?

이 질문과 관련하여 찾고있는 방법은 viewWillAppear:viewDidAppear입니다.

0

워크 플로우가 단순한 경우 매우 간단합니다. 이 메소드는 Menu ViewController라고 부르는 것에서 직접 다른 ViewController의 모든 레이블을 변경합니다.

의 당신이 다음과 같은 상황을 가정 해 봅시다 :

Storyboard

파란색 ViewController

는 FirstViewController 클래스이다. 초록색 ViewController는 SecondViewController 클래스입니다. 각 레이블은 해당 클래스의 헤더 파일에있는 firstVCLabelsecondVCLabel 속성에 의해 참조됩니다. 이 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 코드입니다 이러한 클래스의 구현에있어서의 코드

3

NSNotificationCenter에 좋은 시간 인 것 같습니다. 등이 코드를 사용하여 이러한 알림을 구독 추가 할 수 있습니다 V1, V2,에서

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

: 당신은 당신의 MenuViewController가 다른 뷰 컨트롤러에서 업데이트해야합니다 새로운 데이터로 알림을 생성해야 할 것 당신의 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

많은 분들께 감사드립니다. 귀하의 빠른 답변에 깊은 인상을 받았습니다.

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

새 탭이 선택 될 때마다, 그것은 나타나는 직전, 메뉴 설정 한 전역 변수에 따라, 새로운보기에 레이블을 업데이트 :이 특정 경우에, viewWillAppear 트릭을 수행합니다. 매우 빠르고 깨끗합니다. 여러분 덕분에!

관련 문제