我是开发新手并且一直试图解决这个问题,但在尝试了各种不同的解决方案之后,我仍然无法得到我正在寻找的结果。
我想从另一个类更新ViewController中的UILabel。 这是一个我无法工作的小程序。 我有一个视图控制器有3个UILabel,一个是从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];
firstLabelBox.text=@"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
答案 0 :(得分:0)
Content-Type:application/octet-stream; charset=UTF-8
class
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];
firstLabelBox.text=@"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