我有一个视图,它有一个用户输入服务器信息,并在提交切换到另一个视图。如果他们只是点击提交,它工作正常。我有另一个按钮,用于清除保存的服务器信息,如果他们想在某个时候更改它。问题是,如果在提交按钮之前按下resetServerInfo按钮,它就不起作用。
有效的用户流程:输入服务器名称/服务器IP - > testConnection - > submitServer - >切换视图控制器
失败时:clearServerInformation - >输入服务器名称/服务器IP - > testConnection连接 - >提交 - >什么都不做
代码:
class serverSelect: UIViewController {
@IBOutlet var serverNameField: UITextField!
@IBOutlet var serverIPField: UITextField!
@IBOutlet var successLabel: UILabel!
@IBOutlet var errorLabel: UILabel!
//Submit button - checks fields aren't empty, then saves info to core data
@IBAction func submitServer(sender: UIButton) {
errorLabel.hidden = true
if(!serverNameField.text!.isEmpty && !serverIPField.text!.isEmpty){
saveName(serverNameField.text!, ip: serverIPField.text!)
switchToViewController("userselect")
}
else {
errorLabel.text = "Fields must contain data"
errorLabel.hidden = false
}
}
//Calls deleteserverinformation on button click
@IBAction func resetServerInfo(sender: UIButton) {
let result = deleteServerInformation()
if(result){
successLabel.hidden = false
successLabel.text = "Server Information Reset"
}
else {
errorLabel.hidden = false
errorLabel.text = "Failed to delete. Try again."
}
}
//Removes all server information stored in core data
func deleteServerInformation() -> Bool {
let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDel.managedObjectContext
let coord = appDel.persistentStoreCoordinator
let fetchRequest = NSFetchRequest(entityName: "Server")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try coord.executeRequest(deleteRequest, withContext: context)
return true
} catch let error as NSError {
debugPrint(error)
return false
}
}
//Clears old server information and commits new data to core data
func saveName(name: String, ip: String) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Server")
let entity = NSEntityDescription.entityForName("Server",
inManagedObjectContext:managedContext)
let server = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext: managedContext)
server.setValue(name, forKey: "serverName")
server.setValue(ip, forKey: "serverIP")
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
let server = results as! [NSManagedObject]
if(server.count != 0){
deleteServerInformation()
}
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
//Call to switch view controller on successful submit
func switchToViewController(identifier: String) {
let viewController = self.storyboard!.instantiateViewControllerWithIdentifier(identifier)
self.navigationController?.pushViewController(viewController, animated: true)
}
}
我是新手,所以我想我只是缺少一些概念性的东西。我还没有很好地掌握核心数据。从我所看到的视图控制器流程都与app委托相关,所以它是否必须处理我的deleteServerInfo如何处理app del和我的SaveServer也正在处理它并且当一个接一个地使用它会导致问题?对不起,如果我离开了,但我唯一可以想到的是两个用户交互路径之间的真正变化。
基本上问题是" submitServer"函数运行正常并在视图控制器中没有按下其他按钮时将视图切换到用户选择视图控制器。但是,如果我按下触发" resetServerInfo"当我点击并运行" submitServer"再次,它不再切换视图控制器。