Show other array in next view controller depending on button pressed in previous

huangapple go评论92阅读模式
英文:

Show other array in next view controller depending on button pressed in previous

问题

在视图控制器中,我花了很多时间来使这个工作正常,但我正在尝试根据在前一个视图中按下的按钮来显示一个数组。所以我想要使用相同的视图控制器,但使用不同的数据。

在视图控制器中:

@IBAction func firstDeck(_ sender: UIButton) {
    var dataSource: CardsDataModel? {
        didSet {
            label.text = cardsDeck.FirstDate()
        }
    }
}

@IBAction func secondDeck(_ sender: UIButton) {
    var dataSource: CardsDataModel? {
        didSet {
            label.text = cardsDeck.PizzaFriends()
        }
    }
}

所以有两个卡牌堆:PizzaFriends 和 FirstDates。我尝试使用按钮的IBAction 来显示相关的 cardsDeck,但没有成功。卡片是空的。

英文:

I spent hours to make this work but I am trying to show an array in a view controller based on the button that is pressed in the previous view. So I want to use the same view controller but with different data.

In the view controller:

@IBAction func firstDeck(_ sender: UIButton) {
var dataSource : CardsDataModel? {
        
    didSet {
        label.text = cardsDeck.FirstDate()
        } 
    }
}

@IBAction func secondDeck(_ sender: UIButton) {
var dataSource : CardsDataModel? {

            didSet {
           label.text = cardsDeck.PizzaFriends()
         }
    }
}

So there are two cardDecks: PizzaFriends and FirstDates. I tried using IBAction from the buttons to then show the relevant cardsDeck but without success. The cards are empty.

答案1

得分: 0

NextViewController 中声明一个 String 类型数组变量:

var someData = [String]()

NextViewControllerviewDidLoad() 方法中添加以下内容:

print("Some Data: \(someData)")

PreviousViewController 的 IBAction 方法中,根据按下的按钮不同,呈现或推送 NextViewController,并分配 someText 变量:

@IBAction func firstDeck(_ sender: UIButton) {
    let vc = NextViewController()
    vc.someData = cardsDeck.FirstDate()

    // 如果你想呈现 NextViewController
    present(vc, animated: true, completion: nil)

    // 如果你想将 NextViewController 推送到导航堆栈
    //navigationController?.pushViewController(vc, animated: true)
}

@IBAction func secondDeck(_ sender: UIButton) {
    let vc = NextViewController()
    vc.someData = cardsDeck.PizzaFriends()

    // 如果你想呈现 NextViewController
    present(vc, animated: true, completion: nil)

    // 如果你想将 NextViewController 推送到导航堆栈
    //navigationController?.pushViewController(vc, animated: true)
}

这是一个示例的 NextViewController

class NextViewController: UIViewController {
    
    var someData = [String]()
    
    var cardView = SwipeCardView(frame: CGRect(x: 10, y: 150, width: 300, height: 600))

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white
        
        self.view.addSubview(cardView)
        
        cardView.label.text = "\(someData)"
    }
}
英文:

Declare an Array of String type variable in NextViewController

var someData = [String]()

In viewDidLoad() method of NextViewController add this

print("Some Data: \(someData)")

In the PreviousViewController in the IBAction methods present/push the NextViewController with assign someText variable according to which button has been pressed

@IBAction func firstDeck(_ sender: UIButton) {
    let vc = NextViewController()
    vc.someData = cardsDeck.FirstDate()

    // If you want to present NextViewController
    present(vc, animated: true, completion: nil)

    // If you want to push NextViewController to navigation stack
    //navigationController?.pushViewController(vc, animated: true)
}

@IBAction func secondDeck(_ sender: UIButton) {
    let vc = NextViewController()
    vc.someData = cardsDeck.PizzaFriends()

    // If you want to present NextViewController
    present(vc, animated: true, completion: nil)

    // If you want to push NextViewController to navigation stack
    //navigationController?.pushViewController(vc, animated: true)
}

Here is a sample NextViewController

class NextViewController: UIViewController {
    
    var someData = [String]()
    
    var cardView = SwipeCardView(frame: CGRect(x: 10, y: 150, width: 300, height: 600))

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white
        
        self.view.addSubview(cardView)
        
        cardView.label.text = "\(someData)"
    }
}

huangapple
  • 本文由 发表于 2020年1月6日 18:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定