英文:
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]()
在 NextViewController 的 viewDidLoad()
方法中添加以下内容:
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)"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论