【问题标题】:Custom UIViewController's view is not being shown自定义 UIViewController 的视图未显示
【发布时间】:2015-07-18 15:27:52
【问题描述】:

所以我有这个视图控制器,它有一个 xib 文件,我检查了文件的所有者和视图出口,类和 xib 文件都已连接。但是如果我想推送这个视图控制器,即使它的所有方法都被调用,它也会显示一个空的:viewDidLoadviewWillAppear

self.navigationController?.presentViewController(
  TourViewController(nibName: "TourViewController", bundle: nil), animated: false, completion: nil
)

这是视图控制器:

class TourViewController: TLViewController {

  override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    println("nib init")
  }

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    println("didLoad")

  }

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    println("appear")
  }

}

我更改了颜色背景,还为 XIB 添加了一个标签

【问题讨论】:

  • 我从未在 Swift 中使用过 XIB 文件
  • 使用实时查看调试,看看是否有东西覆盖它
  • @DreamingInBinary,实时查看调试是一个了不起的工具,谢谢,我需要看看 WWDC 视频

标签: ios swift uiviewcontroller xib


【解决方案1】:

嗯,问题出在我的 CUSTOM TLViewController

override func loadView() {
    super.loadView()

    screen = UIScreen.mainScreen().bounds
    visibleFrame = CGRectMake(
      0.0,
      20.0 + 44.0,
      screen.size.width,
      screen.size.height - (20.0 + 44.0)
    )

    view = UIView(frame: screen)
    view.backgroundColor = UIColor.whiteColor()

    self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.tintColor = UIColor.backgroundMenu()
    self.navigationController?.navigationBar.titleTextAttributes =
      [NSForegroundColorAttributeName: UIColor.backgroundMenu()]
    // Back Button
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "",
      style: .Plain, target: nil, action: nil)

    var menuBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
    menuBtn.setImage(UIImage(named: "menu-ico"), forState: UIControlState.Normal)
    menuBtn.addTarget(self, action: Selector("menu"), forControlEvents:  UIControlEvents.TouchUpInside)
    leftMenuButtonItem = UIBarButtonItem(customView: menuBtn)
  }

所以我改变了

class TourViewController: TLViewController 

class TourViewController: UIViewController

【讨论】:

  • 你不应该调用 super.loadView()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多