开发者

accessing class file crashes when scrolling starts (UIScrollview)

开发者 https://www.devze.com 2023-02-28 11:04 出处:网络
hopefully someone will be able to help me. I have a UIScrollView on my page. The .h file has set the UIscrollviewdelegate.

hopefully someone will be able to help me. I have a UIScrollView on my page. The .h file has set the UIscrollviewdelegate.

I have a class file called "Picture.h / Picture.m".

- (id)initWithName:(NSString *)aName filename:(NSString *)aFilename {
    self.name = aName;
    self.filename = aFilename;
    return self; 
}

In this class file, I simply set a couple of strings. I load an array with object of this picture class, for example

Picture *imag开发者_StackOverflow中文版e2 = [[Picture alloc] initWithName:@"Apple" filename:@"apple.png"];
[pictureArray addObject: image2];
[image2 release];

Within my viewController, I call this class and assign is as such

Picture *thisPicture = (Picture *)[appDelegate.pictureArray objectAtIndex:0];
view2image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", thisPicture.filename]];

The above works fine. The image is set to what ever I put, example, "apple.png". However, when I tried to set this in the - (void) scrollViewDidScroll:(UIScrollView *) method within my viewController, I get a bad exec error and the app crashes.

Yet, if I had an array of filenames (so not storing my class object in the array) and access objectAtIndex:0 in the scrollViewDidScroll - it works fine.

So, this code is OK

nextImageView.image = [UIImage imageNamed: [NSString stringWithFormat:@"%@", [appDelegate.pictureCardsArray objectAtIndex:0]]];

but this crashes

Picture *image3 = (Picture *)[appDelegate.pictureArray objectAtIndex:0];
nextImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", image3.filename]];

Interestingly though, if I don't try to access the element of image3 (eg image3.filename) it doesn't crash. This is useless though! Also, if I disable the delegate = self for the uiscrollview, then this code works, but none of the scrolling actions are fired. I came across this post (http://stackoverflow.com/questions/1734720/uiscrollview-on-a-uiviewcontroller) when searching for the solution, but cannot see where I might be releasing the viewController early. To be safe, nothing is getting released (yet!)

Hopefully someone might be able to shed some light on it!!

[edit]Just adding in the full class files][/edit]

Picture.h

#import <UIKit/UIKit.h>

@interface Picture : NSObject {
    NSString *name;
    NSString *filename;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *filename;

- (id)initWithName:(NSString *)aName filename:(NSString *)aFilename;

@end

Picture.m

#import "Picture.h"

@implementation Picture
@synthesize name, filename;

- (id)initWithName:(NSString *)aName filename:(NSString *)aFilename {

    self.name = aName;
    self.filename = aFilename;
    return self;

}

@end


Just for completeness... the solution was as Antwen Van Houdt said - I changed copy to retain and it worked fine.

#import <UIKit/UIKit.h>

@interface Picture : NSObject {
    NSString *name;
    NSString *filename;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *filename;

- (id)initWithName:(NSString *)aName filename:(NSString *)aFilename;

@end
0

精彩评论

暂无评论...
验证码 换一张
取 消