SWIFT uiviewcontroller初始化 [英] SWIFT uiviewcontroller init

查看:433
本文介绍了SWIFT uiviewcontroller初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 Swift .

使用 Objective-C 可以避免使用情节提要,因此初始化视图控制器很容易.我只创建一个UIViewController子类,然后将所有初始化代码放入其中 initWithNibName.

with Objective-C when I could avoid using storyboard, initializing a view controller was easy. I would just create a UIViewController subclass, and I would put all the initialization code inside initWithNibName.

现在有了分镜脚本,我迷路了.我创建了一个UIviewController子类,我尝试添加init(笔尖名称)和init(编码器),但是它崩溃了.

Now with storyboard I'm lost. I created a UIviewController subclass, I tried adding init(nib name) and init(coder) but it crashes.

这是我的代码:

   //
    //  SecondViewController.swift
    //  tabTestWithSwift
    //
    //  Created by Marianna Ruggieri  on 02/11/14.
    //  Copyright (c) 2014 Marianna Ruggieri. All rights reserved.
    //

    import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        println("init with nib")
        super.init()
        tabBarItem.title = "test"
    }

    required init(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        print("init coder")
        super.init()
    }

}

查看以下屏幕截图: http://oi62.tinypic.com/10ogwsh.jpg

See this screenshot: http://oi62.tinypic.com/10ogwsh.jpg

我没有做任何特别的事情.启动一个TabBar项目,并编辑代码,如上面的代码所示.就是这样!!!!

I didn't do anything special. Started a TabBar project and edited the view controller as you see in the code above. That's it!!!!

如果我使用情节提要,哪种方法是初始化UIViewController的正确方法?我应该在哪里放置视图控制器的所有设置?对于某些设置,加载为时已晚.

Which is the correct way to initialize a UIViewController if I'm using storyboard? Where should I put all the settings for my view controller? The load is too late for certain settings.

推荐答案

您正在通过调用super.init()在您应该调用init(nibName:nibBundleOrNil)的位置创建循环. init()的超级实现会调用您的方法,而该方法又会再次调用super init.

You are creating a loop by calling super.init() where you should be calling init(nibName:nibBundleOrNil). The super implementation of init() calls your method, which in turn calls super init again.

这篇关于SWIFT uiviewcontroller初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