所以我试图编写一个" 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,其目的是" show"。
答案 0 :(得分:0)
第一视图控制器
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)
}
}
第二视图控制器
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
}
}