未捕获的异常“ NSInvalidArgumentException”,原因:“-[UITableViewSection视图]:无法识别的选择器已发送到实例0x600003721110”

时间:2019-05-11 15:10:42

标签: swift xcode uisegmentedcontrol

当我进入设置视图控制器时,发生错误。设置具有一个UISegmentedControl,该控件通过SettingsViewcontroller.swift ViewDidLoad()连接到theme.swift。

我一直在关注一个教程,但是当我将Int值类型添加到枚举声明中时,关于主题枚举的错误消失了。 ThemeManager结构通过rawValue返回主题。

我很确定该错误是由settingsViewController.swift引起的。由于加载settingsViewController.swift时执行的唯一代码是在viewDidLoad()方法中,所以我认为问题就在这里。

未捕获异常的原因是“ UITableViewSection视图”,我猜这是UISegmentControl,因为唯一的其他部分是“应用”按钮,该应用的代码直到按下时才运行?!

我一直在阅读类似的错误,并且收集到在不包含该方法的对象上正在调用方法。我没有看到类似的内容,但是我不确定Theme.swift中的第30行。由于多个错误,我不得不更改教程代码。要么该主题枚举都不应该是Int(第13行Theme.swift)。

Theme.swift

import UIKit

let SelectedThemeKey = "SelectedTheme"

enum Theme : Int {
    case Default, Dark, Graphical

    var mainColor: UIColor {
        switch self {
        case .Default:
            return UIColor(red: 87.0/255.0, green: 188.0/255.0, blue: 95.0/255.0, alpha: 1.0)
        case .Dark:
            return UIColor(red: 242.0/255.0, green: 101.0/255.0, blue: 34.0/255.0, alpha: 1.0)
        case .Graphical:
            return UIColor(red: 10.0/255.0, green: 10.0/255.0, blue: 10.0/255.0, alpha: 1.0)
        }
    }
}

struct ThemeManager {
    static func currentTheme() -> Theme {
        if let storedTheme = (UserDefaults.standard.value(forKey: SelectedThemeKey) as AnyObject).integerValue {
            return Theme(rawValue: storedTheme)!
        } else {
            return .Default
        }
    }

    static func applyTheme(theme: Theme) {
        // 1
        UserDefaults.standard.setValue(theme.rawValue, forKey: SelectedThemeKey)
        UserDefaults.standard.synchronize()

        // 2
        let sharedApplication = UIApplication.shared
        sharedApplication.delegate?.window??.tintColor = theme.mainColor
    }
}

SettingsViewcontroller.swift

import UIKit

class SettingsViewController: UITableViewController {

    @IBOutlet weak var themeSelector: UISegmentedControl!
    @IBOutlet weak var applyButton: UIButton!

    //@IBOutlet weak var settingsTableView: UITableView!

    //let settingsTableOptions = ["Turn On Dark Mode"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register custom cell
        //let nib = UINib(nibName: "settingsTableCell", bundle: nil)
        //settingsTableView.register(nib, forCellReuseIdentifier: "Settings")

        themeSelector.selectedSegmentIndex = ThemeManager.currentTheme().rawValue
    }

    func dismiss() {
        self.dismiss(animated: true, completion: nil)
    }

    @IBAction func applyTheme(sender: UIButton) {
        if let selectedTheme = Theme(rawValue: themeSelector.selectedSegmentIndex) {
            ThemeManager.applyTheme(theme: selectedTheme)
        }
        dismiss()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
    }
}



I've setup the story board for the settings view exactly as the tutorial, and don't recognise any options that could cause my problem.

0 个答案:

没有答案