gpt4 book ai didi

ios - 如何让按钮移动到另一个ViewController

转载 作者:行者123 更新时间:2023-11-30 12:08:32 25 4
gpt4 key购买 nike

如何使 Button 移动到另一个 ViewController

任何人都可以帮助我吗,这是我的代码。我尝试使用 button1: UIButton! 但出现错误

class Interest1
{
var title = ""
var description = ""
var featuredImage: UIImage!
var button1: UIButton!
var button2: UIButton!
var button3: UIButton!
var button4: UIButton!

init(title: String, featuredImage: UIImage!, button1: UIButton!)
{
self.title = title
self.featuredImage = featuredImage
}

static func createInterest() -> [Interest1]
{
return [
Interest1(title: "One", featuredImage: UIImage(named:"001.png")!, button1: UIButton), // I got error Here i don't know why
Interest1(title: "Two", featuredImage: UIImage(named:"002.png")!),
Interest1(title: "Three", featuredImage: UIImage(named:"003.png")!),
Interest1(title: "Four", featuredImage: UIImage(named:"004.png")!),
]
}
}

代码:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return interests3.count //error
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as! interestCollectionViewCell
cell.interest2 = self.interests3[indexPath.item] //error
return cell
}

错误是使用了无法解析的标识符“interests3”

最佳答案

我发现您的代码有几个问题。修复它们会很好。

您不必将参数命名为“button1” - “button”在这里更合适,因此您的构造函数可以像这样

init(title: String, featuredImage: UIImage!, button: UIButton!)

在这里强制展开并不是一个好的方法。最好将其删除,因此构造函数为:

init(title: String, featuredImage: UIImage, button: UIButton)

现在,在构造函数内部您根本不使用按钮。我想你想设置它,比如

init(title: String, featuredImage: UIImage, button: UIButton)
{
self.title = title
self.featuredImage = featuredImage
self.button = button
}

最后,在您创建的对象中,您使用的不是现有的构造函数:

Interest1(title: "Two", featuredImage: UIImage(named:"002.png")!)

您还必须提供按钮。所以,您可能想做这样的事情:

   static func createInterest() -> [Interest1]
{
return [
Interest1(title: "One", featuredImage: UIImage(named:"001.png")!, button: someFirstButton), // I got error Here i don't know why
Interest1(title: "Two", featuredImage: UIImage(named:"002.png")!, button: someSecondButton),
Interest1(title: "Three", featuredImage: UIImage(named:"003.png")!, button: someThirdButton)
]
}

请注意,您可能想在内部提供按钮,而不是 someFirstButton、someSecondButton、someThirdButton

祝你好运

关于ios - 如何让按钮移动到另一个ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46377059/

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