简体   繁体   English

在 swift 中,当不同的按钮通向同一个 ViewController 时,我如何知道按下了哪个按钮?

[英]How do I know in swift which button was pressed when different buttons lead to the same ViewController?

I have four buttons which all lead to the same view controller.我有四个按钮,它们都通向相同的视图 controller。 I need to know which button was pressed, because the view controller is set up slightly different for each button.我需要知道按下了哪个按钮,因为每个按钮的视图 controller 设置略有不同。 I tried the following: ViewController (called "SecondViewController") where one of the Buttons is pressed我尝试了以下操作:ViewController(称为“SecondViewController”),其中一个按钮被按下

    var index = 0

    @IBAction func Button1(_ sender: UIButton) {
        index = 1
    }
    @IBAction func Button2(_ sender: UIButton) {
        index = 2
    }
    @IBAction func Button3(_ sender: UIButton) {
        index = 3
    }
    @IBAction func Button4(_ sender: UIButton) {
        index = 4
    }


    func getIndex() -> Int{
        return index
    }

The view controller which will be opened afterwards之后打开的视图 controller

// to get functions from SecondViewController
var second = SecondViewController()

let index = second.getIndex()
print(index)

Unfortunately it always prints zero.不幸的是,它总是打印零。 I guess because I set the index to 0 in the beginning, but I do not understand why doesn't the value update, when the Button was pressed.我猜是因为我在开始时将索引设置为 0,但我不明白为什么按下按钮时值不更新。

What can I do?我能做些什么?

I guess you are using segues, so your segue is executing before your IBAction can update your index value.我猜你正在使用 segue,所以你的 segue 在你的 IBAction 可以更新你的索引值之前执行。 There is a similar question & solution here 这里有一个类似的问题和解决方案

So to fix this give your segue an identifier, and call performSegueWithIdentifier from within your IBAction methods.因此,要解决此问题,请为您的 segue 提供一个标识符,并从您的 IBAction 方法中调用performSegueWithIdentifier

SecondViewController (Previous one which contains buttons) SecondViewController(前一个包含按钮)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let firstViewController = segue.destination as? FirstViewController {
        firstViewController.index = self.index
    }
}

FirstViewController (One should be displayed after buttons clicked) FirstViewController(单击按钮后应显示一个)

var index: Int?

override func viewDidLoad() {
    super.viewDidLoad()

    print(index)
}

if I understand you correctly,you will definitely get index as 0.如果我理解正确,你肯定会得到索引为 0。

var index = 0

@IBAction func Button1(_ sender: UIButton) {
    index = 1
}
@IBAction func Button2(_ sender: UIButton) {
    index = 2
}
@IBAction func Button3(_ sender: UIButton) {
    index = 3
}
@IBAction func Button4(_ sender: UIButton) {
    index = 4
}


func getIndex() -> Int{
    return index
}

above code is in SecondViewController, right?上面的代码在 SecondViewController 中,对吧?

then you call code below in another view controller (maybe FirstViewController)然后你在另一个视图中调用下面的代码 controller (也许是 FirstViewController)

// to get functions from SecondViewController
var second = SecondViewController()

let index = second.getIndex()
print(index)

so you get index out of the SecondViewController after it just was initialized and there is no way you can click on buttons and change index before second.getIndex() .因此,您可以在 SecondViewController 刚刚初始化之后从它中获取索引,并且您无法在second.getIndex()之前单击按钮并更改索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下TableView单元格中的按钮时,我怎么知道按下了哪一行单元格的按钮? - When the button in TableView cell pressed, how do I know the button of which row of cell pressed? 如何在按下按钮时更改按钮的颜色,并在按下其他按钮时重置为原始颜色? - How do I change a button's color when pressed and reset to original color when a different button is pressed? 按下按钮时如何将值发送到ViewController - How to send a value to a ViewController when a button is pressed 当我按下按钮推送到另一个ViewController时,如何知道哪个indexPath.row我的按钮? - How can I know which indexPath.row my button in when I press a button to push to another ViewController? 如何知道在 detailCalloutAccessoryView 中按下了哪个按钮 - How to know which button was pressed in a detailCalloutAccessoryView Swift 2.0:当注释数多时,如何指定按哪个注释? 市场 - Swift 2.0: How do I specify which Annotation is pressed when having more then one Annotation? Market Swift-如何使用从ViewController到特定TabBarController的按钮进行搜索 - Swift - How do I segue with a button from ViewController to a specific TabBarController 如何在同一ViewController中初始化两个NSObject实例-Swift - How Do I Initialize Two Instances of NSObject in the same ViewController - Swift 按下按钮时如何使标签滑到屏幕上? SWIFT 2 - How do I make a label slide onto the screen when a button is pressed? SWIFT 2 当按下按钮时,如何获得显示随机文本的标签? [迅速] - How do I get a label to display random text when a button is pressed? [Swift]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM