2017-03-27 4 views
0

ボタンを押したときにFirstViewからSecondViewにプログラムで切り替える必要がありますが、私はストーリーボードを使用しません。これは私のコードです:ビューコントローラをプログラム的に変更する

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate { 
    var window: UIWindow? 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     let initialViewController = FirstController() 
     window = UIWindow(frame: UIScreen.main.bounds) 
     self.window!.rootViewController = initialViewController 
     self.window!.makeKeyAndVisible() 
     return true 
    } 
} 

FirstView.swift

class FirstView: UIView { 
    var btnImage = UIButton(image: "Image01") 

    override init(frame: CGRect){ 
     print("FirstView init") 
     super.init(frame: screenSize) 
     self.backgroundColor = UIColor.red 
     self.btnImage.translatesAutoresizingMaskIntoConstraints = false 
     addSubview(self.btnImage) 
     self.btnImage.alignLeftOfViewVoid(padding: 12) 
     self.btnImage.addTarget(self.parentViewController, action: #selector (FirstController.onClickListener(object:)), for: .touchUpInside) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    deinit{ 
     self.removeFromSuperview() 
    } 
} 

SecondView.swift

class SecondView: UIView { 

    override init(frame: CGRect){ 
     print("SecondView init") 
     super.init(frame: screenSize) 
     self.backgroundColor = UIColor.green 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    deinit{ 
     self.removeFromSuperview() 
    } 
} 

FirstController.swift

class FirstController: UIViewController { 
    init(){ 
     super.init(nibName: nil, bundle: nil) 
     print("MainController") 
     self.view = FirstView() 
     self.view.backgroundColor=UIColor.red 
    } 

    override var prefersStatusBarHidden: Bool { 
     return true 
    } 

    func onClickListener(object : UIButton!) { 
     print("Click to view 2") 
     weak var view = SecondController() 
     self.navigationController?.pushViewController(view!, animated: true) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

SecondController.swift

class SecondController: UIViewController { 
    init(){ 
     super.init(nibName: nil, bundle: nil) 
     print("SecondController") 
     self.view = SecondView() 
     self.view.backgroundColor=UIColor.green 
    } 

    override var prefersStatusBarHidden: Bool { 
     return true 
    } 

    func onClickListener(object : UIButton!) { 
     print("Click to view 1") 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

私は別のソリューションでそれを試してみましたが、それはうまくいきませんでした。 は非常に

+0

あなたはどんなタイプのスイッチを探していますか?戻るボタンが欲しいですか?それともタブを探していますか?ボタンをタップすると、下からスライドしたいだけですか? –

答えて

1

をありがとうあなたはナビゲーションコントローラにFirstControllerを配置する必要があります。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    let initialViewController = FirstController() 
    let navController = UINavigationController(rootViewController: initialViewController) 
    window = UIWindow(frame: UIScreen.main.bounds) 
    self.window!.rootViewController = navController 
    self.window!.makeKeyAndVisible() 
    return true 
} 

今、あなたはFirstControllerからSecondControllerをプッシュすることができます。

0

ありがとうございました! ...

新AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    let initialViewController = FirstController() 
    let navController = UINavigationController(rootViewController: initialViewController) 
    window = UIWindow(frame: UIScreen.main.bounds) 
    self.window?.rootViewController = navController 
    self.window?.makeKeyAndVisible() 
    return true 
} 

...

そして、あなたは唯一の最初のページにナビゲーションバーを非表示にする場合::完璧に動作

追加FirstController:

override func viewWillAppear(_ animated: Bool) { 
    self.navigationController?.setNavigationBarHidden(true, animated: animated) 
    super.viewWillAppear(animated) 
} 

override func viewWillDisappear(_ animated: Bool) { 
    self.navigationController?.setNavigationBarHidden(false, animated: animated) 
    super.viewWillDisappear(animated) 
} 
関連する問題