UI 视图控制器在中断后崩溃

发布于 2024-10-10 05:13:34 字数 6049 浏览 0 评论 0原文

考虑到 iOS 的多任务处理功能,我认为通过按主页按钮或拨打电话来暂停和恢复我的应用程序不会很痛苦,但对于特定的视图控制器它会崩溃。

导航栏工作正常,即当我点击“返回”时就可以了,但是如果我尝试点击显示的 UI 视图控制器的控件,那么我会得到 EXC_BAD_ACCESS.. =/

请在下面找到我有问题的视图控制器的代码。我自己也不太确定,因为我使用loadView来构建它的View。然而,除了这个中断问题之外,它工作得很好。

StoreViewController.h

#import <UIKit/UIKit.h>

@protocol StoreViewDelegate <NSObject>
@optional
- (void)DirectionsClicked:(double)lat:(double)lon;
@end

@interface StoreViewController : UIViewController 
{
    double latitude;
    double longitude;
    NSString *description;  
    NSString *imageURL;
    short rating;
    NSString *storeType;
    NSString *offerType;

    UIImageView *imageView;
    UILabel *descriptionLabel;

    id<StoreViewDelegate> storeViewDel;
}

@property (nonatomic) double latitude;
@property (nonatomic) double longitude;
@property (nonatomic) short rating;
@property (nonatomic,retain) NSString *description;
@property (nonatomic,retain) NSString *imageURL;
@property (nonatomic,retain) NSString *storeType;
@property (nonatomic,retain) NSString *offerType;
@property (assign) id<StoreViewDelegate> storeViewDel;

@end

StoreViewController.m

#import "StoreViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface StoreViewController()

-(CGSize) calcLabelSize:(NSString *)string withFont:(UIFont *)font  maxSize:(CGSize)maxSize;

@end

@implementation StoreViewController

@synthesize storeViewDel, longitude, latitude, description, imageURL, rating, offerType, storeType;

- (void)mapsButtonClicked:(id)sender
{
}

- (void)loadView 
{
    UIView *storeView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];

    // Colours
    UIColor *lightBlue = [[UIColor alloc] initWithRed:(189.0f / 255.0f) 
                                              green:(230.0f / 255.0f) 
                                               blue:(252.0f / 255.0f) alpha:1.0f];

    UIColor *darkBlue = [[UIColor alloc] initWithRed:(28.0f/255.0f) 
                                                green:(157.0f/255.0f) 
                                                 blue:(215.0f/255.0f) 
                                                alpha:1.0f];

    // Layout
    int width = self.navigationController.view.frame.size.width;
    int height = self.navigationController.view.frame.size.height;
    float firstRowHeight = 100.0f;
    int margin = width / 20;
    int imgWidth = (width - 3 * margin) / 2;

    // Set ImageView 
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(margin, margin, imgWidth, imgWidth)];
    CALayer *imgLayer = [imageView layer];
    [imgLayer setMasksToBounds:YES];
    [imgLayer setCornerRadius:10.0f];
    [imgLayer setBorderWidth:4.0f];
    [imgLayer setBorderColor:[lightBlue CGColor]];
    // Load default image
    NSData *imageData = [NSData dataWithContentsOfFile:@"thumb-null.png"];
    UIImage *image = [UIImage imageWithData:imageData];
    [imageView setImage:image];

    [storeView addSubview:imageView];

    // Set Rating
    UIImageView *ratingView = [[UIImageView alloc] initWithFrame:CGRectMake(3 * width / 4 - 59.0f, 
                                                                        margin, 
                                                                        118.0f,
                                                                        36.0f)];
    UIImage *ratingImage = [UIImage imageNamed:@"bb-rating-0.png"];
    [ratingView setImage:ratingImage];
    [ratingImage release];

    [storeView addSubview:ratingView];

    // Set Get Directions button
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(3 * width / 4 - 71.5f, 
                                                               36.0f + 2*margin, 
                                                               143.0f, 63.0f)];
    [btn addTarget:self action:@selector(mapsButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *mapsImgUp = [UIImage imageNamed:@"bb-maps-up.png"];
    [btn setImage:mapsImgUp forState:UIControlStateNormal];
    [mapsImgUp release];
    UIImage *mapsImgDown = [UIImage imageNamed:@"bb-maps-down.png"];
    [btn setImage:mapsImgDown forState:UIControlStateHighlighted];
    [mapsImgDown release];

    [storeView addSubview:btn];
    [btn release];

    // Set Description Text
    UIScrollView *descriptionView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 
                                                                                   imgWidth + 2 * margin, 
                                                                                   width, 
                                                                                   height - firstRowHeight)];
    descriptionView.backgroundColor = lightBlue;
    CGSize s = [self calcLabelSize:description withFont:[UIFont systemFontOfSize:18.0f] maxSize:CGSizeMake(width, 9999.0f)];
    descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(margin, margin, width - 2 * margin, s.height)];
    descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    descriptionLabel.numberOfLines = 0;
    descriptionLabel.font = [UIFont systemFontOfSize:18.0f];
    descriptionLabel.textColor = darkBlue;
    descriptionLabel.text = description;
    descriptionLabel.backgroundColor = lightBlue;

    [descriptionView addSubview:descriptionLabel];
    [storeView addSubview:descriptionView];
    [descriptionLabel release];

    [lightBlue release];
    [darkBlue release];

    self.view = storeView;
    [storeView release];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc 
{
[imageView release];
    [descriptionLabel release];

    [super dealloc];
}

