ios - 如何在swift中声明全局变量

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

    }

我是 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,因为它是 closures 的规则 swift 。

关于ios - 如何在swift中声明全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28629175/

相关文章:

ios - 为什么当我 pop/self.dismiss 我的 WKInterfacecontroller 时不调用 deinit

iphone - 为什么 UIButton 的 ExclusiveTouch 属性默认不设置为 YES?

ios - AudioKit Playground (错误 : 'Module compiled with Swift 3.1 cannot be imported in Swift 3.0.2' )

ios - 切换标签时向 Controller 发送数据

iOS:具有一对多关系的核心数据更新对象

SwiftUI:如何让 TextField 成为第一响应者?

ios - 更改UIButton内的SF符号大小

ios - Xcode - 按钮在 Storyboard 中看起来不错,但在模拟器上却不行

ios - 如何理解“当使用带有__weak限定词的变量时,该对象总是在autoreleasepool中注册”

ios - UImage 加载整个 SKTextureAtlas 而不仅仅是 SKTexture