【发布时间】:2011-03-26 23:50:09
【问题描述】:
作为我的问题的演示,我创建了一个小示例:我创建了新的基于视图的应用程序,然后我将按钮添加到 xib 并将其连接到 IBAction。然后我写了这个类:
#import <Foundation/Foundation.h>
@interface TaskGenerator : NSObject {
NSArray* mathPictures;
}
- (TaskGenerator*)init;
@property(retain,nonatomic) NSArray* mathPictures;
@end
#import "TaskGenerator.h"
@implementation TaskGenerator
@synthesize mathPictures;
- (TaskGenerator*)init
{
self = [super init];
if (self)
{
mathPictures = [NSArray arrayWithObjects:@"0.png",@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",nil];
}
return self;
}
@end
然后我修改了创建的viewController:
#import <UIKit/UIKit.h>
#import "TaskGenerator.h"
@interface NechapackaViewController : UIViewController {
TaskGenerator *taskGen;
}
-(IBAction)vypis:(id)sender;
@property(nonatomic,retain) TaskGenerator *taskGen;
@end
#import "NechapackaViewController.h"
#import "TaskGenerator.h"
@implementation NechapackaViewController
@synthesize taskGen;
- (void)viewDidLoad {
taskGen = [[TaskGenerator alloc] init];
printf("%d\n",[taskGen.mathPictures count]);
[super viewDidLoad];
}
-(IBAction)vypis:(id)sender
{
printf("%d\n",[taskGen.mathPictures count]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
@end
我不明白的是为什么,点击按钮后,NSArray 有问题,没有初始化,虽然我在 viewDidLoad 中初始化了这个 NSArray。我怎样才能让它为我工作?我需要在加载视图后初始化这个 TaskGenerator,然后我会在各种方法中使用这个对象。 请帮我解决这个问题。
【问题讨论】:
-
在提出问题时,您应该发布您得到的错误或意外结果,以及如果您的程序崩溃时的崩溃日志。你说数组没有初始化,但不是 does 发生了什么。
标签: iphone objective-c cocoa-touch nsarray