簡體   English   中英

以編程方式創建的視圖崩潰

[英]Programmatically created view crashing

患者:兩個控制器-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]));
    }
}

我相信goToRecorderButtonPressed方法中存在問題。

你看,你是分配的內存recorderVC但不INITING它。 您應該使用可用方法中的on初始化您的對象,例如init

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM