当前位置: 首页 > 知识库问答 >
问题:

如何访问我在其他文件中定义的方法?

张嘉熙
2023-03-14

这里我尝试访问我的handleTap()函数,它是我在另一个文件中创建的一个OBJ C函数,我需要它留在那个文件中,那么我如何从第二个ViewController访问这个函数呢?

import UIKit

class SecondViewController: UIViewController {
    
   
    var stackView = UIStackView()
    
    var circle: Button = {
        let button = Button()
        button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
        return button
    }()

这就是我在靠近底部的另一个文件中定义handleTap()的方式。它是一个文件名Button.swift,也许可以在SecondViewController上做一个扩展,包括handleTap(),但我不知道如何做。

    import UIKit

class Button: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        createCircle()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    let percentageLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.textAlignment = .center
        label.font = UIFont.boldSystemFont(ofSize: 28)
        label.textColor = UIColor(red: 0.59, green: 0.42, blue: 0.23, alpha: 1.00)
        return label
        
    }()
    let shapeLayer = CAShapeLayer()
    
    func createCircle() {
        
        let trackLayer = CAShapeLayer()
        
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        button.layer.cornerRadius = 0.5 * button.bounds.size.width
        button.clipsToBounds = true
        button.center = center
        
        addSubview(button)
        
        let circularPath = UIBezierPath(arcCenter: .zero, radius: 50, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
        
        trackLayer.path = circularPath.cgPath
        
        trackLayer.strokeColor = UIColor(red: 0.82, green: 0.69, blue: 0.52, alpha: 1.00).cgColor
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.lineWidth = 10
        trackLayer.position = center
        layer.addSublayer(trackLayer)
        
        shapeLayer.path = circularPath.cgPath
        
        shapeLayer.strokeColor = UIColor(red: 0.59, green: 0.42, blue: 0.23, alpha: 1.00).cgColor
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.lineWidth = 10
        shapeLayer.lineCap = CAShapeLayerLineCap.round
        shapeLayer.position = center
        shapeLayer.transform = CATransform3DMakeRotation(-CGFloat.pi / 2, 0, 0, 1)
        
        addSubview(percentageLabel)
        
        percentageLabel.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
        percentageLabel.center = center
        //  return CGPoint(x: positionX, y: positionY)
        
    }
    
    var done = 0
    var toDo = 0
    
    @objc func handleTap(sender: UIButton) {
        toDo = 5
        if done < toDo {
            done += 1
        } else {
            done -= toDo
        }
        let percentage = CGFloat(done) / CGFloat(toDo)
        percentageLabel.text = "\(Int(percentage * 100))%"
        
        DispatchQueue.main.async {
            self.shapeLayer.strokeEnd = percentage
        }
        layer.addSublayer(shapeLayer)
    }
}

共有1个答案

万俟皓
2023-03-14

我认为,为了使用另一个类的函数,目标必须设置为那个类,函数必须是静态类方法

请参考以下链接:how-to-add-an-action-to-a-uibutton-that-is-in-other-class

 类似资料:
  • 我想访问application.properties中提供的值,例如: 我想在Spring Boot应用程序中访问主程序中的userBucket.path

  • 问题内容: 我有我在create()方法中创建的对象编号列表,我想访问它,以便可以在question()方法中使用它。 还有可能我可能错过的另一种方法吗?我搞砸了吗?如果没有,我应该如何获得与以下相同的功能? 问题答案: 有趣的是,列出的两个答案都忽略了发问者正在使用静态方法的事实。因此,除非类类或成员变量也被声明为静态或静态引用,否则该方法将无法访问任何类或成员变量。这个例子: 会起作用的,但是

  • 问题内容: 我想访问中提供的值,例如: 我想在Spring Boot应用程序的主程序中访问。 问题答案: 你可以使用批注并在使用的任何Spring bean中访问属性 Spring Boot文档的Externalized Configuration部分介绍了你可能需要的所有详细信息。

  • 我在JQuery mobile和Eclipse中的PhoneGap中编写了一个android移动应用程序。在这个应用程序中,我调用jquery ajax来加载来自其他域的数据列表。 我的jQuery ajax调用代码是: 变量“WEBSERVICE\u URL”具有其他域php服务URL。在“pageview”事件上执行上述代码时,我遇到以下错误 有任何解决方案可以从JQuery Mobile P

  • 我的windows批处理文件: test.py: 测试导入。py: 这不起作用。我得到一个错误消息,说'问候'没有定义。我想给你看输出,但我也有麻烦。 使用INT test_导入需要更改什么。py,那么变量在主模块中是可访问的?