UI View Controller在中断后崩溃

时间:2010-12-31 17:17:05

标签: iphone exc-bad-access interrupt

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

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

请在下面找到我有问题的View Controller的代码。我自己并不是很确定,因为我使用了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。

2 个答案:

答案 0 :(得分:1)

或许查看您的崩溃报告,了解崩溃的位置,如果您不清楚导致崩溃的原因,请通过发布相关代码来帮助我们。我们或许能够为您提供进一步的帮助,但我们需要您先做一些必要的工作,比如尝试用手边的相应信息自己解决。既然你知道要找什么,请出去麒麟!

答案 1 :(得分:0)

问题是我发布了一些从未分配过的UIImages。删除以下行解决了我的问题:

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

F。