如何在ViewController中添加一个简单的按钮?
How to add a simple button in a ViewController?
我有以下代码。
import UIKit
class ViewController: UIViewController {
var button : UIButton?
override func viewDidLoad() {
super.viewDidLoad()
button = UIButton.buttonWithType(UIButtonType.System) as UIButton?
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我收到以下错误
错误:
'AnyObject' is not convertible to 'UIButton?'
我知道我可能做错了根本性的事情。我想知道那是什么。
据我所知:
我已将 button 声明为 Optional UIButton
- 我认为这意味着 button 的值可以取消设置或为 nil
因此,
在初始化它时提到类型 as UIButton?
这样对吗?
请尝试以下代码:
var button = UIButton(frame: CGRectMake(150, 240, 75, 30))
button.setTitle("Next", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = UIColor.greenColor()
self.view.addSubview(button)
这段代码应该可以完成工作。
button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
在这种情况下,使用 !
完成的 force cast
是一个安全的选项,因为文档确实保证 returns 方法 UIButton
.
您还可以在 属性:
的声明期间创建按钮
class ViewController: UIViewController {
var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
...
这样就不需要将 属性 声明为可选类型。
您不能按照您正在执行的方式强制转换为可选的 UIButton。转换为可选 UIButton 的正确方法是:
button = UIButton.buttonWithType(UIButtonType.System) as? UIButton
将其解释为:此转换可以是 return nil 或一个 UIButton 对象,从而生成一个可选的 UIButton 对象。
按照下面的代码
var myBtn = UIButton.buttonWithType(UIButtonType.System) as UIButton
//OR
var myBtn = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
//OR
var myBtn = UIButton()
myBtn.setTitle("Add Button To View Controller", forState: .Normal)
myBtn.setTitleColor(UIColor.greenColor(), forState: .Normal)
myBtn.frame = CGRectMake(30, 100, 200, 400)
myBtn.addTarget(self, action: "actionPress:", forControlEvents: .TouchUpInside)
self.view.addSubview(myBtn)
//Button Action
func actionPress(sender: UIButton!)
{
NSLog("When click the button, the button is %@", sender.tag)
}
我有以下代码。
import UIKit
class ViewController: UIViewController {
var button : UIButton?
override func viewDidLoad() {
super.viewDidLoad()
button = UIButton.buttonWithType(UIButtonType.System) as UIButton?
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我收到以下错误
错误:
'AnyObject' is not convertible to 'UIButton?'
我知道我可能做错了根本性的事情。我想知道那是什么。
据我所知: 我已将 button 声明为 Optional UIButton - 我认为这意味着 button 的值可以取消设置或为 nil
因此, 在初始化它时提到类型 as UIButton?
这样对吗?
请尝试以下代码:
var button = UIButton(frame: CGRectMake(150, 240, 75, 30))
button.setTitle("Next", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = UIColor.greenColor()
self.view.addSubview(button)
这段代码应该可以完成工作。
button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
在这种情况下,使用 !
完成的 force cast
是一个安全的选项,因为文档确实保证 returns 方法 UIButton
.
您还可以在 属性:
的声明期间创建按钮class ViewController: UIViewController {
var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
...
这样就不需要将 属性 声明为可选类型。
您不能按照您正在执行的方式强制转换为可选的 UIButton。转换为可选 UIButton 的正确方法是:
button = UIButton.buttonWithType(UIButtonType.System) as? UIButton
将其解释为:此转换可以是 return nil 或一个 UIButton 对象,从而生成一个可选的 UIButton 对象。
按照下面的代码
var myBtn = UIButton.buttonWithType(UIButtonType.System) as UIButton
//OR
var myBtn = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
//OR
var myBtn = UIButton()
myBtn.setTitle("Add Button To View Controller", forState: .Normal)
myBtn.setTitleColor(UIColor.greenColor(), forState: .Normal)
myBtn.frame = CGRectMake(30, 100, 200, 400)
myBtn.addTarget(self, action: "actionPress:", forControlEvents: .TouchUpInside)
self.view.addSubview(myBtn)
//Button Action
func actionPress(sender: UIButton!)
{
NSLog("When click the button, the button is %@", sender.tag)
}