gpt4 book ai didi

swift - 我的 Segue 或按钮无法快速运行

转载 作者:行者123 更新时间:2023-11-30 13:10:37 24 4
gpt4 key购买 nike

我的理想场景是,当我按下名为“1”/ButtonArray.index[0] 的按钮时,它将执行到另一个的segue(segue标识符为“segueFirst”) >UIViewController 称为“FirstCollectionViewController”。但当我按下按钮时什么也没有发生。

现在,我不知道这是否与segue有关,我对此表示怀疑。我确实认为这与按钮有关,但我无法弄清楚。

PS:在 ButtonArray 函数(应该触发 segue)内,我尝试过 performSegueWithIdentifier 但按下按钮时没有任何反应。

我也尝试过 presentViewController 但它给了我这个错误

Cannot convert value of type "FirstCollectionViewController.Type" to expected argument type "UIViewController"

我不明白,因为我已将其声明为 ViewController(在“新 VC”中)。

VC1:

        import Foundation
import UIKit

class CollectionViewController: UICollectionViewController {

var Array = [String]()
var ButtonArray = [String]()

override func viewDidLoad() {
super.viewDidLoad()

Array = ["1","2","3","4"]
ButtonArray = ["1", "2", "3", "4"]


}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Array.count
}


override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell


let Label = cell.viewWithTag(1) as! UILabel

Label.text = Array[indexPath.row]

let Button = cell.viewWithTag(2) as! UIButton

Button.setTitle(ButtonArray[indexPath.row], forState: UIControlState.Normal)
Button.addTarget(self, action: "ButtonArray", forControlEvents:.TouchUpInside)
return cell

}

func ButtonArray(sender : UIButton) {
print("Not the function")
var selectedButtonCell = sender.superview as! UICollectionViewCell

var indexPath = collectionView?.indexPathForCell(selectedButtonCell)
if indexPath?.row == 0 {
print("Not the if-statement")
// presentViewController(FirstCollectionViewController, animated: true, completion: nil)
// performSegueWithIdentifier("segueFirst", sender: nil)
}
}
}

新的风险投资:

import UIKit
import Foundation
class FirstCollectionViewController : UIViewController {

override func viewDidLoad() {
print("hey")
}
}

最佳答案

您忘记在 cellForItemAtIndexPath 中添加按钮的目标,请在 ButtonsetTitle 方法之后添加此行

Button.addTarget(self,action: Selector("ButtonArray:"), forControlEvents: .TouchUpInside)

我认为问题可能是你的按钮框架太小,所以尝试实现 UICollectionViewDelegatedidSelectItemAtIndexPath 方法,如果是的话,还可以使用 ViewController 设置你的 collectionView 委托(delegate)未设置,并检查它是否正在调用 didSelectItemAtIndexPath

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Inside didSelectItemAtIndexPath")
}

关于swift - 我的 Segue 或按钮无法快速运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38770695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com