我正在尝试使用类似于Notes应用程序的Swift构建应用程序,但是当用户按下“+”按钮添加新笔记时我想要弹出警报 - 并且警报会提示用户输入名称。然后将该名称添加为基础表中的新行。到目前为止,我已经能够做到这一点,但每次我点击“+”符号,我添加到警报的按钮都会重新添加。换句话说,警报应该有一个“OK”&一个“取消”按钮。但是在第二次弹出警报时,有2个“OK”按钮和2个“取消”按钮,依此类推。我有什么想法可以解决这个问题?谢谢你的帮助!
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
var data:[String] = []
//create alert controller
var alert = UIAlertController(title: "Name", message: "Enter a name", preferredStyle: .alert)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "Notes"
//create "add" button
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addAlert))
self.navigationItem.rightBarButtonItem = addButton
//create edit button so we can delete rows
self.navigationItem.leftBarButtonItem = editButtonItem
load()
}
//function to add new note. This code is executed when the "Add" button is clicked
func addNote()
{
//code to prevent being able to add rows while in edit mode
if (table.isEditing) {
return
}
let textField = self.alert.textFields![0] as UITextField
let name:String = textField.text!
data.insert(name, at: 0)
let indexPath:IndexPath = IndexPath(row: 0, section: 0)
table.insertRows(at: [indexPath], with: .automatic)
save()
}
func addAlert()
{
alert.addTextField(configurationHandler: { (textField) -> Void in
textField.placeholder = "Enter Name"})
self.present(alert, animated: true, completion: nil)
//grab value from the text field and print when user clicks OK
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (UIAlertAction) in
let textField = self.alert.textFields![0] as UITextField
print("Text Field: \(textField.text!)")
self.addNote()
}))
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
//puts table in edit mode
table.setEditing(editing, animated: animated)
}
//function to delete rows from table
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
data.remove(at: indexPath.row)
table.deleteRows(at: [indexPath], with: .fade)
save()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "detail", sender: nil)
}
func save()
{
UserDefaults.standard.set(data, forKey: "notes")
UserDefaults.standard.synchronize()
}
func load()
{
if let loadedData = UserDefaults.standard.value(forKey: "notes") as? [String]
{
data = loadedData
table.reloadData()
}
}
答案 0 :(得分:1)
在显示提醒之前输入alert.addAction。在addAlert()方法中尝试此代码
func addAlert()
{
var alert = UIAlertController(title: "Name", message: "Enter a name", preferredStyle: .alert)
alert.addTextField(configurationHandler: { (textField) -> Void in
textField.placeholder = "Enter Name"})
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (UIAlertAction) in
let textField = self.alert.textFields![0] as UITextField
print("Text Field: \(textField.text!)")
self.addNote()
}))
self.present(alert, animated: true, completion: nil)
}
答案 1 :(得分:0)
只有在添加动作(按钮)后才能显示警告。
public static String encrypt(String originalPassword) throws Exception {
String methodName = "encrypt -->";
_logger.debug(methodName + Constants.CALLED);
String encryptedString = null;
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
encryptedString = base64Encode(pbeCipher.doFinal(originalPassword.getBytes("UTF-8")));
_logger.debug(methodName + "encrypted string " + encryptedString);
} catch (Exception e) {
_logger.error(methodName + "Encryption failed due to: " + e.getMessage());
throw new Exception("Failed to Encrypt String");
}
_logger.debug(methodName + Constants.END);
return encryptedString;
}
public static String decrypt(String encryptedPassword) throws Exception {
String methodName = "decrypt -->";
_logger.debug(methodName + Constants.CALLED);
String decryptedString = null;
try {
_logger.debug(methodName + " string to decrypt " + encryptedPassword);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
decryptedString = new String(pbeCipher.doFinal(base64Decode(encryptedPassword)), "UTF-8");
} catch (Exception e) {
_logger.error(methodName + "Decryption failed due to: " + e.getMessage());
throw new Exception("Failed to Decrypt String");
}
_logger.debug(methodName + Constants.END);
return decryptedString;
}