2016-09-29 9 views
11

Xcode 8がコンパイル時に変更されると、識別子付きのviewcontrollerがインスタンシエートされます。私はそれをしました、なぜそれは2つのエラーを与えるのですか?Swift 3、Xcode 8インスタンシエートビューコントローラが動作しない

私はSwift 3と一緒に働いています。私はプログラムでページを変更したいと思います。トピックに関するその他の多くの質問を読んでいます。これらのすべては、識別子付きのビュー・コントローラをインスタンス化します。彼らは新しい言語を採用していません。

@IBAction func switchPage(_ sender: UIButton) { 

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let viewController = 
storyboard.instantiateViewController("secondViewController") as! 
UIViewController 
    self.presentViewController(secondViewController, animated: true, 
completion: nil)  

} 

ありがとうございます。私は、提案されたコードを変更し、1つのエラーだけを受け取ります:オプションの型 'UIStoryboard?アンラップされていない。あなたは '!'を使うつもりでしたか?または '?'?どこかに感嘆符を付けるべきですか?

import UIKit 

class ViewController: UIViewController { 

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

@IBAction func ch(_ sender: UIButton) { 



    let viewController = 
storyboard.instantiateViewController(withIdentifier: 
"secondViewController") as! UIViewController 
    self.present(viewController, animated: true) 



} 




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



} 

答えて

37

このようにしてください。これはこれで私のために働いています

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let viewController = storyboard.instantiateViewController(withIdentifier :"secondViewController") as! UIViewController 
self.present(viewController, animated: true)  
+1

感謝を。それは近いですが、私は1つのエラーが発生しています。助言がありますか? – Aleric

+0

@Aleric私はあなたにこの行を削除するように言ったことはありません 'let storyboard = UIStoryboard(name:" Main "、bundle:nil)'編集された答えを確認してください。 –

+0

それは動作します。素晴らしい仕事Nirav。落ち着いて。 – Aleric

2

let vc = UIStoryboard(name: "ZWLStaticTabVc", bundle: nil).instantiateInitialViewController() 
self.navigationController?.pushViewController(vc!, animated: true) 
3

それは私のために働いています:

let gameScene = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "ViewController") as UIViewController 
     let appDelegate = (UIApplication.shared.delegate as! AppDelegate) 
     appDelegate.window?.rootViewController = gameScene 
関連する問題