簡體   English   中英

在自動發布的ios項目中保留變量

[英]retain a variable in a autorelease ios project

我是ios編程的初學者,我的項目將其設置為自動釋放,現在我遇到了自動釋放功能的問題。 我顯示我的代碼,希望您可以支持我以避免這種情況

- (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");
    }
}

這兩個函數在同一類中,我在init函數中打印self.nextViewController,它打印一個地址,但是在swip函數中,我再次打印self.nextViewController,但是它打印空地址,在.h文件中,我定義了它保留

@property (retain, nonatomic) MyPhotoViewController *nextViewController;

改變這個

if(self.nextViewController != NULL)

if(self.nextViewController != nil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM