我对编码和Xcode都是陌生的,并且正在开发我的第一个iOS应用-如果经验丰富的开发人员对我的问题的答案是显而易见的,请原谅我。
我的应用程序可以运行,但是由于我在每个View Controller上具有的按钮,设计看起来非常“过时”。我希望使用更现代的导航系统,该导航系统的导航栏在每个屏幕顶部都带有导航按钮。但是,当我使用这种导航方法时,无法在最终的View Controller上访问在第一个View Controller上输入的全局变量。我已经为每个View Controller仔细检查了我的Swift代码文件,如前所述,如果我有UIButtons而不是Navbar,则可以正常工作。
我唯一看到的区别是Xcode 11 Navbar系统在View Controller之间使用了“显示”(推送)方法,而我的情节提要按钮使用模式表示。
如果有人可以引导我朝正确的方向前进,我将不胜感激-谢谢!
显示来自第一个视图控制器的全局变量的代码为:
\\
import UIKit
var name = ""
var lastname = ""
var address = ""
var city = ""
var zip = ""
var email = ""
var phone = ""
var dateofbirth = ""
class ViewController1: UIViewController {
@IBOutlet weak var outlet: UITextField!
@IBOutlet weak var outlet2: UITextField!
@IBOutlet weak var outlet6: UITextField!
@IBOutlet weak var outlet3: UITextField!
@IBOutlet weak var outlet4: UITextField!
@IBOutlet weak var outlet5: UITextField!
@IBOutlet weak var outlet8: UITextField!
@IBOutlet weak var outlet7: UITextField!
@IBAction func submit(_ sender: Any) {
// Code for First Name
if (outlet.text != "")
{
name = outlet.text!
}
// Code for Last Name
if (outlet2.text != "")
{
lastname = outlet2.text!
}
// Code for Address
if (outlet3.text != "")
{
address = outlet3.text!
}
// Code for city
if (outlet4.text != "")
{
city = outlet4.text!
}
// Code for Zip
if (outlet5.text != "")
{
zip = outlet5.text!
}
// Code for Email
if (outlet6.text != "")
{
email = outlet6.text!
}
// Code for Phone
if (outlet7.text != "")
{
phone = outlet7.text!
}
// Code for Date of Birth
if (outlet8.text != "")
{
dateofbirth = outlet8.text!
}
// Dismissal of Keyboard
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
super.touchesBegan(touches, with: event)
}
override func viewDidLoad() {
super.viewDidLoad()
} }
\\\\
最终View Controller的代码为:
\\\\
import UIKit
class ReportViewController: UIViewController {
//Client ID Label Outlets
@IBOutlet weak var Label: UILabel!
@IBOutlet weak var Label2: UILabel!
@IBOutlet weak var Label3: UILabel!
@IBOutlet weak var Label4: UILabel!
@IBOutlet weak var Label5: UILabel!
@IBOutlet weak var Label6: UILabel!
@IBOutlet weak var Label7: UILabel!
@IBOutlet weak var Label8: UILabel!
// Return function
@IBAction func unwindToReportVC (_sender:UIStoryboardSegue){
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated:Bool){
// Client ID Labels text using declared global variables
Label.text = name
Label2.text = lastname
Label3.text = address
Label4.text = city
Label5.text = zip
Label6.text = email
Label7.text = phone
Label8.text = dateofbirth
} }