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

标签 iphone ios 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”方法中,但它仍然不起作用。正如我所说,我可以手动设置文本并且它可以工作,但我想从数组中提取信息。最终,我想遍历数组以在每次按下按钮时获取文本,但一次一步。谢谢。

最佳答案

你永远不会创建 madWords大批。您需要添加:

self.madWords = [NSMutableArray array];

在顶部:
 - (void) madArrays {

可能是个好地方。其他可能的好地方是我的类(class)init方法或 View Controller viewWillAppear方法。

关于iphone - 使用 NSMutablearray 数据更改 UILabel 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7508661/

相关文章:

iphone - 使用 "inserted"行初始化 UITableView

iphone - 加载 TableView 后,将 UITableView 中的单元格排序为部分

ios - UIWebview 禁用重定向到 AppStore

ios - Swift viewdidload 中的 @IB Action 函数

ios - 如何制作自定义对象的 NSMutableArray 属性的深拷贝

iphone - iOS:在我的应用程序中启动 AppStore 页面

ios - 如何清除 Objective-C 中的所有 NSUserDefaults 值?

iphone - objc_property_t iOS 问题

swift - 将 NSDictionary 添加到现有的 NSMutableArray - Swift

iphone - 我是否在 NSMutableArray 中放入了太多内容?请帮忙!