Я новичок в разработке и стараюсь понять это, но после того, как вы попробовали разные решения, я все еще не могу получить результат, который я ищу.Обновление 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
Вы каждый раз выделяете совершенно новый ViewController. – Larme
Пожалуйста, ознакомьтесь с NSNotification или Delegation. – Matz
Я вижу, спасибо за быстрый ответ. я пересматриваю свой код. –