Swift:插入带有文本输入的警报框(和存储文本输入) [英] Swift: Insert Alert Box with Text Input (and Store Text Input )

查看:60
本文介绍了Swift:插入带有文本输入的警报框(和存储文本输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个 viewController 中,我想让 alert box 出现,提示 user 输入此信息.然后,我希望用户使用 NSUserDefaults 存储此输入.我怎样才能做到这一点?

In one of my viewController, I want to make an alert box appear that prompts the user to type this information.Then, I want the user to store this input using NSUserDefaults. How can I achieve this?

先谢谢你!

推荐答案

看看这个:

let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)

let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
  guard let textFields = alertController.textFields,
    textFields.count > 0 else {
      // Could not find textfield
      return
  }

  let field = textFields[0]
  // store your data
  UserDefaults.standard.set(field.text, forKey: "userEmail")
  UserDefaults.standard.synchronize()
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

alertController.addTextField { (textField) in
  textField.placeholder = "Email"
}

alertController.addAction(confirmAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)

这篇关于Swift:插入带有文本输入的警报框(和存储文本输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