简体   繁体   English

Swift中viewdidload内部的@IB Action函数

[英]@IB Action function inside of viewdidload in Swift

Is it possible to have an @IB Action function inside of viewDidLoad() ? viewDidLoad()内是否可以有@IB Action函数?

The action is a simple one - a Stepper that increases other label.text values accordingly. 该操作很简单-步进器会相应地增加其他label.text值。 However, the values that the stepper needs to work with depend on the return content of a url - which are only known after the viewDidLoad() of course. 但是,步进器需要使用的值取决于url的返回内容-当然,只有在viewDidLoad()之后才知道。

So I think I can't have the IBaction way up on top before the viewDidLoad(), and the error I get if I try to do my IB action inside of the viewDidLoad() is: 因此,我认为在viewDidLoad()之前我无法将IBaction放在首位,如果尝试在viewDidLoad()内部执行IB操作,则会出现以下错误:

"Only instance methods can be declared 'IBAction' ” “只能将实例方法声明为'IBAction'”

EDIT 编辑

Let me clarify myself, sorry for the confusion. 让我澄清一下,很抱歉造成混乱。 I know I need an outlet to get the UIStepper values from. 我知道我需要一个插座来从中获取UIStepper值。 I have that: 我有这个:

    @IBOutlet weak var stepper: UIStepper!

I then have an action also connected to same UIStepper that will increase/decrease value of a label's text (new_total) accordingly: 然后,我还有一个连接到同一UIStepper的动作,该动作将相应地增加/减少标签文本的值(new_total):

@IBOutlet weak var new_total: UILabel!

@IBAction func step_up_pass(sender: AnyObject) {
    new_total.text = "\(Int(stepper.value))"
    }

However, I want to start out with a value (todays_price) I'm getting back from a json request and use that as a starting point, to multiply it using the stepper and put the multiplied value into the label's text. 但是,我想从一个我从json请求中获取的值(todays_price)开始,并以此为起点,使用步进器对其进行乘法,然后将相乘后的值放入标签的文本中。

I have a struct in a separate file that defines my object so: 我在定义我的对象的单独文件中有一个结构,因此:

struct PassengerFromOtherBus {

var fname: String?
var lname: String?
var todays_price: Int?


init(json: NSDictionary) {
    self.fname = json["fname"] as? String
    self.lname = json["lname"] as? String
    self.todays_price = json["todays_price"] as? Int
}

} }

So later on in the view controller, inside of the viewDidLoad(), after connecting to the URL and then parsing it using NSJSONSerialization and a bunch of other code here (that I don't need to confuse you with) I finally have my value todays_price. 因此,稍后在视图控制器中,在viewDidLoad()内部,连接到URL之后,然后使用NSJSONSerialization和此处的其他代码进行解析(无需与您混淆),我终于有了我的价值今日价格。 So my question is, how do I get my action to use that value when it's only known inside of my viewDidLoad()? 所以我的问题是,当仅在viewDidLoad()内部知道该值时,如何获得使用该值的动作? Xcode will not even let me connect the IBAction to anywhere inside the viewDidLoad function! Xcode甚至不让我将IBAction连接到viewDidLoad函数内部的任何地方!

This is not done with an Action but with an Outlet. 这不是通过动作来完成的,而是通过插座来完成的。 Connect the Stepper from IB as an Outlet to your ViewController. 将步进电机从IB作为插座连接到ViewController。 Then just set the values of the Stepper in ViewDidLoad. 然后,只需在ViewDidLoad中设置步进器的值即可。

在此处输入图片说明

I would never go directly from a UIStepper.value to UILabel.text . 我永远不会直接从UIStepper.value转到UILabel.text Use an intermediary variable to store the value. 使用中间变量来存储值。

Do the same for the return from the JSON . JSON的返回执行相同的操作。 By setting a didSet function on those variables you can update the UI when any of the values is updated. 通过在这些变量上设置didSet函数,可以在更新任何值时更新UI。

class FirstViewController: UIViewController {

    var todays_price: Int = 0 {
        didSet { // didSet to trigger UI update
            myLabel.text = "\(stepperValue * todays_price)"
        }
    }

    var stepperValue : Int = 1 {
        didSet { // didSet to trigger UI update
            myLabel.text = "\(stepperValue * todays_price)"
        }
    }

    @IBOutlet weak var myStepper: UIStepper!
    @IBOutlet weak var myLabel: UILabel!

    override func viewDidLoad() {
        //
        let returnValueFromJson = 10
        todays_price = returnValueFromJson

    }

    @IBAction func stepperUpdate(sender: AnyObject) {

        stepperValue = Int(myStepper.value)

    }

}

Just add a variable to the top of your view controller to hold the value from your json request. 只需在视图控制器顶部添加一个变量即可保存json请求中的值。 Then in viewDidLoad you update that variable, and then you can use it to set your label and inside the IBAction (that doesn't have to be inside viewDidLoad). 然后在viewDidLoad中更新该变量,然后可以使用它在IBAction内(不必在viewDidLoad内)设置标签。

So you would do something like this: 因此,您将执行以下操作:

class WhateverViewController: UIViewController {

    var todays_price: Int!

    override func viewDidLoad() {
        super.viewDidLoad()
        todays_price = // The value you got from json goes here
        new_total.text = "\(todays_price)"
    }

    @IBAction func step_up_pass(sender: AnyObject) {
        new_total.text = "\(Int(stepper.value) * todays_price)"
    }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM