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

我是swift编程的新手,如果你知道,请告诉我,提前谢谢。

好吧,您的某些部分包含正确的代码,有些则不包含。像这里的@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,因为它是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'
    }) 
}

相关内容

  • 没有找到相关文章

最新更新