Ios 从另一个类更新ViewController UILabel
我是开发新手,一直在尝试解决这个问题,但在尝试了各种不同的解决方案之后,我仍然无法得到我想要的结果 我想从另一个类更新ViewController中的UILabel。 这里有一个小演示程序,我无法开始工作。 我有一个视图控制器,它有3个UILabel,一个是从viewDidLoad更新的,另外两个是我想从另一个名为Hello的类更新的,这个类是从ViewController调用的,我可以看到该类被正确调用,因为控制台正在记录NSLog条目,但我无法获取更新UILabel的语法 提前谢谢 ViewController.hIos 从另一个类更新ViewController UILabel,ios,objective-c,iphone,uiviewcontroller,Ios,Objective C,Iphone,Uiviewcontroller,我是开发新手,一直在尝试解决这个问题,但在尝试了各种不同的解决方案之后,我仍然无法得到我想要的结果 我想从另一个类更新ViewController中的UILabel。 这里有一个小演示程序,我无法开始工作。 我有一个视图控制器,它有3个UILabel,一个是从viewDidLoad更新的,另外两个是我想从另一个名为Hello的类更新的,这个类是从ViewController调用的,我可以看到该类被正确调用,因为控制台正在记录NSLog条目,但我无法获取更新UILabel的语法 提前谢谢 View
#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
你好,h
#import <Foundation/Foundation.h>
@class ViewController;
@interface Hello : NSObject
+(void)updatedisplay;
+(void) getStringToDisplay;
@end
Hello类的第一个更新方法
然后更新viewDidLoad
在ViewController.m中实现以下方法
您每次都在分配一个全新的ViewController。请查看NSNotification或Delegation。我知道了,谢谢您的快速回复。我将重温我的代码。@Larme感谢您指出它。@Matz非常感谢您提供的信息,这些主题确实对我帮助很大。
#import <Foundation/Foundation.h>
@class ViewController;
@interface Hello : NSObject
+(void)updatedisplay;
+(void) getStringToDisplay;
@end
#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
+ (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];
}
- (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];
}
- (void)secondLabel
{
secondLabelBox.text = @"Apple";
}
- (void)thirdLabel
{
thirdLabelBox.text = @"World";
}