2015-08-31 7 views
0

Я новичок в разработке и стараюсь понять это, но после того, как вы попробовали разные решения, я все еще не могу получить результат, который я ищу.Обновление ViewController UILabel из другого класса

Я хотел бы обновить UILabel в ViewController из другого класса. вот небольшая демонстрационная программа, в которой я не могу работать. У меня есть контроллер вида, который имеет 3 UILabels, один из них обновлен из viewDidLoad, а два других я хотел бы обновить из другого класса Hello, который вызывается из ViewController. Я вижу, что класс правильно называется консоль регистрирует запись NSLog, но я не могу получить синтаксис для обновления UILabel.

Заранее благодарен.

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (nonatomic, retain) IBOutlet UILabel *firstLabelBox; 
@property (nonatomic, retain) IBOutlet UILabel *secondLabelBox; 
@property (nonatomic, retain) IBOutlet UILabel *thirdLabelBox; 

@end 

ViewController.m

#import "ViewController.h" 
#import "Hello.h" 
@interface ViewController() 

@end 


@implementation ViewController 

@synthesize firstLabelBox; 
@synthesize secondLabelBox; 
@synthesize thirdLabelBox; 

- (void)viewDidLoad { 
[super viewDidLoad]; 

[email protected]"Hello"; 

[Hello updatedisplay]; 
[Hello getStringToDisplay]; 

} 
@end 

hello.h

#import <Foundation/Foundation.h> 
@class ViewController; 
@interface Hello : NSObject 

+(void)updatedisplay; 
+(void) getStringToDisplay; 
@end 

hello.m

#import "Hello.h" 
#import "ViewController.h" 


@implementation Hello 

+ (void)updatedisplay 
{ 

NSLog(@"NewClass - updatedisplay"); 
ViewController *labelupdate02 = [[ViewController alloc]init]; 
labelupdate02.secondLabelBox.text = @"Apple"; 
} 

+ (void) getStringToDisplay 
{ 
NSLog(@"Inside getString function - updatedisplay"); 
ViewController *labelupdate03 = [[ViewController alloc]init]; 
labelupdate03.thirdLabelBox.text = @"World"; 
} 

@end 
+2

Вы каждый раз выделяете совершенно новый ViewController. – Larme

+0

Пожалуйста, ознакомьтесь с NSNotification или Delegation. – Matz

+0

Я вижу, спасибо за быстрый ответ. я пересматриваю свой код. –

ответ

0

Первый метод обновления Hello класса

+ (void)updatedisplay 
{ 
    NSLog(@"NewClass - updatedisplay");  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_SECOND_LABEL" object:nil]; 
} 

+ (void) getStringToDisplay 
{ 
    NSLog(@"Inside getString function - updatedisplay");  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_THIRD_LABEL" object:nil]; 
} 

А затем обновить viewDidLoad()

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [email protected]"Hello"; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(secondLabel) name:@"CHANGE_SECOND_LABEL" object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(thirdLabel) name:@"CHANGE_THIRD_LABEL" object:nil]; 

    [Hello updatedisplay]; 
    [Hello getStringToDisplay]; 
} 

Реализовать ниже методов в ViewController.m.

- (void)secondLabel 
{ 
    secondLabelBox.text = @"Apple"; 
} 

- (void)thirdLabel 
{ 
    thirdLabelBox.text = @"World"; 
} 
+0

Большое спасибо за помощь, код работал отлично ... –

Смежные вопросы