在viewDidLoad()

时间:2015-08-20 09:03:12

标签: ios swift uibutton viewdidload

我在故事板中为我的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)
    }
    //...
}

我以同样的方式创建的第一个星期数字是不是零,但是第二个“加上英国”#B;总是零。我不知道出了什么问题。有人能帮助我吗?

UPDATE ::

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

1 个答案:

答案 0 :(得分:-1)

  

选择按钮并右键单击它,看看出口是否存在。如果连接不可用,请将插座提供给按钮。即使它不工作删除存在的插座和再次创建插座。把断点和Debug it

放在一起