簡體   English   中英

如何快速聲明全局變量

[英]how to declare global variables in swift

我在Swift中以編程方式創建了一個UIButton,我想在整個類中訪問myBtn

這是我的代碼

import UIKit
import Foundation

class ViewController: UIViewController
{

    @IBOutlet var btn: UIButton!


    @IBAction func btnPressed()
    {
        self.custum()
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()

        let myBtn = UIButton.buttonWithType(UIButtonType.System) as UIButton
        myBtn.frame = CGRectMake(self.view.frame.size.width/2.5, self.view.frame.size.height/4, 100, 30)
        myBtn.setTitle("Button 1", forState: UIControlState.Normal)
        myBtn.addTarget(self, action: "btnPressed", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(myBtn)
        self.custum()
     }

    func custum()
    {

        UIView.animateWithDuration(1.0, animations:{
            let grow = CGAffineTransformMakeScale(1,1)
            let rotate = CGAffineTransformMakeRotation(10)
            myBtn.transform = CGAffineTransformConcat(grow, rotate) // i'm getting this error use of unresolved identifier 'myBtn'
        }) 

    }

我是快速編程的新手,如果您知道,請先告訴我,謝謝。

Okey,您的某些部分包含正確的代碼,而有些則沒有。 就像這里@IBOutlet var btn: UIButton! 這是類對象的屬性,因此您可以在此類內的所有方法中訪問它。

let myBtn怎么樣,您只需在viewDidLoad方法中創建它即可。 因此它在其中是可見的。 您需要將此屬性存儲在方法外部,但在類內部。

另一個問題是:您要使用let還是var 因為let是常量,所以您需要在任何init方法或while聲明中對其進行初始化(您無法在其他方法(如viewDidLoad )中進行此操作)。 因此,對您而言,最好使用var

以您的示例為例:

class ViewController: UIViewController
    {

        @IBOutlet var btn: UIButton!
        var myBtn: UIButton!


        @IBAction func btnPressed()
        {
            self.custum()
        }

        override func viewDidLoad()
        {
            super.viewDidLoad()

            myBtn = UIButton.buttonWithType(UIButtonType.System) as UIButton
            myBtn.frame = CGRectMake(self.view.frame.size.width/2.5, self.view.frame.size.height/4, 100, 30)
            myBtn.setTitle("Button 1", forState: UIControlState.Normal)
            myBtn.addTarget(self, action: "btnPressed", forControlEvents: UIControlEvents.TouchUpInside)
            self.view.addSubview(myBtn)
            self.custum()
        }

        func custum()
        {

            UIView.animateWithDuration(1.0, animations:{
                let grow = CGAffineTransformMakeScale(1,1)
                let rotate = CGAffineTransformMakeRotation(10)
                self.myBtn.transform = CGAffineTransformConcat(grow, rotate) // i'm getting this error use of unresolved identifier 'myBtn'
            }) 

        }
    }

我在最后一個方法中使用了self.myBtn ,因為這是快速關閉的規則。

import UIKit
import Foundation

class ViewController: UIViewController
{
var myBtn:UIButton
@IBOutlet var btn: UIButton!


@IBAction func btnPressed()
{
    self.custum()
}

override func viewDidLoad()
{
    super.viewDidLoad()

    myBtn = UIButton.buttonWithType(UIButtonType.System) as UIButton
    myBtn.frame = CGRectMake(self.view.frame.size.width/2.5, self.view.frame.size.height/4, 100, 30)
    myBtn.setTitle("Button 1", forState: UIControlState.Normal)
    myBtn.addTarget(self, action: "btnPressed", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(myBtn)
    self.custum()
 }

func custum()
{

    UIView.animateWithDuration(1.0, animations:{
        let grow = CGAffineTransformMakeScale(1,1)
        let rotate = CGAffineTransformMakeRotation(10)
        myBtn.transform = CGAffineTransformConcat(grow, rotate) // i'm getting this error use of unresolved identifier 'myBtn'
    }) 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM