0

患者:两个控制器——ViewController 和(模态呈现的)RecorderViewController 症状:RecorderViewController 模态呈现后,它做了一些工作。被解雇后,程序因 EXC_BAD_ACCESS 而崩溃

在 AppDelegate.m 中:

@synthesize window = _window;
@synthesize controller = _controller;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.controller = [ViewController alloc];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.controller;
    [self.window makeKeyAndVisible];

    return YES;
}

在 ViewController.m 中

#import "ViewController.h"
#import "RecorderViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize presentModalButton;

- (void)loadView 
{
    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

    self.presentModalButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.presentModalButton.frame = CGRectMake(0, self.view.frame.size.height/2, 100, 50);
    [self.presentModalButton addTarget:self action:@selector(goToRecorderButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.presentModalButton];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)goToRecorderButtonPressed:(id)sender {
    RecorderViewController *recorderVC = [RecorderViewController alloc];
    [self presentModalViewController:recorderVC animated:YES];
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

返回 UIApplicationMain 时 main.m 中的崩溃:#import

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
4

1 回答 1

0

我相信方法有问题goToRecorderButtonPressed

看,您正在为 a 分配内存,recorderVC但没有初始化它。您应该使用可用的方法初始化您的对象,例如init

- (IBAction)goToRecorderButtonPressed:(id)sender {
    RecorderViewController *recorderVC = [[RecorderViewController alloc] init];
    [self presentModalViewController:recorderVC animated:YES];
}
于 2012-05-15T17:21:46.800 回答