我继承了一个大型代码,在applicationDidFinishLaunchingWithOptions
[self.window makeKeyAndVisible]
期间使用EXC_BAD_ACCESS(代码= 2,地址...)继续崩溃,因此我没有关于控制台的有用信息。通过消除,我将下面的一个简单示例分开,当将navigationController视图添加到baseVC.view时会崩溃。任何人都可以帮忙解释为什么它会崩溃以及如何修复它?
@interface ViewController () <UINavigationControllerDelegate>
@property (nonatomic, strong) UIViewController *baseVC;
@property (nonatomic, strong) UINavigationController *customNC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.baseVC = [[UIViewController alloc] init];
CGSize size = self.view.bounds.size;
self.baseVC.view.frame = CGRectMake(0.0, 0.0, size.width, size.height );
[self.view addSubview:self.baseVC.view];
self.customNC = [[UINavigationController alloc] initWithRootViewController:self.baseVC];
[self addChildViewController:self.customNC];
[self.customNC setNavigationBarHidden:YES animated:NO];
self.customNC.delegate = self;
self.customNC.view.frame = self.baseVC.view.frame;
[self.baseVC.view addSubview:self.customNC.view];
}
@end
我的实际代码更复杂,但此示例的行为是相同的。谢谢。
编辑:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[RootVC alloc] initWithNibName:@"RootVC" bundle:nil];
[self.window makeKeyAndVisible];
return YES;
}
答案 0 :(得分:1)
self.customNC = [[UINavigationController alloc] initWithRootViewController:self.baseVC];
[self.baseVC.view addSubview:self.customNC.view];
问题在于此代码行。因此,self.baseVC.view位于self.customNC.view中,反之亦然。它导致崩溃。
答案 1 :(得分:0)
EXC_BAD_ACCESS
当您对应用程序的内存有问题时,可能会发布某些内容并尝试使用它,就会发生此异常。要调试它,请使用iOS工具。
如果您没有这个didFinishLaunchingWithOptions,请尝试添加此行:
[self.window addSubview:[self.navigationController view]];
[self.window makeKeyAndVisible];