[英]SWIFT uiviewcontroller init
I am starting to use Swift .我开始使用Swift 。
with Objective-C when I could avoid using storyboard, initializing a view controller was easy.使用Objective-C,当我可以避免使用故事板时,初始化视图控制器很容易。 I would just create a UIViewController
subclass, and I would put all the initialization code inside initWithNibName
.我只会创建一个UIViewController
子类,并将所有初始化代码放在initWithNibName
。
Now with storyboard I'm lost.现在有了故事板,我迷路了。 I created a UIviewController
subclass, I tried adding init
(nib name) and init
(coder) but it crashes.我创建了一个UIviewController
子类,我尝试添加init
(笔尖名称)和init
(编码器),但它崩溃了。
This is my code:这是我的代码:
//
// 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()
}
}
See this screenshot: http://oi62.tinypic.com/10ogwsh.jpg看到这个截图: http : //oi62.tinypic.com/10ogwsh.jpg
EDIT: I didn't do anything special.编辑:我没有做任何特别的事情。 Started a TabBar
project and edited the view controller as you see in the code above.启动一个TabBar
项目并编辑视图控制器,如您在上面的代码中看到的那样。 That's it!!!!就是这样!!!!
Which is the correct way to initialize a UIViewController
if I'm using storyboard?如果我使用情节UIViewController
哪个是初始化UIViewController
的正确方法? Where should I put all the settings for my view controller?我应该把我的视图控制器的所有设置放在哪里? The load is too late for certain settings.对于某些设置,加载为时已晚。
You are creating a loop by calling super.init()
where you should be calling init(nibName:nibBundleOrNil)
.您正在通过调用super.init()
创建一个循环,您应该在其中调用init(nibName:nibBundleOrNil)
。 The super implementation of init()
calls your method, which in turn calls super init again. init()
的 super 实现调用您的方法,该方法又再次调用 super init。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.