自定义类初始化的EXC_BAD_ACCESS

时间:2013-02-21 13:18:28

标签: objective-c memory-management

我写了以下课程:

.h

#import <Foundation/Foundation.h>
@class TDLPaneViewController;

@interface TDLSubViewController : NSObject

@property (nonatomic, strong) IBOutlet UIView* view;
@property (nonatomic, strong) TDLPaneViewController* paneViewController;

- (id) initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;

- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisppear:(BOOL)animated;
- (void)viewDidDisppear:(BOOL)animated;

@end

.m

#import "TDLSubViewController.h"

@interface TDLSubViewController ()
{
     UIView *view;
}

@property (nonatomic, strong) NSString* nibName;
@property (nonatomic, strong) NSBundle* nibBundle;

@end

@implementation TDLSubViewController

@synthesize paneViewController, view;
@synthesize nibName, nibBundle;

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super init];

    if (self)
    {
          self.nibName = nibNameOrNil;
          self.nibBundle = nibBundleOrNil;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
     NSLog(@"viewWillAppear:%i", animated);
}
- (void)viewDidAppear:(BOOL)animated
{
     NSLog(@"viewDidAppear:%i", animated);
}
- (void)viewWillDisppear:(BOOL)animated
{
     NSLog(@"viewWillDisppear:%i", animated);
} 
- (void)viewDidDisppear:(BOOL)animated
{
     NSLog(@"viewDidDisppear:%i", animated);
}

@end

显示另一个视图时出错:       - (void)viewDidAppear:(BOOL)动画     {          [super viewDidAppear:animated];

     TDLTestSubViewController* subView = [[TDLTestSubViewController alloc] initWithNibName:@"TDLTestSubViewController" bundle:nil] ;

     [self pushSubViewController:nil animated:YES leftSide:YES];

} 

TDLTestSubViewController是TDLSubViewController的子类。

如果我使用UIViewController而不是TDLTestSubViewController一切正常,如果我关闭ARC它也可以。所以我认为问题出在TDLSubViewController类中。请,建议可能出现的问题?

1 个答案:

答案 0 :(得分:5)

{必须时,initWithNibName:bundle:方法不会返回self