首页 文章

使用self.window时的EXC_BREAKPOINT? . makeKeyAndView()

提问于
浏览
0

出于某种原因,当我在AppDelegate中使用 self.window?.makeKeyAndView() 时,我得到了EXC_Breakpoint . 当我通过Segue正常加载ViewController时,它不会发生 .

基本上,我对此代码的目标是在用户已经登录时跳过初始视图控制器 .

AppDelegate.swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if NSUserDefaults.standardUserDefaults().objectForKey("auth_token") != nil {
        self.window?.rootViewController = MainViewController()
        self.window?.makeKeyAndVisible()

    }

    return true

}

MainViewController.swift:

这是我的viewDidLoad()中的一大块代码:

navTitleLabel1 = UILabel()
    navTitleLabel1.frame = CGRect(x: 0, y: 8, width: wBounds, height: 20)
    navTitleLabel1.text = "View 1" //TRIGGERS EXC_BREAKPOINT (EXC_ARM_BREAKPOINT)
    navTitleLabel1.textColor = UIColor.whiteColor()
    navTitleLabel1.textAlignment = NSTextAlignment.Center
    self.navbarView.addSubview(navTitleLabel1)

    navTitleLabel2 = UILabel()
    navTitleLabel2.alpha = 0.0
    navTitleLabel2.frame = CGRect(x: 100, y: 8, width: wBounds, height: 20)
    navTitleLabel2.text = "View 2"
    navTitleLabel2.textColor = UIColor.whiteColor()
    navTitleLabel2.textAlignment = NSTextAlignment.Center
    self.navbarView.addSubview(navTitleLabel2)

当我注释掉触发的行是我评论的行时,那么下一个等效的View 2字符串就会触发它 . 我不明白为什么会这样 .

Edit:

我把它固定如下:

func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {

if NSUserDefaults.standardUserDefaults().objectForKey("auth_token") != nil {


    var storyboard = UIStoryboard(name: "Main", bundle: nil)

    self.window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as? UIViewController

    self.window?.makeKeyAndVisible()

}

return true

}

1 回答

  • 2

    您需要显示更多信息,例如堆栈跟踪以及控制台中的任何其他信息 .

    但是在这种情况下你的问题很明显 . 您通过直接初始化它来创建一个新的视图控制器( MainViewController() ),当您这样做时,故事板中没有任何信息存在,因此您的所有插座都将为零,并且由于它们是隐式解包的选项( ! ),这会导致崩溃 .

    崩溃的解释清楚地印在控制台中 .

    如果在故事板上定义了VC的内容,则必须从故事板中加载它 . 使用 instantiateViewControllerWithIdentifier .

相关问题