繁体   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