我有一个在启动新视图控制器时设置的属性。

- (IBAction)headerPressed:(UIButton *)sender {
    RouteViewController *route = [[RouteViewController alloc] initWithRoute:[[Route getRoute:TEST_ROUTE] autorelease]];
    [[self navigationController] pushViewController:circle animated:NO];
}


在新视图中,它是一个UIViewController

RouteViewController.h

@interface =RouteViewController : UIViewController <MKMapViewDelegate> {
    Route *r;
}

-(id)initWithRoute:(Route *)route;


@property (retain, nonatomic) Route *r;


RouteViewController.m

-(id)initWithRoute:(Route *)route{
    self = [super init];

    r = route;

    return self;
}
- (void)loadView {
    [super loadView];

    NSLog(@"TEST: %d", r.Route);  // throws exception. Actually ANY time I access my r property

}


r.Route是const TEST_ROUTE包含的int值。

现在,当我设置断点时,在NSLog时,检查器显示我的对象包含

Self >
    r >
       Route: 1




r >
    Route: 1


因此该属性可用,并且确实包含正确的数据。但是,当我尝试访问该属性时,它会抛出EXC_BAD_ACCESS。

有什么想法吗? :)(这在这里杀了我!)

最佳答案

这里有一些问题:

RouteViewController *route = [[RouteViewController alloc] initWithRoute:[[Route getRoute:TEST_ROUTE] autorelease]];

我认为应将自动释放消息发送到此处的RouteViewController而不是Route对象,因为getRoute方法由于命名约定应返回一个自动释放对象。



RouteViewController *route = [[[RouteViewController alloc] initWithRoute:[Route getRoute:TEST_ROUTE]] autorelease];


您需要保留传递给initWithRoute方法的Route对象的所有权:

要么:

-(id)initWithRoute:(Route *)route{
    self = [super init];

    r = [route retain];

    return self;
}


要么:

-(id)initWithRoute:(Route *)route{
    self = [super init];

    self.r = route;

    return self;
}

关于objective-c - loadView无法访问我的媒体资源,引发EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13459723/

10-12 02:35