ios - UIButton 在 vi​​ewDidLoad() 中始终为 nil

标签 ios swift uibutton viewdidload

我在 Storyboard中的 ViewController 中添加了一个 UIButton。然后我为它创建了一个 IBOutlet 连接。然后在 viewDidLoad() 中,我想使用创建的按钮进行操作,但它始终为零。

class LeftWeekViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var weekNumberBtn: PassThroughButton!
    @IBOutlet weak var weekNumberBtnBottomSpacing: NSLayoutConstraint!
    @IBOutlet weak var plusBtn: UIButton!

    var data: [String] = ["sadasd", "adgfdgfdg", "hgfhgfhgfh", "sadasd", "adgfdgfdg", "hgfhgfhgfh", "sadasd", "adgfdgfdg", "hgfhgfhgfh"]
    var sections: [String] = ["[Ma 26 Mei]", "[Di 27 Mei]", "[Wo 28 Mei]"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let calender = NSCalendar.currentCalendar()
        let dateComponent = calender.components(.CalendarUnitWeekOfYear  | .CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear, fromDate:NSDate())
        weekNumberBtn.setTitle("\(dateComponent.weekOfYear)", forState: UIControlState.Normal)
        weekNumberBtn.layer.cornerRadius = weekNumberBtn.frame.height / 2
        weekNumberBtn.clipsToBounds = true
        self.view.bringSubviewToFront(weekNumberBtn)
        self.tableView.separatorStyle = .None

        weekNumberBtnBottomSpacing.constant = -1 * (self.tableView(self.tableView, heightForHeaderInSection: 0) - 8)

        tableView.registerNib(UINib(nibName: "LeftWeekTableViewCell", bundle: nil), forCellReuseIdentifier: "LeftWeekCell")

        var pencilImage = UIImage(named: "pencil")!
        pencilImage = resizeImage(pencilImage, size: CGSize(width: plusBtn.frame.width - 15, height: plusBtn.frame.height - 15))

        plusBtn.layer.cornerRadius = plusBtn.frame.size.height / 2
        plusBtn.clipsToBounds = true
        plusBtn.setImage(pencilImage, forState: .Normal)
        plusBtn.contentMode = UIViewContentMode.Center
        plusBtn.layer.shadowColor = UIColor.blackColor().CGColor
        plusBtn.layer.masksToBounds = false
        plusBtn.layer.shadowOffset = CGSizeMake(0, 15)
        plusBtn.layer.shadowRadius = 10
        plusBtn.layer.shadowOpacity = 0.4

        self.view.bringSubviewToFront(plusBtn)
    }
    //...
}

我以相同方式创建的第一个 weekNumberBtn 不是 nil,但第二个“plusBtn”始终为 nil。我不知道出了什么问题。有人可以帮助我吗?

更新::

我清理并重建了我的项目,它抛出了一个错误,指出我在创建 ViewController 时传递了错误的 xib 文件名。我已经更改了 xib 文件名,一切正常。

最佳答案

Select the Button and Right click on it see the outlets prepely is there or not. if connections are not available the give the outlet to tha button. even if it not working remove exist outlets And Create outlet again. put the break points and Debug it

关于ios - UIButton 在 vi​​ewDidLoad() 中始终为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113996/

相关文章:

arrays - 在 Swift 3 中对 [Dictionary<String, Any>] 进行排序

ios - UIButton touchDragInside 不起作用

ios - flutter 构建 Xcode : Module 'firebase_analytics' not found

android - 数据显示为空但url在Postman上有效

ios - iTunes Connect 更新应用程序未出现在应用程序商店中

ios - Swift 函数无法尝试从 rest 服务获取 JSON 数组并转换为 swift 对象

swift - NSNumberFormatter.stringFromNumber 会返回 nil 吗?

ios - 如何使用 MPMusicPlayerController 从特定索引播放 MPMediaItemCollection?

iphone - Interface Builder(iPhone dev)自定义按钮背景图像不考虑拉伸(stretch)设置

ios - UIButton 中的重复 UILabel