使用 NSMutablearray 数据更改 UILabel 中的文本

发布于 2024-12-06 01:16:11 字数 1173 浏览 2 评论 0原文

我试图在单击按钮时使用数组中的文本更改 UILabel 的文本,但它没有执行任何操作。

@interface Test01AppDelegate : NSObject <UIApplicationDelegate> {
UILabel *helloLabel;
UIButton *hellobutton;
NSMutableArray *madWords;
}

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UIButton *hellowButton;
@property (nonatomic, retain) IBOutlet UILabel *hellowLabel;

@property (nonatomic, retain) NSMutableArray *madWords;

- (void) madArrays;
- (IBAction)helloYall;

@end

#import "Test01AppDelegate.h"

@implementation Test01AppDelegate

@synthesize window = _window;
@synthesize hellowButton;
@synthesize hellowLabel;
@synthesize madWords;

 - (void) madArrays {
[madWords addObject:@"Part A"];
[madWords addObject:@"Part B"];
[madWords addObject:@"Part C"];
[madWords addObject:@"Part D"];
}

- (IBAction)helloYall {

 [self madArrays];

 self.hellowLabel.text = [madWords objectAtIndex:0];

}

可以设置 helloLabel 文本

@"some text here";

,效果很好。另外,我尝试将“madArrays”方法复制到“helloYall”方法中,但它仍然不起作用。正如我所说,我可以手动设置文本并且它可以工作,但我想从数组中提取信息。最终,我想循环遍历数组以获取每次按下按钮时的文本,但一次一步。谢谢。

I'm trying to change the text of a UILabel with text from an array upon a button click, but it doesn't do anything.

@interface Test01AppDelegate : NSObject <UIApplicationDelegate> {
UILabel *helloLabel;
UIButton *hellobutton;
NSMutableArray *madWords;
}

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UIButton *hellowButton;
@property (nonatomic, retain) IBOutlet UILabel *hellowLabel;

@property (nonatomic, retain) NSMutableArray *madWords;

- (void) madArrays;
- (IBAction)helloYall;

@end

and

#import "Test01AppDelegate.h"

@implementation Test01AppDelegate

@synthesize window = _window;
@synthesize hellowButton;
@synthesize hellowLabel;
@synthesize madWords;

 - (void) madArrays {
[madWords addObject:@"Part A"];
[madWords addObject:@"Part B"];
[madWords addObject:@"Part C"];
[madWords addObject:@"Part D"];
}

- (IBAction)helloYall {

 [self madArrays];

 self.hellowLabel.text = [madWords objectAtIndex:0];

}

I can set the helloLabel text with

@"some text here";

and it works fine. Also, I tried copying the "madArrays" method into the "helloYall" method and it still didn't work. As I said, I can manually set the text and it works, but I'd like to pull the info from an array. Eventually, I'd like to loop through the array to grab the text on each button press, but one step at a time. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-12-13 01:16:11

您永远不会创建 madWords 数组。您需要

self.madWords = [NSMutableArray array];

在以下位置添加::

 - (void) madArrays {

可能是一个好地方。其他可能不错的地方是类 init 方法或视图控制器 viewWillAppear 方法。

You never create the madWords array. You need to add:

self.madWords = [NSMutableArray array];

at the top of:

 - (void) madArrays {

would probably be a good place. Other possibly good places would be i the class init method or the view controller viewWillAppear method.

夏见 2024-12-13 01:16:11
   // Or you can try this in your init Method:

    //first allocate the ivar

- (void)myInitMethod {

    madArrays = [[NSMutableArray]alloc]init];
}


//then you can do anything directly to the ivar or throughout de setter

- (void)doAnythingWithiVar {

// do your stuff

}

    //when you are done you can dealloc your ivar

- (void)dealloc {

    [madArrays release];
    [super dealloc];
}
   // Or you can try this in your init Method:

    //first allocate the ivar

- (void)myInitMethod {

    madArrays = [[NSMutableArray]alloc]init];
}


//then you can do anything directly to the ivar or throughout de setter

- (void)doAnythingWithiVar {

// do your stuff

}

    //when you are done you can dealloc your ivar

- (void)dealloc {

    [madArrays release];
    [super dealloc];
}
撩心不撩汉 2024-12-13 01:16:11

当您填充它时,madArrays 看起来仍然是nil。在某些时候,您需要类似 [self setMadArrays:[[NSMutableArray alloc] init]]; 的东西。另外,在调用 super 之前,不要忘记在 dealloc 中释放 madArrays,否则会出现内存泄漏。

It looks like madArrays is still nil when you come to populate it. At some point you need something like [self setMadArrays:[[NSMutableArray alloc] init]];. Also, don't forget to release madArrays in the dealloc before calling super as you'll have a memory leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文