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

标签 swift uibutton nsobject

我对 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/

相关文章:

swift - 无法将类型 'GenericClass<Class>' 的值转换为预期的参数类型 'GenericClass<Class>?'

objective-c - Foundation NSObject.h 不带 - (id)init 方法

ios - 如何禁用 UIButton 色调颜色?

ios - 我如何确定 NSData 的内容何时指示 Web 服务请求失败?

ios - 在 NSObject 中设置 float 会抛出 doesNotRecognizeSelector 错误

ios - 无法打开 Optional.Swift 中的 None 错误

ios - 通用类不将委托(delegate)调用转发给具体子类

swift - 设置 DarkTheme 没有效果 Braintree iOS v4 SDK

ios - uitableviewcell 内的 UIButton 触发单元格而不是按钮

ios - 等同于 UIButtonTypeRoundedRect swift?