【发布时间】:2022-01-05 10:44:39
【问题描述】:
我需要将这些存储在一个集合中。然后我将有两个按钮“上一个”和“下一个”。如果我们到达集合的结尾,它应该从头开始或跳到结尾。
class ViewController: UIViewController {
var photoCollection: [[String:Any]] = [
["image": UIImage(named: "Sea house")!, "text": "sea house"]
// Other photos
]
@IBOutlet weak var photo: UIImageView!
@IBOutlet weak var Text: UILabel!
func showImage() {
photo.image = photoCollection[count]["image"] as! UIImage
Text.text = photoCollection[count]["text"] as! String
}
@IBAction func Previous(_ sender: UIButton)
{
guard count > 0 else {return}
count -= 1
showImage()
}
@IBAction func Next(_ sender: UIButton) {
guard count < photoCollection.count - 1 else {return}
count += 1
showImage()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
请帮助调试代码。
谢谢!
【问题讨论】:
-
什么是你所说的“收藏”?
UICollectionView?我没有看到任何一行表明您正在使用带有UICollectionView对象的 photoCollection 家伙。 -
它是 UIImage“图像视图”,我有 10 张图像“每张都有文字描述”,需要将它们存储在一个集合中。
标签: swift function button collections uiimage