滚动开始时访问类文件崩溃(UIScrollview)
希望有人能够帮助我。我的页面上有一个 UIScrollView 。 .h 文件已设置 UIscrollviewdelegate。
我有一个名为“Picture.h / Picture.m”的类文件。
- (id)initWithName:(NSString *)aName filename:(NSString *)aFilename {
self.name = aName;
self.filename = aFilename;
return self;
}
在这个类文件中,我只是设置了几个字符串。我加载一个包含该图片类的对象的数组,例如
Picture *image2 = [[Picture alloc] initWithName:@"Apple" filename:@"apple.png"];
[pictureArray addObject: image2];
[image2 release];
在我的 viewController 中,我调用该类并进行分配,
Picture *thisPicture = (Picture *)[appDelegate.pictureArray objectAtIndex:0];
view2image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", thisPicture.filename]];
上面的工作正常。图像设置为我放置的内容,例如“apple.png”。但是,当我尝试在 viewController 中的 - (void)scrollViewDidScroll:(UIScrollView *)
方法中设置它时,我收到了错误的执行错误,并且应用程序崩溃了。
然而,如果我有一个文件名数组(因此不在数组中存储我的类对象)并访问scrollViewDidScroll中的objectAtIndex:0 - 它工作得很好。
所以,这段代码没问题,
nextImageView.image = [UIImage imageNamed: [NSString stringWithFormat:@"%@", [appDelegate.pictureCardsArray objectAtIndex:0]]];
但是会崩溃。
Picture *image3 = (Picture *)[appDelegate.pictureArray objectAtIndex:0];
nextImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", image3.filename]];
有趣的是,如果我不尝试访问 image3 的元素(例如 image3.filename),它不会崩溃。虽然这没什么用!另外,如果我禁用 uiscrollview 的 delegate = self ,则此代码可以工作,但不会触发任何滚动操作。我在搜索解决方案时遇到了这篇文章(http://stackoverflow.com/questions/1734720/uiscrollview-on-a-uiviewcontroller),但看不到我可能会提前释放 viewController 的位置。为了安全起见,目前还没有发布任何内容!
希望有人能够对此有所了解!
[编辑]只需添加完整的类文件][/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
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 *image2 = [[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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了完整性......解决方案正如 Antwen Van Houdt 所说 - 我更改了副本以保留,并且效果很好。
Just for completeness... the solution was as Antwen Van Houdt said - I changed copy to retain and it worked fine.