如何使用 Storyboard 中制作的 UIBarButtonItem 设置动作
How to set up an action with UIBarButtonItem made in Storyboard
我知道如果我以编程方式这样做,我会创建操作菜单项:
share = UIBarButtonItem(
title: "Continue",
style: .Plain,
target: self,
action: "Pun"
我正在尝试使用情节提要来做到这一点。有没有简单的方法可以做到这一点?我正在尝试创建一个共享按钮,它将复制一个字符串并提示用户共享它(通过电子邮件、打印机等)。谢谢。这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
let screenSize: CGRect = UIScreen.mainScreen().bounds
let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width:100, height: 50))
@IBOutlet weak var share: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
btn1.backgroundColor = UIColor.clearColor()
btn1.layer.cornerRadius = 5
btn1.layer.borderWidth = 1
btn1.layer.borderColor = UIColor.redColor().CGColor
btn1.setTitle("1", forState: .Normal)
scrollView.addSubview(btn1)
scrollView.contentSize.height = 900
/*share = UIBarButtonItem(
title: "Continue",
style: .Plain,
target: self,
action: "Pun"
)*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Add this
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var newFrame = btn1.frame
newFrame.size.width = scrollView.bounds.width
btn1.frame = newFrame
}
}
您需要创建要执行的函数,然后转到故事板并单击 UI 栏按钮项。然后,您需要单击属性菜单最右侧的连接检查器。然后拖动 "Touch Up Inside" 到视图和 select 函数。
我知道如果我以编程方式这样做,我会创建操作菜单项:
share = UIBarButtonItem(
title: "Continue",
style: .Plain,
target: self,
action: "Pun"
我正在尝试使用情节提要来做到这一点。有没有简单的方法可以做到这一点?我正在尝试创建一个共享按钮,它将复制一个字符串并提示用户共享它(通过电子邮件、打印机等)。谢谢。这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
let screenSize: CGRect = UIScreen.mainScreen().bounds
let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width:100, height: 50))
@IBOutlet weak var share: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
btn1.backgroundColor = UIColor.clearColor()
btn1.layer.cornerRadius = 5
btn1.layer.borderWidth = 1
btn1.layer.borderColor = UIColor.redColor().CGColor
btn1.setTitle("1", forState: .Normal)
scrollView.addSubview(btn1)
scrollView.contentSize.height = 900
/*share = UIBarButtonItem(
title: "Continue",
style: .Plain,
target: self,
action: "Pun"
)*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Add this
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var newFrame = btn1.frame
newFrame.size.width = scrollView.bounds.width
btn1.frame = newFrame
}
}
您需要创建要执行的函数,然后转到故事板并单击 UI 栏按钮项。然后,您需要单击属性菜单最右侧的连接检查器。然后拖动 "Touch Up Inside" 到视图和 select 函数。