如何从NSObject访问多个按钮?

问题描述 投票:1回答:1

我对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 uibutton nsobject
1个回答
0
投票

我认为创建自定义集合视图单元而不是创建自定义对象会更好更容易。在我用swift开发ios的所有时间里,我只需要在处理目标c时从NSObject实例化。

© www.soinside.com 2019 - 2024. All rights reserved.