如何在 swift 中声明全局变量

how to declare global variables in swift

我在 Swift 中以编程方式创建了一个 UIButton,我想访问整个 myBtn class

这是我的代码

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'
        }) 

    }

我是 swift 编程新手,如果您知道请告诉我,在此先感谢。

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'
    }) 

}

好的,你的一些部分包含正确的代码,一些没有。 就像这里 @IBOutlet var btn: UIButton! 这是 class 对象 属性,所以你可以在这个 class.

中的所有方法中访问它

let myBtn呢,你用viewDidLoad方法创建就可以了。所以它在里面是可见的。您需要将此 属性 存储在方法外部,但在 class.

内部

另一个问题是:您想使用 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,因为它是 swift 中 closures 的规则。