Xcode(11.2)iOS模拟器在启动时显示黑屏

时间:2019-12-30 03:38:46

标签: swift xcode realm

一切都很好,但是当我试图弄清楚如何以编程方式实现剧集而不使用情节提要时,我开始遇到这个问题。当我尝试运行该应用程序时,我只能看到黑屏,并且日志中没有错误。

我尝试注释掉segue代码,尝试删除模拟器中的所有内容,然后重新启动模拟器,Xcode和我的mac,但无济于事。

一些帮助将不胜感激。

这是我的代码...

import UIKit
import RealmSwift

@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.

        do { _ = try Realm()
        } catch { print("Error initializing new realm, \(error)") }

        if let window = window {
            let navController = UINavigationController(rootViewController: FirstViewController())
            window.backgroundColor = UIColor.white
            window.rootViewController = navController
            window.makeKeyAndVisible()
        }
        return true
    }
}

import UIKit
import RealmSwift

class FirstViewController: UITableViewController {

    let realm = try! Realm()

    var days : Results<Days>?
    var workouts : Results<Workouts>?

    var daysOfWeek : [String] = ["Monday", "Tuesday", "Wednsday", "Thursday", "Friday", "Saturday", "Sunday"]

    var indexCheck : Int = 0

    let picker = UIPickerView()
    var textField1 = UITextField()
    var textField2 = UITextField()



    //MARK: - viewDidLoad()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        picker.delegate = self
        picker.dataSource = self


        navigationItem.title = "Workouts"
        navigationController?.navigationBar.prefersLargeTitles = true

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

        let addBarButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addWorkout))
        self.navigationItem.rightBarButtonItem = addBarButton


        loadDays()
    }

    //MARK: - Add a New Workout
    @objc func addWorkout() {

        var containsDay = false
        var counter = 0

        let alert = UIAlertController(title: "New Workout", message: "Please name your workout...", preferredStyle: .alert)

        let addAction = UIAlertAction(title: "Add Workout", style: .default) { (UIAlertAction) in
            //Add day and workout to database
            //if day exists, append workout to the existing day.
            //if day doesn't exist, create a day and append workout to newly created day object.

            //First, we have to create an initial days object...
            //Need to check if ANY of the weekdays == picked day, only execute then.
            if self.days?.isEmpty == false {
                for i in 0...(self.days!.count - 1) {

                    if self.days?[i].weekday == self.daysOfWeek[self.picker.selectedRow(inComponent: 0)] {
                        containsDay = true
                        counter = i
                        break
                    }
                }

                if containsDay == true {
                    let newWorkout = Workouts()
                    newWorkout.title = self.textField2.text!

                    try! self.realm.write {
                        self.days?[counter].workout.append(newWorkout)
                        self.loadDays()
                    }
                } else {
                    let dow = Days()
                    let newWorkout = Workouts()
                    dow.weekday = self.daysOfWeek[self.picker.selectedRow(inComponent: 0)]
                    newWorkout.title = self.textField2.text!
                    dow.workout.append(newWorkout)
                    self.save(newDay: dow)
                }
            } else {
                let dow = Days()
                let newWorkout = Workouts()
                dow.weekday = self.daysOfWeek[self.picker.selectedRow(inComponent: 0)]
                newWorkout.title = self.textField2.text!
                dow.workout.append(newWorkout)

                self.save(newDay: dow)
            }
        }

        alert.addTextField { (alertTextField1) in
            alertTextField1.placeholder = "Day of Week"
            alertTextField1.text = self.textField1.text
            self.textField1 = alertTextField1
            alertTextField1.inputView = self.picker
        }

        alert.addTextField { (alertTextField2) in
            alertTextField2.placeholder = "Muscle Group"
            self.textField2 = alertTextField2
            alertTextField2.inputView = nil
        }


        alert.addAction(addAction)
        present(alert, animated: true, completion: nil)
    }


    //MARK: - TableView DataSource and Delegate Methods
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label = UILabel()
        label.text = days?[section].weekday ?? "Section Header"
        label.backgroundColor = UIColor.lightGray
        return label
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return days?.count ?? 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return days?[section].workout.count ?? 0
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let workout = days?[indexPath.section].workout[indexPath.row].title ?? "Workout"

        cell.textLabel?.text = "\(workout)  Section:\(indexPath.section) Row:\(indexPath.row)"

        return cell
    }


    //MARK: - Swipe To Delete
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {
            try! realm.write {
                realm.delete((days?[indexPath.section].workout[indexPath.row])!)

                tableView.beginUpdates()

                tableView.deleteRows(at: [indexPath], with: .automatic)

                if days?[indexPath.section].workout.isEmpty == true {
                    realm.delete((days?[indexPath.section])!)
                    let indexSet = IndexSet(arrayLiteral: indexPath.section)
                    tableView.deleteSections(indexSet, with: .automatic)
                }

                tableView.endUpdates()
            }
        }
    }


    //MARK: - Load Data
    func loadDays() {
        days = realm.objects(Days.self)
        tableView.reloadData()
    }

    //MARK: - Save Data
    func save(newDay : Days) {
        do {
            try realm.write {
                realm.add(newDay)
            }
        } catch {
            print("Error saving day \(error)")
        }
        self.loadDays()
    }


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let vc = SecondViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let destinationVC = segue.destination as! SecondViewController

        if let indexPath = tableView.indexPathForSelectedRow {
            destinationVC.selectedWorkout = days?[indexPath.section].workout[indexPath.row]
        }
    }
}


//MARK: - PickerView Delegate Methods
extension FirstViewController : UIPickerViewDelegate, UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 7
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return daysOfWeek[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        textField1.text = daysOfWeek[row]
    }

}

1 个答案:

答案 0 :(得分:1)

我想您没有从 Storyboard 中设置您的初始视图控制器。这意味着,您没有将情节提要文件中的任何视图控制器设置为是初始视图控制器,如下图所示:

enter image description here


因此未设置您的k。您需要手动初始化window属性。使用以下内容而不是您的版本:

window

但是如果您有func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. do { _ = try Realm() } catch { print("Error initializing new realm, \(error)") } let window = UIWindow(frame: UIScreen.main.bounds) let navController = UINavigationController(rootViewController: FirstViewController()) window.rootViewController = navController window.makeKeyAndVisible() self.window = window //this is important because otherwise your window will be deallocated immediately return true } ,请关注iOS 13: Swift - 'Set application root view controller programmatically' does not work