【问题标题】:Building UIButton With Border Initialization使用边框初始化构建 UIButton
【发布时间】:2014-08-31 18:03:44
【问题描述】:

我正在尝试初始化一个自定义 UIButton 类,但我做错了,不知道如何实现它。我构建了一个自定义 UIButton 类并在 IBOutlet 中对其进行了初始化。该按钮工作正常,但没有显示我设置的属性。

import UIKit
import Foundation

class WBCircleButton : UIButton {
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.layer.borderColor = UIColor.blueColor().CGColor
    self.layer.borderWidth = 2
    self.layer.cornerRadius = self.frame.size.width / 2

    }
}

class WBMainViewController: UIViewController {

var timeControl = WBTimeController()

var timer = NSTimer()

@IBOutlet weak var timeDisplay: UILabel!

//this doesn't work as expected ?????
@IBOutlet weak var startButton: WBCircleButton!


//********************************************
@IBAction func startTimer(sender: AnyObject) {

    timeControl.startTimer()

    startButton.setTitle("Stop", forState: UIControlState.Normal)

    let aSelector: Selector = "updateTimerDisplay"

    timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)

}


//********************************************
func updateTimerDisplay () {
    timeDisplay.text = timeControl.timeLabelText
}

//********************************************
override func viewDidLoad() {
    super.viewDidLoad()

}

//********************************************
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    我认为您没有验证某些内容。我创建了一个 Xcode Single View Application 模板项目并执行了以下操作(检查每个步骤以了解您缺少什么)。

    在您的项目导航器中:

    创建一个新的 UIViewController 类文件,将其命名为“WBMainViewController”并在其中设置以下代码:

    import UIKit
    
    class WBMainViewController: UIViewController {
    
        @IBOutlet weak var startButton: WBCircleButton!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            startButton.setTitle("Stop", forState: UIControlState.Normal)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
    }
    

    在您的项目中新建一个 UIButton 类文件,将其命名为“WBCircleButton”并在其中设置以下代码:

    import UIKit
    
    class WBCircleButton: UIButton {
    
        required init(coder decoder: NSCoder) {
            super.init(coder: decoder)
    
            layer.borderColor = UIColor.blueColor().CGColor
            layer.borderWidth = 2
            layer.cornerRadius = self.frame.size.width / 2
        }
    
    }
    

    在界面生成器中:

    选择您的 UIViewController 场景并在 Identity Inspector 中将其类设置为“WBMainViewController”。

    在您的场景中添加一个 UIButton,为其设置自动布局约束,并在 Identity Inspector 中将您的 UIButton 类设置为“WBCircleButton”。

    选择您的 ViewController 场景并单击“显示助手编辑器”。请务必显示您的 ViewController 代码并将您的 startButton IBOutlet 拖到 Interface Builder 场景中的 UIButton 上。

    启动您的项目。

    在模拟器中启动我的项目后,我能够在我的 ViewController 中显示这个大按钮:

    【讨论】:

    • 这很好用。我在 viewController 中定义了自定义按钮类,一旦将它放在单独的文件中并附加,它就会显示属性。我没有意识到在 viewController 中定义按钮类不是要走的路。
    猜你喜欢
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多