Pregunta

Me inicializar una vista (imagen) a través de:

Image *myImageView = [[Image alloc]init];
    myImageView.myId = randomImageNumber;
    [myImageView initWithImage:myImage];

En la clase de imagen que hago un registro (LOG1) y obtener el establecido previamente randomImageNumber. Más tarde, en la misma clase, hago un segundo Log (LOG2). ¿Por qué mi segundo registro tiene ningún valor más?

Aquí mi aplicación en archivos de la Clase Image:

@synthesize myId;
-(id) initWithImage: (UIImage *) anImage
{
    NSLog(@"LOG1%d",myId);
    if ((self = [super initWithImage:anImage]))
    {
        self.userInteractionEnabled = YES;
    }
    return self;
}

}
-(void)touchesBegan...
....
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  NSLog(@"LOG2%d",myId);
}

El "yo" vacía de retorno MyID la que he declarado en el fichero cabecera y que se fijó en la inicialización. ¿Cómo puedo evitar eso?

Headerfile mi aspecto como este:

@interface Image : UIImageView 
{
   int myId;
}
@property (assign) int myId;

@end
¿Fue útil?

Solución

par de cosas en el código. Nunca llame a init más de una vez en un objeto, que los tornillos sólo su objeto.

Cambiar a esto:

Image *myImageView = [[Image alloc] initWithImage:myImage];
myImageView.myId = randomImageNumber;

Ese es su problema, por defecto al inicializar una subclase de NSObject, todas las propiedades se establecen en 0 (o nil si son punteros).

Si usted necesita tener un valor por defecto para myId continuación, haga lo siguiente:

// Image.m

@implementation

// other code

-(id) initWithImage:(UIImage *) image
{
    if (self = [super initWithImage:image])
    {
         self.myId = randomImageNumber;
    }

    return self;
}

// other code

@end

Otros consejos

Creo que he encontrado que:

https: / /www.google.com/maps/place/Variable/@53.626739,10.025728,17z/data=!3m1!4b1!4m2!3m1!1s0x47b1885360fab615:0x584b82c7dfb5f612

¿Se puede comprobar si esta variable es la suya?

besties Phil

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top