我有困難,我覺得應該工作,我想知道是否有一些事情,我不知道初始化IBOutlets的值。如何使用初始化程序爲IBOutlet賦值?
所以我創建了一個有三個IBOutlets的類。然後,我爲這些插座變量創建了初始化器。當我創建該類的新實例並傳遞硬編碼值時,我得到無作爲回報。
所以我println()編輯傳入的settingLabel值以確保傳入的參數確實帶有一個值,它返回爲「hello」。然後我嘗試將值分配給設置標籤文本,但是當我調用println(settingsLabel.text)時返回爲無。
(線程1:EXV_BAD_INSTRUCTION(代碼= EXC_I386_INVOP,子碼=爲0x0))
我的問題是:
缺少什麼我在這裏和/或如何分配一個IBOutlet一個初始值的值?
這是我調用初始化:
let cell = SettingCell(settingLabel: "hello", settingSwitch: true, timeSetting: 3)
這是我SettingCell.swift類:
class SettingCell: UITableViewCell {
@IBOutlet weak var settingsLabel: UILabel!
@IBOutlet weak var settingsSwitch: UISwitch!
@IBOutlet weak var timeSetting: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
init(settingLabel: String, settingSwitch: Bool, timeSetting: NSNumber) {
println(settingLabel) // returns hello <--------------------
super.init(style: .Default, reuseIdentifier: "SwitchSettingCell")
settingsLabel.text = settingLabel
self.settingsSwitch.on = settingSwitch
self.timeSetting.text = timeSetting.description as String
println(settingsLabel.text) // returns nil <--------------------
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var cellDelegate: SettingCellDelegate?
@IBAction func handledSwitchChange(sender: UISwitch) {
self.cellDelegate?.didChangeSwitchState(sender: self, isOn:settingsSwitch!.on)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
myStoryboard:
的settingsLabel它不是一個可選 –
我是在故障排除與自選玩,感謝指出了這一點,我已經測試並更新了帖子以反映當前失敗的代碼。 –