简体   繁体   English

在自动发布的ios项目中保留变量

[英]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. 我是ios编程的初学者,我的项目将其设置为自动释放,现在我遇到了自动释放功能的问题。 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 这两个函数在同一类中,我在init函数中打印self.nextViewController,它打印一个地址,但是在swip函数中,我再次打印self.nextViewController,但是它打印空地址,在.h文件中,我定义了它保留

@property (retain, nonatomic) MyPhotoViewController *nextViewController;

Change this 改变这个

if(self.nextViewController != NULL)

to

if(self.nextViewController != nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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