iOS从另一个类更新ViewController UILabel [英] iOS Updating ViewController UILabel from another Class
问题描述
我是刚接触开发的人,一直在试图解决这个问题,但我确信,我错过了一些愚蠢的东西,但在尝试了各种不同的解决方案后,我仍然无法得到结果我正在寻找。
I'm new to development and been banging my head against a wall trying to figure this out, I'm sure, that I'm missing something silly but after trying various different solutions, I'm still unable to get the result I'm looking for.
我希望能够从另一个类更新ViewController中的UILabel,这是一个我无法达到的小程序工作,我有ViewController有两个UILabel,一个用 viewDidDoad
更新,另一个我要从另一个名为NewClass的类更新 ViewController
,我可以看到该类被正确调用,因为控制台正在记录 NSLog
条目,但我无法获得更新 UILabel
的语法。
I would like to be able to update a UILabel in a ViewController from another class, here is a little demo program that I cannot get to work, I have the ViewController which has two UILabels, one is updated from with the viewDidDoad
and the other one I would like to update from the other class called NewClass which is called from ViewController
, I can the see the class is being called correctly as the console is logging the NSLog
entry but I cannot get the syntax for updating the UILabel
.
提前致谢。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UILabel *_labelupdate01;
UILabel *_labelupdate02;
}
@property (nonatomic, retain) IBOutlet UILabel *labelupdate01;
@property (nonatomic, retain) IBOutlet UILabel *labelupdate02;
@end
ViewController.m
#import "ViewController.h"
#import "NewClass.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize labelupdate01;
@synthesize labelupdate02;
- (void)viewDidLoad
{
[super viewDidLoad];
labelupdate01.text = @"Update from ViewController";
[NewClass updatedisplay];
}
@end
NewClass.h
#import <Foundation/Foundation.h>
@class ViewController;
@interface NewClass : NSObject
@property (nonatomic, assign) ViewController *ViewController;
+ (void)updatedisplay;
@end
NewClass.m
#import "NewClass.h"
#import "ViewController.h"
@implementation NewClass
@synthesize ViewController = _ViewController;
+ (void)updatedisplay
{
NSLog(@"NewClass - updatedisplay");
ViewController *labelupdate02;
labelupdate02.labelupdate02.text = @"Update from NewClass";
}
@end
推荐答案
在newClass中定义它,如
define this in newClass like that
+ (void)updatedisplay:(ViewController *)vc
{
vc.labelupdate02.text=@"Update from NewClass";
}
并从你的viewController调用它
and call it from your viewController
[NewClass updatedisplay:self];
这篇关于iOS从另一个类更新ViewController UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!