在loadView()中声明变量时出现“使用未解决的标识符”错误

时间:2019-03-18 03:13:41

标签: ios swift

我正在尝试创建一个简单的应用程序,当用户点击该应用程序时,标签文本将更改,如下面的代码所示。但是,在我处理抽头的函数中,它表示未解析标签。我相信这是因为标签已加载到loadView中,并且无法在整个类中访问(我是Swift的初学者,所以请谅解。)这是我的代码:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let containerView = UIView (frame: CGRect(x: 0, y: 0, width: 600, height: 600))
        containerView.backgroundColor = UIColor.white

        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        label.center = CGPoint(x: 160, y: 285)
        label.textAlignment = .center
        label.text = "Hey"
        containerView.addSubview(label)
        self.view = containerView
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
        view.addGestureRecognizer(tap)

    }
    @objc func handleTap(sender:UITapGestureRecognizer) {
        label.text = "Changed"
    }
}
PlaygroundPage.current.liveView = MyViewController()

谢谢!

2 个答案:

答案 0 :(得分:0)

您正在尝试访问/* sprites desktop */ .sprite{ width: 200px; height: 114px; background-image: url('https://www.subscriptionboxaustralia.com/wp-content/uploads/2018/05/css_sprites-1.jpg') ; display: inline-block; margin: 0 10px 0 0; } #beauty{ background-position: -260px -20px; margin: 20px 20px 0 10px;} #beauty:hover{ border-width: 1px; border-style: solid; border-color: blue; } #food{ background-position: -500px -328px; margin: 20px 20px 0 10px; } #food:hover{ border-width: 1px; border-style: solid; border-color: blue; } #kids{ background-position: -20px -174px; margin: 20px 20px 0 10px; } #kids:hover{ border-width: 1px; border-style: solid; border-color: blue; } #organic{ background-position: -260px -174px; margin: 20px 20px 0 10px; } #organic:hover{ border-width: 1px; border-style: solid; border-color: blue; } #pet{ background-position: -20px -328px; margin: 20px 20px 0 10px; } #pet:hover{ border-width: 1px; border-style: solid; border-color: blue; } #fitness{ background-position: -260px -328px; margin: 20px 20px 0 10px; } #fitness:hover{ border-width: 1px; border-style: solid; border-color: blue; } #fashion{ background-position: -500px -20px; margin: 20px 20px 0 10px; } #fashion:hover{ border-width: 1px; border-style: solid; border-color: blue; } #home{ background-position: -500px -174px; margin: 20px 20px 0 10px; } #home:hover{ border-width: 1px; border-style: solid; border-color: blue; } #book{ background-position: -20px -20px; margin: 20px 20px 0 10px; } #book:hover{ border-width: 1px; border-style: solid; border-color: blue; } #international{ background-position: -20px -482px; margin: 20px 20px 0 10px; } #international:hover{ border-width: 1px; border-style: solid; border-color: blue; } #craft{ background-position: -260px -482px; margin: 20px 20px 0 10px; } #craft:hover{ border-width: 1px; border-style: solid; border-color: blue; } #mens{ background-position: -500px -482px; margin: 20px 20px 0 10px; } #mens:hover{ border-width: 1px; border-style: solid; border-color: blue; } #period{ background-position: -740px -20px; margin: 20px 20px 0 10px; } #period:hover{ border-width: 1px; border-style: solid; border-color: blue; } #adult{ background-position: -740px -174px; margin: 20px 20px 0 10px; } #adult:hover{ border-width: 1px; border-style: solid; border-color: blue; } #gift{ background-position: -740px -328px; margin: 20px 20px 0 10px; } #gift:hover{ border-width: 1px; border-style: solid; border-color: blue; } #collectables{ background-position: -740px -482px; margin: 20px 20px 0 10px; } #collectables:hover{ border-width: 1px; border-style: solid; border-color: blue; } #misc{ background-position: -20px -636px; margin: 20px 20px 0 10px; } #misc:hover{ border-width: 1px; border-style: solid; border-color: blue; } #adult{ background-position: -740px -174px; margin: 20px 20px 0 10px; } #adult:hover{ border-width: 1px; border-style: solid; border-color: blue; } #popular{ background-position: -740px -636px; margin: 20px 20px 0 10px; } #popular:hover{ border-width: 1px; border-style: solid; border-color: blue; } #all{ background-position: -503px -636px; margin: 20px 20px 0 10px; } #all:hover{ border-width: 1px; border-style: solid; border-color: blue; } #drinks{ background-position: -258px -637px; margin: 20px 20px 0 10px; } #drinks:hover{ border-width: 1px; border-style: solid; border-color: blue; } 方法中声明的labelloadView()无权访问handleTap()

label声明为类变量。

有关声明和范围的更多信息,请参见此处:https://docs.swift.org/swift-book/ReferenceManual/Declarations.html

答案 1 :(得分:0)

您已将label声明为loadView中的局部变量-这意味着只能在loadView函数中访问它。您需要将其作为属性,以便在整个课程中都可以访问。

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {

    var label: UILabel!

    override func loadView() {
        let containerView = UIView (frame: CGRect(x: 0, y: 0, width: 600, height: 600))
        containerView.backgroundColor = UIColor.white

        self.label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        self.label.center = CGPoint(x: 160, y: 285)
        self.label.textAlignment = .center
        self.label.text = "Hey"
        containerView.addSubview(self.label)
        self.view = containerView
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
        view.addGestureRecognizer(tap)

    }
    @objc func handleTap(sender:UITapGestureRecognizer) {
        self.label.text = "Changed"
    }
}
PlaygroundPage.current.liveView = MyViewController()
相关问题