gpt4 book ai didi

ios - 重写一些基本的 swift 代码,使用大的 if 语句

转载 作者:搜寻专家 更新时间:2023-11-01 06:07:14 28 4
gpt4 key购买 nike

我正在开发一个项目,使用 UITabBarcontroller,在 Xcode 中使用 swift 代码。

在这个项目中,用户可以(除其他外)选择他们喜欢的图像。这些最喜欢的图像将设置在我最喜欢的 UIViewController 上的 30 个按钮上。为了完成我想要的,我开发了一些非常基本的代码:我为 30 个按钮分配了 30 个 IBOutlets,并制作了 30 个(大)if 语句。它实际上有效,但我知道这段代码可以用更简单、更简洁的方式完成。我还不能那样做。

我真的很想学习,所以有人可以帮我重写这段代码吗?非常感谢您的帮助 :)。只要朝正确的方向插入就已经很好了

例如,我是否应该为 30 个按钮分配标签值,并使用 .viewWithTag(而不是 30 个 IBOutlets)“找到”合适的按钮。我是否应该使用某种循环来处理数组的不同计数? (见下文)

这是我的代码:

// I have created a subclass of UITabBarController 
class TabBarData: UITabBarController {

/* In this class I have initialized an empty array to share between the various tabs.
The array will be populated with the favorites chosen by the user. */

var array = [Int]()

}

/* There are multiple ViewControllers in my project that have the same
code for adding a favorite. So for now I describe one example ViewController */

class exampleVC: UIViewController {

/* A variable that identifies the image by the number. There a a few
hundred images in the project, every images has its own identifying number */
var imageNumber = 0

// This function will execute if user adds a favorite:
func userAddedFavorite(imageNumber: Int) {

// The ViewController within the TabBarController getting acces to the properties
if let tbc = self.tabBarController as? TabBarData {

// The Array can not be bigger then a count of 30:
if tbc.array.count < 30 {

// When a user adds a new favorite image, the array gets filled with a new value:

tbc.array.append(imageNumber)


// Now I set the button images, in viewWillAppear, for my favorites VC:

class Favorites: UIViewController {

// IBOutlets for the 30 buttons
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!

// etcetera…

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if let tbc = self.tabBarController as? TabBarData {

if tbc.array.isEmpty {

print("The user has no Favorites at the moment. The array is empty")

} else if tbc.array.count == 1 {

// At the moment the images in my project have been named: test1/test2/test3 etc...
button1.setImage(UIImage(named: "test\(tbc.array[0])"), forState: .Normal)

} else if tbc.array.count == 2 {

button1.setImage(UIImage(named: "test\(tbc.array[0])"), forState: .Normal)
button2.setImage(UIImage(named: "test\(tbc.array[1])"), forState: .Normal)

} else if tbc.array.count == 3 {

button1.setImage(UIImage(named: "test\(tbc.array[0])"), forState: .Normal)
button2.setImage(UIImage(named: "test\(tbc.array[1])"), forState: .Normal)
button3.setImage(UIImage(named: "test\(tbc.array[2])"), forState: .Normal)

} else {

print("etcetera.....,the if-statements getting bigger each count up......")
}

最佳答案

与其制作 30 个 IBOutlet(每个按钮一个),不如制作一个包含所有 30 个的 IBOutletCollection:

@IBOutlet var buttons: [UIButton]!

然后你可以:

for (index, item) in tbc.array.enumerate() {
buttons[index].setImage(UIImage(named: "test\(item)"), forState: .Normal)
}

关于ios - 重写一些基本的 swift 代码,使用大的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34398711/

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