gpt4 book ai didi

ios - 将数据从一个类传递到另一个类

转载 作者:行者123 更新时间:2023-11-28 08:08:05 24 4
gpt4 key购买 nike

所以我正在尝试为我所在的类(class)编写一个“Mad Lib”主题的应用程序,但我不断收到

EXC_BAD_INSTRUCTION

点击提交按钮时出错。类里其他同学也想不通。请帮助我!

这是我的 firstViewController.swift:

import UIKit

class ViewController: UIViewController
{
@IBOutlet weak var firstTextField: UITextField! //adjective
@IBOutlet weak var secondTextField: UITextField! //male name
@IBOutlet weak var thirdTextField: UITextField! //verb
@IBOutlet weak var fourthTextField: UITextField! //female name
@IBOutlet weak var fifthTextField: UITextField! //adjective
@IBOutlet weak var sixthTextField: UITextField! //athlete
@IBOutlet weak var seventhTextField: UITextField! //food
@IBOutlet weak var eighthTextField: UITextField! //restaurant name
var userInfo = myCustomClass()

override func viewDidLoad()
{
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
userInfo.adjectiveOne = firstTextField.text!
userInfo.maleName = secondTextField.text!
userInfo.verb = thirdTextField.text!
userInfo.femaleName = fourthTextField.text!
userInfo.adjectiveTwo = fifthTextField.text!
userInfo.athlete = sixthTextField.text!
userInfo.food = seventhTextField.text!
userInfo.restaurantName = eighthTextField.text!
let nextView = segue.destination as! secondViewController
nextView.passedObject = userInfo
}
}

这是我的 secondViewController.swift:

import UIKit

class secondViewController: UIViewController
{
var passedObject = myCustomClass()
@IBOutlet weak var myFinishedProduct: UILabel!

override func viewDidLoad()
{
super.viewDidLoad()
myFinishedProduct.text = "There was once a \(passedObject.adjectiveOne) man /n \(passedObject.maleName). One day while he was \(passedObject.verb) he saw /n \(passedObject.femaleName), a rather \(passedObject.adjectiveTwo) woman. /n She was also a \(passedObject.athlete), and a very good /n one too. The two went to lunch together at \(passedObject.restaurantName) /n and ate some \(passedObject.food). After /n that they never crossed paths again."
}
}

最后这是我的 NSOBject,名为“myCustomClass.swift”:

import UIKit

class myCustomClass: NSObject
{
var adjectiveOne = ""
var maleName = ""
var verb = ""
var femaleName = ""
var adjectiveTwo = ""
var athlete = ""
var food = ""
var restaurantName = ""
}

基本上,`无论用户在八个文本字段中输入什么,都会在按下提交按钮时存储在 myCustomClass 中。从那里开始,在 secondViewController 中,它将八个输入放入故事中并将其显示在标签上。

感谢任何帮助,谢谢!

编辑:“提交按钮”连接到我的故事书上的 secondViewController,目的是“显示”。

最佳答案

第一个 View Controller

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var txtmobile: UITextField!
@IBOutlet weak var txtlname: UITextField!
@IBOutlet weak var txtfname: UITextField!
var ArrayStudent:[PassData] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func btnclick(_ sender: UIButton)
{
let objdata = PassData(fname: txtfname.text!, lname: txtlname.text!, mobile: txtmobile.text!)
ArrayStudent.append(objdata)
passdata()

}
func passdata()
{
let objstory = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
objstory.Arradata1 = ArrayStudent
_ = self.navigationController?.pushViewController(objstory, animated: true)
}

}

第二 View Controller

  import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var lblmobile: UILabel!
@IBOutlet weak var lbllastname: UILabel!
@IBOutlet weak var lblname: UILabel!
var Arradata1:[PassData ] = []
override func viewDidLoad() {
super.viewDidLoad()
lblname.text = Arradata1.first?.StrFirstName
lbllastname.text = Arradata1.first?.StrLastName
lblmobile.text = Arradata1.first?.StrMobile

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

NSObject 类

import UIKit

class PassData: NSObject
{
var StrFirstName:String!
var StrLastName:String!
var StrMobile:String!
init(fname:String , lname:String , mobile:String)

{
StrMobile = mobile
StrFirstName = fname
StrLastName = lname
}
}

关于ios - 将数据从一个类传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44523477/

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