Swift 3.0 标签变为空白(零值?)
Swift 3.0 Labels Coming Blank (Nil value?)
我是新手 Swift,我正在尝试制作一个允许用户将事件信息输入文本字段的应用程序,然后当他们按下按钮时,该信息将显示在标签中一个单独的视图。但是,我目前遇到一个问题,当我在模拟器中单击按钮时,在每个字段中输入信息后,我只得到一个完全空白的视图,而不是显示信息的标签。
这是输入信息的控制器代码:
import UIKit
class CreateEventController: UIViewController {
@IBOutlet weak var TitleTxt: UITextField!
@IBOutlet weak var CityTxt: UITextField!
@IBOutlet weak var CountryTxt: UITextField!
@IBOutlet weak var VenueTxt: UITextField!
@IBOutlet weak var TimeTxt: UITextField!
@IBOutlet weak var UrlTxt: UITextField!
@IBAction func SaveEventBtn(_ sender: UIButton) {
if TitleTxt.text!.isEmpty || CityTxt.text!.isEmpty ||
CountryTxt.text!.isEmpty || VenueTxt.text!.isEmpty ||
TimeTxt.text!.isEmpty || UrlTxt.text!.isEmpty{
let alert = UIAlertController(title: "Missing values", message: "Please populate all fields", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else {
print("The event title is \((TitleTxt.text)!)")
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "CreateEvntBtn") {
let destVC = segue.destination as! CreatedEventController
destVC.CrTitle = TitleTxt.text!
destVC.CrCity = CityTxt.text!
destVC.CrCountry = CountryTxt.text!
destVC.CrVenue = VenueTxt.text!
destVC.CrTime = TimeTxt.text!
destVC.CrUrl = UrlTxt.text!
}
}
}
}
}
这里是应该显示信息的视图控制器:
import UIKit
class CreatedEventController: UIViewController {
@IBOutlet weak var CrTitleLbl: UILabel!
@IBOutlet weak var CrCityLbl: UILabel!
@IBOutlet weak var CrCountryLbl: UILabel!
@IBOutlet weak var CrVenueLbl: UILabel!
@IBOutlet weak var CrTimeLbl: UILabel!
@IBOutlet weak var CrUrlLbl: UILabel!
var CrTitle, CrCity, CrCountry, CrVenue, CrTime, CrUrl : String!
override func viewDidLoad() {
super.viewDidLoad()
CrTitleLbl.text = CrTitle
CrCityLbl.text = CrCity
CrCountryLbl.text = CrCountry
CrVenueLbl.text = CrVenue
CrTimeLbl.text = CrTime
CrUrlLbl.text = CrUrl
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我确定我遗漏了一些非常简单的东西,但我们将不胜感激。干杯!
您的 prepareForSegue
函数嵌套在另一个函数中。它需要处于 class 的级别。将其移到 SaveEventBtn
.
之外
此外,您需要实际执行转场。在您当前 prepareForSegue
的位置,添加 performSegue(withIdentifier: "mySegueID", sender: nil)
。请注意,mySegueID
是您在情节提要中创建的转场的名称。您目前拥有的它与按钮操作相同,可能是这种情况,但您可能应该将其命名为其他名称。
此外,为了让你的代码在未来更易读,避免你自己因错误而头疼,Swift的标准是让变量名(TitleTxt
,CountryTxt
)和函数名称 (SaveEventBtn
) 驼峰式大小写 (titleTextField
, countryTextField
, saveTapped
).
首先,您想覆盖 prepare(for:sender:)
,而不是创建嵌套。其次,您要调用 performSegue
,这将启动导航:
import UIKit
class CreateEventController: UIViewController {
@IBOutlet weak var TitleTxt: UITextField!
@IBOutlet weak var CityTxt: UITextField!
@IBOutlet weak var CountryTxt: UITextField!
@IBOutlet weak var VenueTxt: UITextField!
@IBOutlet weak var TimeTxt: UITextField!
@IBOutlet weak var UrlTxt: UITextField!
@IBAction func SaveEventBtn(_ sender: UIButton) {
if TitleTxt.text!.isEmpty || CityTxt.text!.isEmpty ||
CountryTxt.text!.isEmpty || VenueTxt.text!.isEmpty ||
TimeTxt.text!.isEmpty || UrlTxt.text!.isEmpty {
let alert = UIAlertController(title: "Missing values", message: "Please populate all fields", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
} else {
print("The event title is \((TitleTxt.text)!)")
// this will perform the segue (of course the segue with the given identifier must exist)
self.performSegue(withIdentifier: "CreateEvntBtn", sender: self)
}
}
// this will give you the opportunity to configure your destination VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "CreateEvntBtn") {
let destVC = segue.destination as! CreatedEventController
destVC.CrTitle = TitleTxt.text!
destVC.CrCity = CityTxt.text!
destVC.CrCountry = CountryTxt.text!
destVC.CrVenue = VenueTxt.text!
destVC.CrTime = TimeTxt.text!
destVC.CrUrl = UrlTxt.text!
}
}
}
三期:
prepareForSegue
必须在 class 的顶层,而不是在另一个方法中。
prepareForSegue
的签名有误。这是
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
在 IBAction
你必须调用 performSegue(withIdentifier:sender)
else {
print("The event title is \((TitleTxt.text)!)")
performSegue(withIdentifier:"CreateEvntBtn", sender:nil)
}
如果您是 Swift 的新手,请习惯函数和变量名称以小写字母开头的命名约定。
我是新手 Swift,我正在尝试制作一个允许用户将事件信息输入文本字段的应用程序,然后当他们按下按钮时,该信息将显示在标签中一个单独的视图。但是,我目前遇到一个问题,当我在模拟器中单击按钮时,在每个字段中输入信息后,我只得到一个完全空白的视图,而不是显示信息的标签。
这是输入信息的控制器代码:
import UIKit
class CreateEventController: UIViewController {
@IBOutlet weak var TitleTxt: UITextField!
@IBOutlet weak var CityTxt: UITextField!
@IBOutlet weak var CountryTxt: UITextField!
@IBOutlet weak var VenueTxt: UITextField!
@IBOutlet weak var TimeTxt: UITextField!
@IBOutlet weak var UrlTxt: UITextField!
@IBAction func SaveEventBtn(_ sender: UIButton) {
if TitleTxt.text!.isEmpty || CityTxt.text!.isEmpty ||
CountryTxt.text!.isEmpty || VenueTxt.text!.isEmpty ||
TimeTxt.text!.isEmpty || UrlTxt.text!.isEmpty{
let alert = UIAlertController(title: "Missing values", message: "Please populate all fields", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else {
print("The event title is \((TitleTxt.text)!)")
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "CreateEvntBtn") {
let destVC = segue.destination as! CreatedEventController
destVC.CrTitle = TitleTxt.text!
destVC.CrCity = CityTxt.text!
destVC.CrCountry = CountryTxt.text!
destVC.CrVenue = VenueTxt.text!
destVC.CrTime = TimeTxt.text!
destVC.CrUrl = UrlTxt.text!
}
}
}
}
}
这里是应该显示信息的视图控制器:
import UIKit
class CreatedEventController: UIViewController {
@IBOutlet weak var CrTitleLbl: UILabel!
@IBOutlet weak var CrCityLbl: UILabel!
@IBOutlet weak var CrCountryLbl: UILabel!
@IBOutlet weak var CrVenueLbl: UILabel!
@IBOutlet weak var CrTimeLbl: UILabel!
@IBOutlet weak var CrUrlLbl: UILabel!
var CrTitle, CrCity, CrCountry, CrVenue, CrTime, CrUrl : String!
override func viewDidLoad() {
super.viewDidLoad()
CrTitleLbl.text = CrTitle
CrCityLbl.text = CrCity
CrCountryLbl.text = CrCountry
CrVenueLbl.text = CrVenue
CrTimeLbl.text = CrTime
CrUrlLbl.text = CrUrl
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我确定我遗漏了一些非常简单的东西,但我们将不胜感激。干杯!
您的 prepareForSegue
函数嵌套在另一个函数中。它需要处于 class 的级别。将其移到 SaveEventBtn
.
此外,您需要实际执行转场。在您当前 prepareForSegue
的位置,添加 performSegue(withIdentifier: "mySegueID", sender: nil)
。请注意,mySegueID
是您在情节提要中创建的转场的名称。您目前拥有的它与按钮操作相同,可能是这种情况,但您可能应该将其命名为其他名称。
此外,为了让你的代码在未来更易读,避免你自己因错误而头疼,Swift的标准是让变量名(TitleTxt
,CountryTxt
)和函数名称 (SaveEventBtn
) 驼峰式大小写 (titleTextField
, countryTextField
, saveTapped
).
首先,您想覆盖 prepare(for:sender:)
,而不是创建嵌套。其次,您要调用 performSegue
,这将启动导航:
import UIKit
class CreateEventController: UIViewController {
@IBOutlet weak var TitleTxt: UITextField!
@IBOutlet weak var CityTxt: UITextField!
@IBOutlet weak var CountryTxt: UITextField!
@IBOutlet weak var VenueTxt: UITextField!
@IBOutlet weak var TimeTxt: UITextField!
@IBOutlet weak var UrlTxt: UITextField!
@IBAction func SaveEventBtn(_ sender: UIButton) {
if TitleTxt.text!.isEmpty || CityTxt.text!.isEmpty ||
CountryTxt.text!.isEmpty || VenueTxt.text!.isEmpty ||
TimeTxt.text!.isEmpty || UrlTxt.text!.isEmpty {
let alert = UIAlertController(title: "Missing values", message: "Please populate all fields", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
} else {
print("The event title is \((TitleTxt.text)!)")
// this will perform the segue (of course the segue with the given identifier must exist)
self.performSegue(withIdentifier: "CreateEvntBtn", sender: self)
}
}
// this will give you the opportunity to configure your destination VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "CreateEvntBtn") {
let destVC = segue.destination as! CreatedEventController
destVC.CrTitle = TitleTxt.text!
destVC.CrCity = CityTxt.text!
destVC.CrCountry = CountryTxt.text!
destVC.CrVenue = VenueTxt.text!
destVC.CrTime = TimeTxt.text!
destVC.CrUrl = UrlTxt.text!
}
}
}
三期:
prepareForSegue
必须在 class 的顶层,而不是在另一个方法中。prepareForSegue
的签名有误。这是func prepare(for segue: UIStoryboardSegue, sender: Any?) {
在
IBAction
你必须调用performSegue(withIdentifier:sender)
else { print("The event title is \((TitleTxt.text)!)") performSegue(withIdentifier:"CreateEvntBtn", sender:nil) }
如果您是 Swift 的新手,请习惯函数和变量名称以小写字母开头的命名约定。