繁体   English   中英

从UITabBarController显示ViewController标签

[英]presenting ViewController Label from UITabBarController

我是迅速编写IOS应用程序的新手。 我已经用代码在AppDelegate.swift中对TabBar进行了编程,并在MainPageViewController.swift和DetailViewController.swift中使用了2个ViewControllers程序。

    let tabBarController = UITabBarController()
    let mainPageVC = MainPageViewController()
    let detailVC = DetailViewController()
    tabBarController.viewControllers = [mainPageVC,detailVC]
    let mainTabBar = UITabBarItem(title: nil, image: UIImage(named: "03")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "06")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
    let detailTabBar = UITabBarItem(title: nil, image: UIImage(named: "02")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "05")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
    mainPageVC.tabBarItem = mainTabBar
    deviceVC.tabBarItem = deviceTabBar
    mainPageVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
    deviceVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
    UITabBar.appearance().barTintColor = UIColor(red: 211/255, green: 1, blue: 246/255, alpha: 1)
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()

然后我在MainPageViewController.swift中添加了一个Label

class MainPageViewController: UIViewController {
@IBOutlet weak var testLabel: UILabel!
@IBOutlet weak var mainTabBarItem: UITabBarItem!
override func viewDidLoad() {
    super.viewDidLoad()
    let backgraondLayer = color.gl
    backgraondLayer.frame = view.frame
    self.view.layer.insertSublayer(backgraondLayer, atIndex: 0)
    testLabel.backgroundColor = UIColor.blackColor()
}

我不断收到“致命错误:在展开可选值时意外发现nil”错误。 而且,如果我删除“ Label.backgroundColor”代码并在“属性”检查器中设置背景色,则构建应用程序将不会显示标签。

我错过了什么代码? 提前致谢!

如果在testLabel.backgroundColor = UIColor.blackColor()给您“展开一个可选值时为零”,则可能是您的标签未连接到Interface Builder中的控件。 在编辑器中,IBOutlets的声明旁边有一个小圆圈。 空一个表示未连接,并在其中点连接一个圆圈。

如果您仍处于应用程序开发的起步阶段,请考虑在Interface Builder / Storyboard中进行UITabBarController处理。 每次添加新标签时,它将使您免于繁琐的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM