以编程方式在Swift中设置Root View Controller

时间:2014-10-22 18:00:52

标签: ios swift

我不确定为什么这么琐事,但我的快速翻译速度很慢。无论如何,我无法弄清楚它在抱怨什么。我要做的就是设置根视图控制器,并且编译器发出错误说“Splash pageController没有名为init的成员”

这是我的应用代表:

  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 {




    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

我知道非常基本。通常,在objective-c中,我将初始化一个nib名称并将视图控制器设置为该名称。 swift的主要区别是什么?提前谢谢,希望我很清楚。

1 个答案:

答案 0 :(得分:2)

这一行:

var rootview: SplashViewController = SplashViewController()

...调用SplashViewController的init方法。但是你没有给SplashViewController任何init方法。给它一个。

我通常做的是在init覆盖中指定笔尖;例如:

override init() {
    super.init(nibName:"SplashViewController", bundle:nil)
}

如果你没有沿着那些行做某事,视图控制器将找不到它的笔尖,你最终会得到一个黑屏。

请注意,该代码很可能会为您提供一条新的错误消息,抱怨您没有实现init(coder:);所以你必须实现它,即使只是在意外调用时抛出错误:

required init(coder: NSCoder) {
    fatalError("NSCoding not supported")
}