简体   繁体   中英

retain a variable in a autorelease ios project

i am a beginner with ios programming, my project set it autorelease, and now i get a problem with autorelease function. i show my code and hope you can suport me to avoid this

- (void)initPhotoImage
{
    photoImage = [[MyPhotoImageView alloc] initWithFrame:CGRectMake(5, 30, 0, 0)];
    photoImage.photoViewController = self;
    [photoView addSubview:photoImage];

    UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)];
    gesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [photoView addGestureRecognizer:gesture];
    UISwipeGestureRecognizer *gesture1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
    gesture1.direction = UISwipeGestureRecognizerDirectionRight;
    [photoView addGestureRecognizer:gesture1];

}

-(void)didSwipeLeft:(UIGestureRecognizer *)gestureRecognizer {

    if(self.nextViewController != NULL){
        [UIView animateWithDuration:1 animations:^{
            self.view.frame = CGRectMake(-1*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
        }];
        NSLog(@"next");
    }else{
        NSLog(@"next view is null");
    }
}

this 2 functions in a same class, i print self.nextViewController in the init function, it print a address but in swip function, i print self.nextViewController again but it print null address, in .h file i defined it retain

@property (retain, nonatomic) MyPhotoViewController *nextViewController;

Change this

if(self.nextViewController != NULL)

to

if(self.nextViewController != nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM