gpt4 book ai didi

iOS 从另一个类更新 ViewController UILabel

转载 作者:可可西里 更新时间:2023-11-01 05:36:50 27 4
gpt4 key购买 nike

我是开发新手,一直在用头撞墙试图解决这个问题,我敢肯定,我遗漏了一些愚蠢的东西,但在尝试了各种不同的解决方案之后,我仍然无法得到我正在寻找的结果。

我希望能够从另一个类更新 ViewController 中的 UILabel,这是一个我无法开始工作的小演示程序,我的 ViewController 有两个 UILabel,其中一个是使用 viewDidDoad 和另一个我想从另一个名为 NewClass 的类更新,它是从 ViewController 调用的,我可以看到该类被正确调用,因为控制台正在记录NSLog 条目,但我无法获取更新 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中定义

+ (void)updatedisplay:(ViewController *)vc
{
vc.labelupdate02.text=@"Update from NewClass";

}

并从你的 viewController 中调用它

[NewClass updatedisplay:self];

关于iOS 从另一个类更新 ViewController UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15604872/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com