任何人都可以帮助我
如何显示所有用户
import CoreData
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Username: UITextField!
@IBOutlet weak var Password: UITextField!
var array = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Login(_ sender: Any)
{
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
//let predicate = NSPredicate(format: "name == %@ && password == %@", Username.text! ,Password.text! )
//request.predicate = predicate
request.fetchLimit = 1
do
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let users = try! context.fetch(request)
// for var i in 0 ..< Users.count
if(users.count > 0)
{
let user = users.first as? Users
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "UsersData") as? UsersData
nextViewController?.userInfo = user
self.present(nextViewController!, animated:true, completion:nil)
}
else
{
let alertController = UIAlertController(title: "",message:"No user Found", preferredStyle: .alert)
let OKAction = UIAlertAction(title:"Ok", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
@IBAction func RegisterButton(_ sender: Any) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "RegisterPage") as? RegisterPage
self.present(nextViewController!, animated:true, completion:nil)
}
}
这是我保存值的地方
这是我从
获取数据的地方class RegisterPage: UIViewController {
@IBOutlet weak var UsernameRegister: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var Mobile: UITextField!
@IBOutlet weak var PasswordRegister: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Register(_ sender: Any) {
if (UsernameRegister.text == "" || email.text == "" || Mobile.text == "" || PasswordRegister.text == "" )
{
let alertController = UIAlertController(title: "Note",message:"All fields are required", preferredStyle: .alert)
let OKAction = UIAlertAction(title:"Ok", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
else if !validateEmail(candidate:email.text!)
{
let alertController = UIAlertController(title: "",message:"Email not valid", preferredStyle: .alert)
let OKAction = UIAlertAction(title:"Ok", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
else if(UsernameRegister.text != "" && email.text != "" && Mobile.text != "" && PasswordRegister.text != "" )
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let userData = NSEntityDescription.insertNewObject(forEntityName:"Users",into:context)
userData.setValue(UsernameRegister.text, forKey: "name")
userData.setValue(email.text, forKey: "email")
userData.setValue(Mobile.text, forKey: "mobile")
userData.setValue(PasswordRegister.text, forKey: "password")
print(userData)
do
{
try context.save()
print(userData)
}
catch
{
print("error")
}
}
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController =
storyBoard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
self.present(nextViewController!, animated:true, completion:nil)