所以我知道我的问题是我的传输阵列没有正确初始化。我应该把transfer = [[NSMutableArray alloc] init]放在哪里;?
@interface PictureViewController (){
Poi *labelPoi;
}
@end
@implementation PictureViewController
@synthesize imageX;
@synthesize imageY;
@synthesize name;
@synthesize transfer;
- (id)init
{
self = [super init];
if(self) {
transfer = [[NSMutableArray alloc] init];
}
return self;
}
-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name{
self = [super init];
if(self){
// transfer = [[NSMutableArray alloc] init];
self.imageX = imageX;
self.imageY = imageY;
self.name = name;
}
return self;
}
/*-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
transfer = [[NSMutableArray alloc] init];
self.imageX = imageX;
self.imageY = imageY;
self.name = name;
NSLog(@"imageX: %f", self.imageX);
NSLog(@"imageY: %f", imageY);
NSLog(@"name: %@", name);
}
return self;
}*/
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"transfer count: %lu",(unsigned long)transfer.count);
for(int i = 0; i < transfer.count; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake([[transfer objectAtIndex:i] imageLocationX], [[transfer objectAtIndex:i] imageLocationY], 200, 50)];
label.text = [[transfer objectAtIndex:i] name];
label.font = [UIFont systemFontOfSize:14];
label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[self.view addSubview:label];
NSLog(@"asdfasdsd");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)display:(double)imageXX andY:(double)imageYY withName:(NSString *)namee{
NSLog(@"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
NSLog(@"imageX: %f",imageXX);
NSLog(@"imageY: %f", imageYY);
NSLog(@"name: %@", namee);
labelPoi = [[Poi alloc] init];
//transfer = [[NSMutableArray alloc] init];
labelPoi.imageLocationX = imageXX;
labelPoi.imageLocationY = imageYY;
labelPoi.name = namee;
[transfer addObject:labelPoi];
NSLog(@"label.x: %f should be: %f", labelPoi.imageLocationX, imageXX);
NSLog(@"label.y: %f should be: %f", labelPoi.imageLocationY, imageYY);
NSLog(@"label.name: %@ should be: %@",labelPoi.name,namee);
NSLog(@"transssssfer: %lu", (unsigned long)transfer.count);
NSLog(@"asfddfsaasfdfdsfsd %f", [[transfer objectAtIndex:0] imageLocationX]);
return self;
}
@end
Poi对象由imageLocationX,imageLocationY和name组成,我试图将Poi对象放入名为transfer的数组中,但是每当我尝试访问传输元素时,我都会收到0或null。 (id)显示函数从该函数中的不同视图NSMutable alloc多次调用,该数组被重置。
这是输出:
2013-07-19 11:22:06.736 AR_UAV_App[12466:11303] %%%%%%%%%%%%%%%%%%%%%%%%
2013-07-19 11:22:06.736 AR_UAV_App[12466:11303] imageX: 224.485718
2013-07-19 11:22:06.736 AR_UAV_App[12466:11303] imageY: 116.353401
2013-07-19 11:22:06.736 AR_UAV_App[12466:11303] name: Beutel Student Health Center
2013-07-19 11:22:06.736 AR_UAV_App[12466:11303] label.x: 224.485718 should be: 224.485718
2013-07-19 11:22:06.736 AR_UAV_App[12466:11303] label.y: 116.353401 should be: 116.353401
2013-07-19 11:22:06.736 AR_UAV_App[12466:11303] label.name: Beutel Student Health Center should be: Beutel Student Health Center
2013-07-19 11:22:06.737 AR_UAV_App[12466:11303] transssssfer: 0
2013-07-19 11:22:06.737 AR_UAV_App[12466:11303] asfddfsaasfdfdsfsd 0.000000
2013-07-19 11:22:06.737 AR_UAV_App[12466:11303] #############################################################
编辑:.h文件
@interface PictureViewController : UIViewController{
NSMutableArray *transfer;
}
@property (nonatomic) double imageX;
@property (nonatomic) double imageY;
@property (nonatomic) NSString *name;
@property (nonatomic,retain) NSArray *transfer;
- (IBAction)backView:(id)sender;
- (IBAction)load:(id)sender X:(double)imageX andY:(double)imageY withName:(NSString *)name;
-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name;
-(id)display:(double)imageX andY:(double)imageY withName:(NSString *)name;
@end
答案 0 :(得分:2)
这个init方法应该成为“指定的”初始化器:
-(id)initWithLabel:andY:withName:(NSString *)name
(顺便说一句,它根据naming conventions没有正确命名)
designated initializer应正确初始化实例(也就是说,在您的情况下,它可能会初始化数组 transfer ,除非您使用lazy accessor)。
“指定初始化程序”通常是“最专业”的初始化程序 - 也就是说,具有大多数参数的初始化程序。大多数情况下,只有一个和易于识别的指定初始值设定项。
“指定的初始化程序”具有规范形式:
-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name{
self = [super init];
if(self){
// initialization
...
}
}
其他初始化方法(如init
方法)应调用指定的初始化程序,如:
- (id)init {
return [self initWithLabel:0 andY:0 withName:@""];
}
答案 1 :(得分:0)
我想有一个问题可能是当你创建这个控制器时,你打电话给:
-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name
如果你这样做,你正在调用[super init]
什么永远不会通过你的类的init方法(你分配数组的地方)。如果你有不同的方法来初始化控制器,我建议你有一个:
commonInit{
_transfer = [[NSMutableArray] alloc] init];
}
然后为控制器中的每个init方法调用此方法,以确保分配了数组。
其他的事情就是在您的控制器的viewDidLoad
中分配您的数组。
只是为了知道,你可以“分配”一个数组,而不必通过调用[NSMutableArray arrayWithCapacity:0];
答案 2 :(得分:0)
您是否使用-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name
初始化PictureViewController?如果是这样,我可以告诉你问题是什么......
-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name
{
self = [super init]; // <--- You are likely confused about this line
.....
}
[super init]
未向您的自定义-(id)init
方法传递消息。它反而指的是PictureViewController
的超类,它可能是UIViewController。
您的问题应该通过在transfer = [[NSMutableArray alloc] init];
自定义初始化方法中取消注释initWithLabel:
来解决。
答案 3 :(得分:-1)
创建一个属性(合成它@synthesize transfer = _transfer;),使用惰性实例化,如:
-(NSMutableArray*)transfer
{
if (!_transfer)
{
_transfer=[NSMutableArray array];
}
return _transfer;
}
并将其从初始
中删除