简体   繁体   English

以编程方式创建的视图崩溃

[英]Programmatically created view crashing

Patients: Two controllers -- ViewController and (modally presented) RecorderViewController Symptoms: After the RecorderViewController is modally presented, it does some work. 患者:两个控制器-ViewController和(以模态形式显示)RecorderViewController症状:在以模态形式呈现RecorderViewController之后,它完成了一些工作。 After being dismissed, the program crashes with EXC_BAD_ACCESS 被解雇后,该程序将崩溃,并显示EXC_BAD_ACCESS

In AppDelegate.m: 在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;
}

In ViewController.m 在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

Crashes in main.m at return UIApplicationMain: #import 返回UIApplicationMain时,main.m中崩溃:#import

#import "AppDelegate.h"

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

I believe there is problem in goToRecorderButtonPressed method. 我相信goToRecorderButtonPressed方法中存在问题。

Look, you are allocating memory for a recorderVC but don't initing it. 你看,你是分配的内存recorderVC但不INITING它。 You should initialize your object with on of available methods, for example, init : 您应该使用可用方法中的on初始化您的对象,例如init

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在tableviewcell中以编程方式创建的视图中的问题 - Issue in view created programmatically in tableviewcell 方法调用时,以编程方式崩溃的UIButton崩溃了 - UIButton that's created programmatically crashing when method called 添加故事板视图作为以编程方式创建的视图的子视图 - Add a storyboard view as a subview of a programmatically created view 从以编程方式创建的表视图创建 segue - Creating segue from programmatically created table view 以编程方式创建的带有按钮目标的自定义视图 - Programmatically created custom view with button target touches开始在以编程方式创建的视图和图像视图上不起作用 - touchesBegan not functioning on programmatically created view and imageview 以编程方式创建的视图不显示 - Programmatically created View doesn't show CAGradientLayer 不适用于以编程方式创建的 UI 中的完整视图 - CAGradientLayer not applying to the full view in programmatically created UI 如何重新排列以编程方式创建的旋转视图? - How to rearrange programmatically created view on rotation? UILabel 在以编程方式创建的堆栈视图中不可单击 Swift - UILabel not clickable in stack view programmatically created Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM