gpt4 book ai didi

iphone - Swift:我需要获取 UIVIew 集合之一的索引才能知道哪个集合被点击

转载 作者:行者123 更新时间:2023-11-30 10:09:19 25 4
gpt4 key购买 nike

我需要获取 UIView 集合之一的索引,以了解哪个集合被点击,然后链接到下一个屏幕中相应的数组元素。问题似乎是 sender! 我收到了错误消息:

Could not cast value of type 'UITapGestureRecognizer' (0x107dcdc20) to 'UIView' (0x107dc2578).

(我使用 UIView 创建彩色图 block 。我应该使用标签或按钮而不是 UIView 吗?)

import UIKit

class ColoringViewController: UIViewController {

var coloringItem: ColoringItem?
var colorListTileArray = [UIView]()

@IBOutlet var colorListTile: [UIView]! // UIView color tiles


override func viewDidLoad() {
super.viewDidLoad()

if coloringItem != nil {
for (var i = 0; i<colorListTile.count; i++) {

colorListTileArray += [colorListTile[i]]

}
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showColoringDescriptionSegue" {

let tappedTile = sender!.self //<- doesn't work
//let tappedTile = sender!.view as! UIView <- doesn't work either :(
let colorIndex = colorListTileArray.indexOf(tappedTile as! UIView)

print(colorIndex)
}
}

@IBAction func showColoringDescription(sender: AnyObject) {

performSegueWithIdentifier("showColoringDescriptionSegue", sender: sender)
}

}

最佳答案

你很接近。 showColoringDescription 的发送者是 UITapGestureRecognizer。更改签名以采用其中之一。然后将 sender.view 传递给 performSegueWithIdentifier

prepareForSegue 中,将 sender 转换为 UIView 并将其用于 colorListTileArray 中的查找:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showColoringDescriptionSegue" {

let tappedTile = sender as! UIView
let colorIndex = colorListTileArray.indexOf(tappedTile)

print(colorIndex)
}
}

@IBAction func showColoringDescription(sender: UITapGestureRecognizer) {
performSegueWithIdentifier("showColoringDescriptionSegue", sender: sender.view)
}

关于iphone - Swift:我需要获取 UIVIew 集合之一的索引才能知道哪个集合被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33965379/

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