使用 NSMutablearray 数据更改 UILabel 中的文本
我试图在单击按钮时使用数组中的文本更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您永远不会创建
madWords
数组。您需要在以下位置添加::
可能是一个好地方。其他可能不错的地方是类
init
方法或视图控制器viewWillAppear
方法。You never create the
madWords
array. You need to add:at the top of:
would probably be a good place. Other possibly good places would be i the class
init
method or the view controllerviewWillAppear
method.当您填充它时,
madArrays
看起来仍然是nil
。在某些时候,您需要类似[self setMadArrays:[[NSMutableArray alloc] init]];
的东西。另外,在调用super
之前,不要忘记在dealloc
中释放madArrays
,否则会出现内存泄漏。It looks like
madArrays
is stillnil
when you come to populate it. At some point you need something like[self setMadArrays:[[NSMutableArray alloc] init]];
. Also, don't forget to releasemadArrays
in thedealloc
before callingsuper
as you'll have a memory leak.