按照课程要求,代码已经全部写好,但是在模拟器运行时,发现是一片白的。
主要涉及AppDelegate 和ViewController的问题,但是我还是没明白。
另外也尝试修改main.storyboard中的Class,但是没有实际的作用。
所写代码已打包完成。另附具体代码如下:
本人小白,如若表述不清,课程视频可以参考公众号“91年女神”中Xcode→UITabBarController视图控制器。
FirstSubViewController.swift
import UIKit
class FirstSubViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Item #1"
self.tabBarItem.image = UIImage(named: "home")
self.tabBarItem.selectedImage = UIImage(named: "home")
self.view.backgroundColor = UIColor.red
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
}
SecondSubViewController.Swift
import UIKit
class SecondSubViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Item #2"
self.tabBarItem.image = UIImage(named: "settings")
self.view.backgroundColor = UIColor.purple
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarItem.badgeValue = "8"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
AppDelegate.Swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let viewController1 = FirstSubViewController()
let viewController2 = SecondSubViewController()
let tabViewController = UITabBarController()
tabViewController.viewControllers = [viewController1, viewController2]
tabViewController.selectedIndex = 0
tabViewController.view.backgroundColor = UIColor.red
self.window?.rootViewController = tabViewController
let tabBar = tabViewController.tabBar
let item = tabBar.items![0]
item.image = UIImage(named: "home")
item.title = "home"
let item2 = tabBar.items![1]
item2.image = UIImage(named: "settings")
item2.title = "settings"
item2.badgeValue = "9"
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
相似问题