[英]Programmatically Setting Root View Controller in Swift
I'm not sure why this is so trivial, but my Swift translation has been a slow one.我不知道为什么这会如此微不足道,但我的 Swift 翻译一直很慢。 Anyways, I can't figure out what it is complaining about.
无论如何,我无法弄清楚它在抱怨什么。 All I am trying to do is set the root view controller, and the compiler spits an error saying:
我想要做的就是设置根视图控制器,编译器吐出一个错误说:
"Splash pageController does not have a member named init"
“Splash pageController 没有名为 init 的成员”
Here's my app delegate:这是我的应用程序委托:
var window: UIWindow?
var CLIENT_KEY = c_key()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
var url_string = "XXXX=\(CLIENT_KEY.client_key)"
var g_home_url = String.stringWithContentsOfURL(NSURL.URLWithString(url_string), encoding: NSUTF8StringEncoding, error: nil)
if (g_home_url? != nil){
NSUserDefaults.standardUserDefaults().setObject(g_home_url, forKey: "showUrl")
}
let DocumentsDirectory = NSSearchPathDirectory.DocumentationDirectory
let UserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(DocumentsDirectory, UserDomainMask, true){
if paths.count > 0{
if let dirPath = paths[0] as? String {
// let url: NSURL = NSURL.URLWithString("\(g_home_url)/XXX\(CLIENT_KEY.client_key)")
// println(url)
var err: NSError?
var g_home_url = NSUserDefaults.standardUserDefaults().objectForKey("showUrl") as String
println(g_home_url)
var image = UIImage(data: NSData(contentsOfURL: NSURL.URLWithString("\(g_home_url)XXX=\(CLIENT_KEY.client_key)")))
let writePath = dirPath.stringByAppendingPathComponent("splash_page.png")
UIImagePNGRepresentation(image)
}
}
}
//This is where the error shows up
var rootview: SplashViewController = SplashViewController()
if let window = window {
window.rootViewController = rootview;
}
SplashViewController飞溅视图控制器
class SplashViewController: UIViewController {
}
Very basic I know.我知道的非常基本。 Normally, in Objective-C I would init a nib name and set the view controller to that.
通常,在 Objective-C 中,我会初始化一个笔尖名称并将视图控制器设置为该名称。 What's the main difference in Swift?
Swift 的主要区别是什么?
This line:这一行:
var rootview: SplashViewController = SplashViewController()
...calls SplashViewController's init
method. ...调用 SplashViewController 的
init
方法。 But you have not given SplashViewController any init
method.但是你没有给 SplashViewController 任何
init
方法。 Give it one.给一个。
What I usually do is specify the nib in my init
override;我通常做的是在我的
init
覆盖中指定笔尖; for example:例如:
override init() {
super.init(nibName:"SplashViewController", bundle:nil)
}
If you don't do something along those lines, the view controller won't find its nib and you'll end up with a black screen.如果你不按照这些方式做一些事情,视图控制器将找不到它的笔尖,你最终会得到一个黑屏。
Note that that code will very likely net you a new error message complaining that you didn't implement init(coder:)
;请注意,该代码很可能会给您带来一条新的错误消息,抱怨您没有实现
init(coder:)
; so you'll have to implement it, even if only to throw an error if it is accidentally called:所以你必须实现它,即使只是在意外调用时抛出错误:
required init(coder: NSCoder) {
fatalError("NSCoding not supported")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.