我是故事板编程的新手。我设计了一个故事板(比如SB1),它的viewcontroller中有两个功能。我有另一个故事板(SB2),它有两个按钮。从两个按钮我想显示SB1,但是当我按下button1,然后执行func1,如果我按下button2,那么应该执行fun2。任何建议。
我的代码如下。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let viewController = segue.destinationViewController as? StotyBoard1ViewController
where segue.identifier == "LocatorSegue" {
viewController.func1()
}
//how to invoke SB1 Func2 from here
}
@IBAction func button1Click(sender: AnyObject) {
self.performSegueWithIdentifier("SB1Segue", sender: self)
}
@IBAction func button2Click(sender: AnyObject) {
{
self.performSegueWithIdentifier("SB1Segue", sender: self)
}
答案 0 :(得分:1)
制作一个BOOL
类型的全局变量让我们在isCallFunc1
课程中说SB1
,现在在prepareForSegue
分配BOOL
变量的值如同
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let viewController = segue.destinationViewController as? StotyBoard1ViewController
if segue.identifier == "SB1Segue" {
viewController.isCallFunc1 = true
} else {
viewController.isCallFunc1 = false
}
}
根据BOOL变量,现在在SB1的viewWillAppear
中你必须调用适当的函数,如
if isCallFunc1 {
function1()
} else {
function2()
}
同时更改button2Click的标识符以区分标识符
@IBAction func button1Click(sender: AnyObject) {
self.performSegueWithIdentifier("SB1Segue", sender: self)
}
@IBAction func button2Click(sender: AnyObject) {
{
self.performSegueWithIdentifier("SB1SegueOther", sender: self)
}
答案 1 :(得分:1)
你可以这样做: -
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
var sender: UIButton?
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let viewController = segue.destinationViewController as? StotyBoard1ViewController {
if segue.identifier == "SB1Segue" {
if self.sender == button1 {
viewController.func1()
} else if self.sender == button2 {
viewController.func2()
}
}
}
@IBAction func button1Click(sender: AnyObject) {
self.sender = sender as! UIButton
self.performSegueWithIdentifier("SB1Segue", sender: self)
}
@IBAction func button2Click(sender: AnyObject) {
{
self.sender = sender as! UIButton
self.performSegueWithIdentifier("SB1Segue", sender: self)
}