程序化UINavigationController视图转换,首先显示第二个视图

时间:2015-12-13 09:09:17

标签: ios swift

我正在尝试以编程方式创建一个导航控制器,在按下按钮时转换到另一个视图。在其当前状态下,当点击按钮时,第二视图被放置在第一视图的“后面”而没有水平过渡。由于此时我仍然可以看到按钮,如果我再次点击按钮,水平过渡现在可以工作,但第二个视图仍然位于第一个视图的后面。任何人都可以让我知道我错过了什么吗?

谢谢!

ViewController.swift

import UIKit

class ViewController: UINavigationController {

    var btn: UIButton!

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

        self.view.backgroundColor = UIColor.whiteColor()
        createUI()
    }

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

    func createUI() {
        btn = UIButton(frame: CGRect(x: 10, y: 100, width: 100, height: 50))
        btn.backgroundColor = UIColor.orangeColor()
        btn.setTitle("GO", forState: UIControlState.Normal)
        btn.addTarget(self, action: "go", forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn)
    }

    func go() {
        println("go")
        var view = SecondViewController()
        self.pushViewController(view, animated: true)
    }

}

SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

    var lbl: UILabel!

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

        self.view.backgroundColor = UIColor.redColor()
        createUI()
    }

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

    func createUI() {
        lbl = UILabel(frame: CGRect(x: 50, y: 200, width: 200, height: 50))
        lbl.backgroundColor = UIColor.greenColor()
        lbl.text = "LABEL"
        self.view.addSubview(lbl)
    }

}

first view

second view under first view

2 个答案:

答案 0 :(得分:0)

在jovanjovanovic上面的评论之后对文件进行了以下更新。现在,初始控制器被设置为导航控制器,其根视图参数为FirstViewController。现在似乎工作正常,但希望这是一种可以接受的方式。

<强> AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        var rootView = UINavigationController(rootViewController: FirstViewController())
        window?.rootViewController = rootView
        window?.makeKeyAndVisible()
        return true
    }

    func applicationWillResignActive(application: UIApplication) {

    }

    func applicationDidEnterBackground(application: UIApplication) {

    }

    func applicationWillEnterForeground(application: UIApplication) {
    }

    func applicationDidBecomeActive(application: UIApplication) {
    }

    func applicationWillTerminate(application: UIApplication) {
    }

}

<强> FirstViewController

import UIKit

class FirstViewController: UIViewController {
    var btn: UIButton!

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

        self.view.backgroundColor = UIColor.whiteColor()
        createUI()
    }

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

    func createUI() {
        btn = UIButton(frame: CGRect(x: 10, y: 100, width: 100, height: 50))
        btn.backgroundColor = UIColor.orangeColor()
        btn.setTitle("GO 1", forState: UIControlState.Normal)
        btn.addTarget(self, action: "go", forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(btn)
    }

    func go() {
        println("go")
        var view = SecondViewController()
        self.navigationController?.pushViewController(view, animated: true)
    }
}

<强> SecondViewController

import UIKit

class SecondViewController: UIViewController {

    var lbl: UILabel!

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

        self.view.backgroundColor = UIColor.redColor()
        createUI()
    }

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

    func createUI() {
        lbl = UILabel(frame: CGRect(x: 50, y: 200, width: 200, height: 50))
        lbl.backgroundColor = UIColor.greenColor()
        lbl.text = "LABEL"
        self.view.addSubview(lbl)
    }

}

答案 1 :(得分:0)

看起来像你的&#34; ViewController.swift&#34;实际上是UINavigationController - 它不应该对它有任何意见。您应该拥有UINavigationController,用于管理转换和堆栈,以及其他ViewControllers(继承自UIViewController,而不是UINavigationController),用于您要显示的内容。查看一些教程,也许这个 - makeapppie.com/tag/uinavigationcontroller-in-swift