Ios 在swift中验证UIAlertAction中的用户
又是我。我对UIAlertAction中的简单密码验证有问题。这是我的应用程序的目标,在TableView中,当用户向右滑动时,将显示2个选项,1.-用于编辑记录,2。删除记录 当用户选择edit时,应用程序需要验证我所称的record what文件夹是否有密码。如果它有密码,它将提示用户插入密码。如果密码正确,UIAlertAction应显示下一个视图;如果密码不正确,我想显示一条错误消息,说明密码不正确,屏幕不应更改 以下是我的视图控制器的代码:Ios 在swift中验证UIAlertAction中的用户,ios,swift,Ios,Swift,又是我。我对UIAlertAction中的简单密码验证有问题。这是我的应用程序的目标,在TableView中,当用户向右滑动时,将显示2个选项,1.-用于编辑记录,2。删除记录 当用户选择edit时,应用程序需要验证我所称的record what文件夹是否有密码。如果它有密码,它将提示用户插入密码。如果密码正确,UIAlertAction应显示下一个视图;如果密码不正确,我想显示一条错误消息,说明密码不正确,屏幕不应更改 以下是我的视图控制器的代码: import UIKit import Co
import UIKit
import CoreData
class FoldersListViewController: UITableViewController, UITableViewDelegate,UITableViewDataSource {
@IBOutlet var myFoldersTableView: UITableView!
var context: NSManagedObjectContext!
// public property that represent an array of all folders created by an user in the application.
var folders: [Folder] = []
// public property that will allow to set the context that will be use to save the data.
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
self.loadFolders()
if(folders.count == 0){
loadTestData()
myFoldersTableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return folders.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: cellFolder = tableView.dequeueReusableCellWithIdentifier("cellFolder", forIndexPath: indexPath) as! cellFolder
let folder = folders[indexPath.row]
if(folder.picture.length > 0){
var newSize:CGSize = CGSize(width: 64,height: 64)
let rect = CGRectMake(0,0, newSize.width, newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, true, 0.0)
var photo = UIImage(data: folder.picture)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
photo!.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
cell.imageView?.layer.masksToBounds = true
cell.imageView?.layer.cornerRadius = 15.0
cell.imageView?.clipsToBounds = true
cell.imageView?.image = newImage
}
cell.labelFolderName.text = folder.name
return cell
}
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
func loadFolders() {
var request = NSFetchRequest(entityName: "Folder")
let handler = HACoreDataHandler()
context = handler.context
var error: NSError?
folders = context.executeFetchRequest(request, error: &error) as! [Folder]
if(error != nil){
let alert = UIAlertController(title: "Error", message: error?.description, preferredStyle: UIAlertControllerStyle.Alert)
var dismiss = UIAlertAction(title: "Dismiss", style: .Default) { (alertAction: UIAlertAction!) ->
Void in
}
alert.addAction(dismiss)
presentViewController(alert, animated: true, completion: nil)
}
}
func loadTestData(){
var tmpFolder = NSEntityDescription.insertNewObjectForEntityForName("Folder", inManagedObjectContext: context!) as! Folder
tmpFolder.name = "Erika Velez"
let tmpPhoto = UIImage(named: "ErikaVelez")
tmpFolder.picture = UIImageJPEGRepresentation(tmpPhoto, 1)
tmpFolder.isprotected = false
folders.append(tmpFolder)
var error: NSError?
context?.save(&error)
if(error != nil){
let alert = UIAlertController(title: "Error", message: error?.description, preferredStyle: UIAlertControllerStyle.Alert)
var dismiss = UIAlertAction(title: "Dismiss", style: .Default) { (alertAction: UIAlertAction!) ->
Void in
}
alert.addAction(dismiss)
presentViewController(alert, animated: true, completion: nil)
}
}
override func viewWillAppear(animated: Bool) {
self.loadFolders()
myFoldersTableView.reloadData()
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
var item = folders[indexPath.row] as Folder
folders.removeAtIndex(indexPath.row)
context?.deleteObject(item)
var error: NSError?
context?.save(&error)
if(error != nil){
let alert = UIAlertController(title: "Error", message: error?.description, preferredStyle: UIAlertControllerStyle.Alert)
var dismiss = UIAlertAction(title: "Dismiss", style: .Default) { (alertAction: UIAlertAction!) ->
Void in
}
alert.addAction(dismiss)
presentViewController(alert, animated: true, completion: nil)
}
self.myFoldersTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var delete = UITableViewRowAction(style: .Default, title: "Delete") { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
var folder = self.folders[indexPath.row] as Folder
var handler = HACoreDataHandler()
var error: NSError?
self.folders.removeAtIndex(indexPath.row)
handler.context?.deleteObject(folder)
handler.context?.save(&error)
self.myFoldersTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
delete.backgroundColor = UIColor.redColor()
var details = UITableViewRowAction(style: .Normal, title: "Edit") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
let tmpFolder = self.folders[indexPath.row] as Folder
let destination = self.storyboard?.instantiateViewControllerWithIdentifier("editFolder") as! EditFolderViewController
destination.folder = tmpFolder
let navigationController = self.parentViewController as! UINavigationController
var response: Bool = true
if( tmpFolder.isprotected == true){
var alert = UIAlertController(title: "Password Validation", message: "Please enter the folder password", preferredStyle: .Alert)
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = ""
})
alert.addAction(UIAlertAction(title: "Login", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as! UITextField
if (tmpFolder.password == textField.text){
response = true
self.performSegueWithIdentifier("listOcassions", sender: self)
} else {
let wrongPasswordAlert = UIAlertController(title: "Error", message: "Invalid password.", preferredStyle: UIAlertControllerStyle.Alert)
var dismiss = UIAlertAction(title: "Dismiss", style: .Default) { (alertAction: UIAlertAction!) ->
Void in
}
wrongPasswordAlert.addAction(dismiss)
self.presentViewController(wrongPasswordAlert, animated: true, completion: nil)
response = false
} // else if (folder.password == textField.text)
}))
}
if(response){
navigationController.pushViewController(destination, animated: true)
}
//navigationController.presentViewController(destination, animated: true, completion: nil)
//self.showDetailViewController(destination, sender: self)
}
details.backgroundColor = UIColor.grayColor()
return [delete,details]
}
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
var tmpFolder = folders[fromIndexPath.row]
folders.removeAtIndex(fromIndexPath.row)
folders.insert(tmpFolder, atIndex: toIndexPath.row)
}
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier=="listOcassions"){
let ocassions = segue.destinationViewController as! DatesViewController
var path = myFoldersTableView.indexPathForSelectedRow()
var folder = folders[path!.row]
ocassions.folder = folder
}
}
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
var response: Bool = true
if let ident = identifier{
if ident == "listOcassions" {
let path = myFoldersTableView.indexPathForSelectedRow()
var folder = folders[path!.row]
if( folder.isprotected == true){
var alert = UIAlertController(title: "Password Validation", message: "Please enter the folder password", preferredStyle: .Alert)
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = ""
})
alert.addAction(UIAlertAction(title: "Login", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as! UITextField
if (folder.password == textField.text){
response = true
self.performSegueWithIdentifier("listOcassions", sender: self)
} else {
let wrongPasswordAlert = UIAlertController(title: "Error", message: "Invalid password.", preferredStyle: UIAlertControllerStyle.Alert)
var dismiss = UIAlertAction(title: "Dismiss", style: .Default) { (alertAction: UIAlertAction!) ->
Void in
}
wrongPasswordAlert.addAction(dismiss)
self.presentViewController(wrongPasswordAlert, animated: true, completion: nil)
response = false
} // else if (folder.password == textField.text)
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
} //if folder.isprotected == true
}
}
return response
}
}
下面是我放置的突出显示的代码,但它不起作用。非常感谢您的帮助
提前谢谢
Julio.你说它不工作是什么意思?问题是什么?你似乎没有在任何地方显示警报控制器。嗨,当我说它不工作时,我的意思是验证没有发生,因为如果你检查这一行,这是我试图显示警报的地方Hi Paulw11,很抱歉我错过了这一行,但即使如此,屏幕仍会加载,无需等待密码验证