我试图在单击按钮时使用数组中的文本更改 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”方法中,但它仍然不起作用。正如我所说,我可以手动设置文本并且它可以工作,但我想从数组中提取信息。最终,我想遍历数组以在每次按下按钮时获取文本,但一次一步。谢谢。
最佳答案
你永远不会创建 madWords
大批。您需要添加:
self.madWords = [NSMutableArray array];
在顶部:
- (void) madArrays {
可能是个好地方。其他可能的好地方是我的类(class)
init
方法或 View Controller viewWillAppear
方法。
关于iphone - 使用 NSMutablearray 数据更改 UILabel 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7508661/