@end

有什么建议吗?

谢谢你,

F.

Given the multitasking of iOS I thought it wouldn't be a pain to pause and resume my app, by pressing the home button or due to a phone call, but for a particular view controller it crashes.

The navigation bar is working fine i.e. when I tap "Back" it's ok, but if I try to tap controls of the displayed UI View Controller then I get EXC_BAD_ACCESS.. =/

Please find the code of my problematic View Controller below. I'm not very sure about it myself, because I used loadView to build its View. However apart from this interruption problem it works fine.

StoreViewController.h

#import <UIKit/UIKit.h>

@protocol StoreViewDelegate <NSObject>
@optional
- (void)DirectionsClicked:(double)lat:(double)lon;
@end

@interface StoreViewController : UIViewController 
{
    double latitude;
    double longitude;
    NSString *description;  
    NSString *imageURL;
    short rating;
    NSString *storeType;
    NSString *offerType;

    UIImageView *imageView;
    UILabel *descriptionLabel;

    id<StoreViewDelegate> storeViewDel;
}

@property (nonatomic) double latitude;
@property (nonatomic) double longitude;
@property (nonatomic) short rating;
@property (nonatomic,retain) NSString *description;
@property (nonatomic,retain) NSString *imageURL;
@property (nonatomic,retain) NSString *storeType;
@property (nonatomic,retain) NSString *offerType;
@property (assign) id<StoreViewDelegate> storeViewDel;

@end

StoreViewController.m

#import "StoreViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface StoreViewController()

-(CGSize) calcLabelSize:(NSString *)string withFont:(UIFont *)font  maxSize:(CGSize)maxSize;

@end

@implementation StoreViewController

@synthesize storeViewDel, longitude, latitude, description, imageURL, rating, offerType, storeType;

- (void)mapsButtonClicked:(id)sender
{
}

