-1

所以我知道我的问题是我的传输数组没有被正确初始化。我应该把 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 和名称组成,我试图将 Poi 对象放入名为 transfer 的数组中,但是,每当我尝试访问传输元素时,我都会收到 0 或 null。(id)display 函数在该函数中从不同的视图 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
4

4 回答 4

2

这个 init 方法应该成为“指定的”初始化器:

-(id)initWithLabel:andY:withName:(NSString *)name

(顺便说一下,它没有根据命名约定正确命名)

指定的初始化程序应正确初始化实例(也就是说,在您的情况下,它可能会初始化数组transfer,除非您使用惰性访问器)。

“指定初始化程序”通常是“最专业”的初始化程序 - 即具有最多参数的初始化程序。大多数情况下,只有一个易于识别的指定初始化程序。

“指定初始值设定项”具有规范形式:

-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name{
    self = [super init];
    if(self){
        // initialization
        ...
    }
}

像该方法这样的其他 init 方法init应调用指定的初始化程序,如:

- (id)init {
    return [self initWithLabel:0 andY:0 withName:@""];
}
于 2013-07-19T17:06:08.210 回答
0

我想一个问题可能是,当您创建此控制器时,您正在调用:

 -(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name

如果你这样做,你正在调用[super init]永远不会通过你的类的 init 方法(你分配数组的地方)。如果您有不同的方法来初始化控制器,我建议您使用:

commonInit{
   _transfer = [[NSMutableArray] alloc] init];
}

然后为控制器中的每个 init 方法调用此方法,以确保分配数组。另一件事就是在viewDidLoad你的控制器中分配你的数组。

只是为了让大家知道,您可以“分配”一个数组,而不必担心通过调用来释放对象[NSMutableArray arrayWithCapacity:0];

于 2013-07-19T16:58:01.683 回答
0

你在初始化你的 PictureViewController-(id)initWithLabel:(double)imageX andY:(double)imageY withName:(NSString *)name吗?如果是这样,我可以告诉你问题是什么......

-(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:自定义 init 方法中取消注释来解决。

于 2013-07-19T17:01:54.630 回答
-1

创建一个属性(合成它@synthesize transfer=_transfer;),使用惰性实例化,如:

-(NSMutableArray*)transfer
{
   if (!_transfer)
   {
       _transfer=[NSMutableArray array];
   }

   return _transfer;
}

并从你的初始化中取出

于 2013-07-19T18:04:20.083 回答