gpt4 book ai didi

swift - 如何从 NSObject 访问多个按钮?

转载 作者:行者123 更新时间:2023-11-28 07:29:37 25 4
gpt4 key购买 nike

我对 Swift 和一般编程还很陌生,所以请多多包涵:

我创建了一个 NSObject 来访问多个文本和按钮。

import UIKit

class WelcomeScreen: NSObject {

var messageText: String?
var imageName: String?
var buttonName: UIButton?

static func sampleScreens() -> [WelcomeScreen] {

let splitButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Split", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.tag = 0
return button
}()

let playButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Play", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.tag = 1

return button
}()

let splitScreen = WelcomeScreen()
splitScreen.messageText = "It's simple, start splitting your bill"
splitScreen.imageName = "SplitButton"
splitScreen.buttonName = splitButton



let otherScreen = WelcomeScreen()
otherScreen.messageText = "Choose a random party member to pay for the whole meal"
otherScreen.imageName = "PlayButton"
otherScreen.buttonName = playButton

return [splitScreen, otherScreen]
}

这是我访问文本和图像但无法访问按钮的地方。

class ButtonCell: UICollectionViewCell {
var button: WelcomeScreen? {
didSet {
if let text = button?.messageText {
descriptionText.text = text
}
if let imageName = button?.imageName{
splitButtonView.image = UIImage(named: imageName)
}
splitButton = button?.buttonName
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setUpView()

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

let splitButtonView: UIImageView = {
let imageView = UIImageView(image: #imageLiteral(resourceName: "SplitButton"))

//enables autolayout for our imageView
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit

return imageView
}()


var splitButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Split", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor

return button

}()
let playButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Play", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor

return button
}()

private let descriptionText: UILabel = {

let textLabel = UILabel()

textLabel.text = "It's simple, start splitting your app"
textLabel.font = UIFont(name: "IBMPlexSans-Light", size: 20)
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.textAlignment = .center
textLabel.backgroundColor = .clear
textLabel.textColor = UIColor.gray
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 2


return textLabel
}()
func setUpView(){
backgroundColor = UIColor.clear


addSubview(descriptionText)
addSubview(splitButtonView)
addSubview(splitButton)



addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-30-[v0]-30-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": descriptionText]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-30-[v0]-30-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": splitButton]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-50-[v1(==70)][v0(==70)]-40-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": splitButton, "v1": descriptionText]))

}
}

我的程序有多个单元格,不同的文本和图像出现在不同的单元格上。我正在尝试弄清楚如何让我的按钮也显示在不同的单元格上。以下是屏幕截图:

First Page Second Page

希望我说清楚了。任何帮助将非常感激。谢谢!

附言此代码的大部分来自 YouTube 上的各种 swift 教程。

最佳答案

我认为创建自定义 Collection View 单元格比创建自定义对象更好、更容易。在我使用 swift 开发 ios 的所有时间里,我只需要在处理 objective-c 时从 NSObject 实例化。

关于swift - 如何从 NSObject 访问多个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55250355/

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