- (void)loadView 
{
    UIView *storeView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];

    // Colours
    UIColor *lightBlue = [[UIColor alloc] initWithRed:(189.0f / 255.0f) 
                                              green:(230.0f / 255.0f) 
                                               blue:(252.0f / 255.0f) alpha:1.0f];

    UIColor *darkBlue = [[UIColor alloc] initWithRed:(28.0f/255.0f) 
                                                green:(157.0f/255.0f) 
                                                 blue:(215.0f/255.0f) 
                                                alpha:1.0f];

    // Layout
    int width = self.navigationController.view.frame.size.width;
    int height = self.navigationController.view.frame.size.height;
    float firstRowHeight = 100.0f;
    int margin = width / 20;
    int imgWidth = (width - 3 * margin) / 2;

    // Set ImageView 
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(margin, margin, imgWidth, imgWidth)];
    CALayer *imgLayer = [imageView layer];
    [imgLayer setMasksToBounds:YES];
    [imgLayer setCornerRadius:10.0f];
    [imgLayer setBorderWidth:4.0f];
    [imgLayer setBorderColor:[lightBlue CGColor]];
    // Load default image
    NSData *imageData = [NSData dataWithContentsOfFile:@"thumb-null.png"];
    UIImage *image = [UIImage imageWithData:imageData];
    [imageView setImage:image];

    [storeView addSubview:imageView];

    // Set Rating
    UIImageView *ratingView = [[UIImageView alloc] initWithFrame:CGRectMake(3 * width / 4 - 59.0f, 
                                                                        margin, 
                                                                        118.0f,
                                                                        36.0f)];
    UIImage *ratingImage = [UIImage imageNamed:@"bb-rating-0.png"];
    [ratingView setImage:ratingImage];
    [ratingImage release];

    [storeView addSubview:ratingView];

    // Set Get Directions button
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(3 * width / 4 - 71.5f, 
                                                               36.0f + 2*margin, 
                                                               143.0f, 63.0f)];
    [btn addTarget:self action:@selector(mapsButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *mapsImgUp = [UIImage imageNamed:@"bb-maps-up.png"];
    [btn setImage:mapsImgUp forState:UIControlStateNormal];
    [mapsImgUp release];
    UIImage *mapsImgDown = [UIImage imageNamed:@"bb-maps-down.png"];
    [btn setImage:mapsImgDown forState:UIControlStateHighlighted];
    [mapsImgDown release];

    [storeView addSubview:btn];
    [btn release];

    // Set Description Text
    UIScrollView *descriptionView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 
                                                                                   imgWidth + 2 * margin, 
                                                                                   width, 
                                                                                   height - firstRowHeight)];
    descriptionView.backgroundColor = lightBlue;
    CGSize s = [self calcLabelSize:description withFont:[UIFont systemFontOfSize:18.0f] maxSize:CGSizeMake(width, 9999.0f)];
    descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(margin, margin, width - 2 * margin, s.height)];
    descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    descriptionLabel.numberOfLines = 0;
    descriptionLabel.font = [UIFont systemFontOfSize:18.0f];
    descriptionLabel.textColor = darkBlue;
    descriptionLabel.text = description;
    descriptionLabel.backgroundColor = lightBlue;

    [descriptionView addSubview:descriptionLabel];
    [storeView addSubview:descriptionView];
    [descriptionLabel release];

    [lightBlue release];
    [darkBlue release];

    self.view = storeView;
    [storeView release];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc 
{
[imageView release];
    [descriptionLabel release];

    [super dealloc];
}

@end

Any suggestions ?

Thank you,

F.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-10-17 05:13:34

也许查看您的崩溃报告,了解崩溃的位置,如果您不清楚导致崩溃的原因,可以通过发布相关代码来帮助我们帮助您。我们也许能够为您提供进一步的帮助,但我们需要您首先做一些先决工作,例如尝试使用手头的适当信息自行解决问题。现在你知道要寻找什么了,请继续前进吧,独角兽!

Perhaps look at your crash report, get an idea of where it's crashing, and help us help you by posting the relevant code if it is not clear to you what is causing the crash. We may be able to help you further, but we need you to do some pre-requisite work first, like trying to solve it yourself with the appropriate information at hand. Now that you know what to look for, please, go forth and unicorn!

比忠 2024-10-17 05:13:34

问题是我释放了一些从未分配的 UIImage。删除以下行解决了我的问题:

[ratingImage release];
[mapsImgUp release];
[mapsImgDown release];

F.

The problem was that I released some UIImages that were never allocated. Erasing the following lines solved my issue:

[ratingImage release];
[mapsImgUp release];
[mapsImgDown release];

F.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文