在我的应用程序中,我使用segue在两个视图控制器之间传递数据,这应该很容易,但由于原因我无法看到我一直在那里"未解析的标识符"
她是与该功能有关的一些代码。
来自ViewController 1的
import UIKit
import CoreData
class ViewController: UIViewController, UITextFieldDelegate
{
@IBOutlet var panelWidthTextField: UITextField!
@IBOutlet var panelHightTextField: UITextField!
@IBOutlet var panelsWideTextField: UITextField!
@IBOutlet var panelsHightTextField: UITextField!
@IBOutlet var panelPitchTextField: UITextField!
@IBOutlet var calculateButton: UIButton!
@IBOutlet var resultWithLabel: UILabel!
@IBOutlet var resultHightLabel: UILabel!
@IBOutlet var fillAllFieldsLabel: UILabel!
var pawidth:String!
var pahight:String!
var papitch:String!
override func viewDidLoad()
{
super.viewDidLoad()
panelWidthTextField.text = pawidth
panelHightTextField.text = pahight
panelPitchTextField.text = pap itch
来自Second ViewController
import UIKit
import CoreData
class DataBase: UIViewController, UITextFieldDelegate
{
@IBOutlet var makerTextField: UITextField!
@IBOutlet var modelTextField: UITextField!
@IBOutlet var stPanelWidthTextField: UITextField!
@IBOutlet var stPanelHightTextField: UITextField!
@IBOutlet var stPitchTextField: UITextField!
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// Removes keyboard when touch outside edit field.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
view.endEditing(true)
super.touchesBegan(touches, withEvent: event)
}
@IBAction func saveButton(sender: UIButton)
{
let ed = NSEntityDescription.entityForName("Ledinfo", inManagedObjectContext: moc)
let model = Ledinfo(entity:ed!, insertIntoManagedObjectContext:moc)
model.manufactor = makerTextField.text
model.model = modelTextField.text
model.panelwidth = stPanelWidthTextField.text
model.panelhight = stPanelHightTextField.text
model.pitch = stPitchTextField.text
do {
try moc.save()
makerTextField.text = ""
modelTextField.text = ""
stPanelWidthTextField.text = ""
stPanelHightTextField.text = ""
stPitchTextField.text = ""
Alert.show("Succsess", message: "Your Record Is Saved", vc: self)
}
catch _ as NSError
{
Alert.show("Failed", message: "Something Went Wrong", vc: self)
}
}
@IBAction func searchButton(sender: UIButton)
{
let ed = NSEntityDescription.entityForName("Ledinfo", inManagedObjectContext: moc)
let req = NSFetchRequest()
req.entity = ed
let cond = NSPredicate(format: "manufactor = %@", makerTextField.text!)
req.predicate = cond
do {
let result = try moc.executeFetchRequest(req)
if result.count > 0
{
let model = result[0] as! Ledinfo
makerTextField.text = model.manufactor
modelTextField.text = model.model
stPanelWidthTextField.text = model.panelwidth
stPanelHightTextField.text = model.panelhight
stPitchTextField.text = model.pitch
} else
{
Alert.show("Failed", message: "No Record Is Found", vc: self)
}
} catch _ as NSError!
{
Alert.show("Failed", message: "No Record Is Found" , vc: self)
}
}
@IBAction func transfereButton(sender: UIButton) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "transfereButton") {
let svc = segue.destinationViewController as! ViewController
svc.pawidth = stPanelWidthTextField.text
svc.pahight = stPanelHightTextField.text
svc.papitch = stPitchTextField.text
}
}
}
找不到panelWidthTextField.text,panelHightTextField.text和panelPitchTextField.text作为标识符。
我检查拼写,似乎无法找到遗漏的内容。
感谢任何帮助
答案 0 :(得分:1)
&#34; Segue公司&#34;意味着,准备在那里&#34; prepareForSegue&#34;方法您将ViewController的属性设置为DataBase控制器中的某些数据。在您的示例中,可以这样做:
svc.pawidth = someDataFromDataBaseWhichYouWantToPassToSecondVC
svc.pahight = someDataFromDataBaseWhichYouWantToPassToSecondVC
svc.papitch = someDataFromDataBaseWhichYouWantToPassToSecondVC
然后,您可以从ViewController类操作此数据。 您错误地认为您没有将数据从一个VC传递到另一个VC,而不是您尝试将1stVC的属性设置为1stVC的另一个属性,并且不需要segue。
答案 1 :(得分:0)
这与segues无关。您的DataBase
课程中是否有3个文字字段,其中包含panelWidthTextField
,panelHightTextField
和panelPithcTextField
?它抱怨无法找到这些变量。
答案 2 :(得分:0)
您应该调用performSegueWithIdentifier("transfereButton", sender: nil)
IBOutlet操作中的transfereButton
来实际运行prepareForSegue
。