如何在swift中声明全局变量

时间:2015-02-20 12:35:28

标签: ios swift uibutton global-variables

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

    }

我是快速编程的新手,请提前告诉我你是否知道。

2 个答案:

答案 0 :(得分:2)

Okey,你的一些部分包含正确的代码,有些则没有。 像这里@IBOutlet var btn: UIButton!这是类对象属性,所以你可以在这个类中的所有方法中访问它。

let myBtn怎么样,你只需要用viewDidLoad方法创建它。所以它在里面可见。您需要将此属性存储在方法外部,但在类中。

另一个问题是:您要使用let还是var?由于let是常量,因此您需要在任何init方法内或在声明时初始化它(您不能在其他方法中执行此操作,如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的规则。

答案 1 :(得分:-1)

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

}