【问题标题】:Programmatic UINavigationController view transition displaying second view under first程序化 UINavigationController 视图转换在第一个视图下显示第二个视图
【发布时间】:2015-12-14 05:22:30
【问题描述】:

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

谢谢!

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)
    }

}

【问题讨论】:

  • 看起来你的“ViewController.swift”实际上是 UINavigationController - 它不应该有任何视图。您应该拥有 UINavigationController,用于管理转换和堆栈,以及其他 ViewController(从 UIViewController 继承,而不是 UINavigationController)用于您要显示的内容。查看一些教程,也许是这个 - makeapppie.com/tag/uinavigationcontroller-in-swift
  • 谢谢@jovanjovanovic 我用你的建议来编辑我的文件。我在下面发布了更新作为答案。我不确定是否应该将我的答案标记为正确,或者是否应该让您将您的答案贴在下面以便我可以将您的答案标记为正确?
  • 我已将我的评论添加为答案 - 您可以将其标记为正确的。谢谢!

标签: ios swift


【解决方案1】:

似乎您的“ViewController.swift”实际上是 UINavigationController - 它不应该有任何视图。您应该拥有 UINavigationController,用于管理转换和堆栈,以及其他 ViewController(从 UIViewController 继承,而不是 UINavigationController)用于您要显示的内容。查看一些教程,也许是这个 - makeapppie.com/tag/uinavigationcontroller-in-swift

【讨论】:

    【解决方案2】:

    在上面 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)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多