使用参数

时间:2018-02-06 10:28:10

标签: ios swift xcode swift4 xcode9

描述了一个函数,其参数用于扩展另一个类。

我想更改该功能中存在的UIView属性(alpha)的值"在另一个类的按钮中更改"。

但是,可以在ViewDidLoad时更改此功能。

点击按钮时如何更改值? 如果可能请写下代码。

ViewController类

class ViewController: UIViewController {

    @IBOutlet weak var vHome: UICollectionView!
    @IBOutlet weak var vSearch: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        selectRd(rd: 3) //This time can change calue of alpha.
    }
}

extension ViewController {

    func selectRd(rd reader:Int){

        var animateView:UIView!

        let animateVHome   = vHome
        let animateVSearch = vSearch

        UIView.animate(withDuration: 0.3, delay: 0.0, animations: {
            animateVHome.alpha   = 0.0
            animateVSearch.alpha = 0.0
        }, completion: nil)

        switch reader {
        case 2:
            animateView = animateVHome
        case 3:
            animateView = animateVSearch
        default:
            break
        }

        UIView.animate(withDuration: 0.3, delay: 0.3, animations: {
            animateView.alpha = 1.0
        }, completion: nil)
    }

}



另一类

class Another{
    @objc func buttonTapped(sender : HeaderButton) {
        let vC = ViewController
        vC.selectRd(rd: 2) //This time can not!!
    }
}





如果您无法理解这个问题,请发表评论。

1 个答案:

答案 0 :(得分:0)

问题是如果一个类当前处于活动状态,则无法访问该类函数,因为这会导致崩溃,因此在viewControler中添加一个var

1) Copy the current project (Right click->Copy from Project Explorer)
2) Paste it (Right click->Paste from Project Explorer) and give the copy another name
3) The indexer should now start and index both projects
4) When indexation is complete, you can delete the copy

...

class ViewController: UIViewController {

@IBOutlet weak var vHome: UICollectionView!
@IBOutlet weak var vSearch: UICollectionView!
var index:Int?
override func viewDidLoad() {
    super.viewDidLoad()
    selectRd(rd: self.index) //This time can change calue of alpha.
 }
}