在iOS应用中创建帐户原型时遇到问题

福尔摩斯

自8月以来,我一直在学习iOS开发,而我现在才开始开发我的第一个应用程序。在我甚至不知道如何开发应用程序之前,我一直在创建具有MINIMAL功能的小型原型应用程序,只是为了了解如何做我的实际产品的一部分。现在,我停留在一个仅创建一个帐户并将其存储在数组中以供以后使用的项目中,即在“我的个人资料”页面上显示数据模型对象及其各种属性;

更具体地说,我只是试图了解如何(从最基本的“基本知识”层面)从用户那里获取输入的信息(用户名,密码,全名),将其转换为我的数据模型对象,然后将其存储将对象分成相同类型的对象数组,最后在配置文件页面上显示数据模型对象。我正在使用它来了解应用程序一般体系结构的一部分。

import Foundation
import UIKit

class Account  {
    var username: String
    var password: String
    var fullName: String
    
    static var accounts: [Account] = []
    
    init(username: String, password: String, fullName: String) {
        self.username = username
        self.password = password
        self.fullName = fullName
    }
}

我的数据模型文件Account.swift

import UIKit

class RegisterViewController: UIViewController {
    
    @IBOutlet var usernameField: UITextField!
    @IBOutlet var passwordField: UITextField!
    @IBOutlet var fullNameField: UITextField!
    
    var newAccount = Account(username: "", password: "", fullName: "")
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func createAccount(_ sender: UIButton) {
        newAccount.username = usernameField.text!
        newAccount.password = passwordField.text!
        newAccount.fullName = fullNameField.text!
        
        Account.accounts.append(newAccount)
    }

我的视图控制器的注册视图

import UIKit

class ViewController: RegisterViewController {

    @IBOutlet var usernameLogin: UITextField!
    @IBOutlet var passwordLogin: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func loginToProfile(_ sender: UIButton) {
        if usernameLogin.text! == newAccount.username {
            print("success")
        } else {
            print("error")
        }
        
        if passwordLogin.text! == newAccount.password {
            print("success")
        } else {
            print("error")
        }
    }
    
    @IBAction func unwindToLogin(unwindSegue: UIStoryboardSegue)  {
        
    }

}

我的登录视图的veiw控制器。

在此处输入图片说明

顶部是创建帐户的视图,底部是登录视图。我现在的问题是通过登录vc中的if语句,我的控制台输出错误,表明在我尝试注册帐户然后登录后字符串不匹配。

I am aware that I force-unwrapped all the uitextfield optionals didnt think that affected much in this specific case. Nonetheless if there are any other blaring issues feel free to point them out

Kiril S.

From what it looks, you have 2 different controllers: RegisterViewController and another ViewController, which, although it subclasses RegisterViewController, is not the same instance. So when you fill in newAccount in RegisterViewController, it doesn't mean you are filling in newAccount inside ViewController.

What you should do, is instead to look for matching account in Account.accounts, and compare its properties to the one provided by a user, i.e.:

@IBAction func loginToProfile(_ sender: UIButton) {

    // Find the right account
    guard let account = Account.accounts.filter { $0.username == usernameLogin.text! } else {
        print("Error: No such account")
        return
    }

    // Now check if passwords match
    guard account.password == passwordLogin.text! else {
        print("Error: incorrect password")
        return
    }

Note: this is just minimal change to get you unstuck. But really, I would do some other changes:

  1. There's no reason for ViewController to subclass RegisterViewController - they are different view controllers, they share information, but they are completely different in their functional purpose.

  2. I would make Account a struct, that represents a single account. While static var accounts: [Account] = [] should be in some other class, that holds all the accounts, and, in the future, retrieves them from elsewhere. Simplest form of MVC pattern or an Accounts singleton for now will do. Later you can improve on that part, while struct Account will probably remain unchanged.

  3. Second change would also allow you to change the way you compare accounts: instead of function above, you could do something like (many variations possible, giving just 1 example, which assumes you have a singleton which holds all accounts)

    @IBAction func loginToProfile(_ sender: UIButton) {
    
        Accounts.shared.login(username: usernameLogin.text!, password: passwordLogin.text!)
    

    这将使您完全抽象如何从视图控制器验证用户

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Rails博客中创建和显示用户时遇到问题

来自分类Dev

在Neo4j中从CSV导入创建关系时遇到问题

来自分类Dev

使用time(NULL)在C中创建Delay()函数时遇到问题

来自分类Dev

在Ember应用中定位nth-child时遇到问题

来自分类Dev

应用样式时遇到问题-OpenPyXL

来自分类Dev

创建Laravel查询时遇到问题

来自分类Dev

创建外键时遇到问题

来自分类Dev

在Microsoft SQL Server Management Studio中创建视图时遇到问题

来自分类Dev

在python 3中应用python 2代码时遇到问题

来自分类Dev

在C#中创建用户控件时遇到问题

来自分类Dev

创建双重动画时遇到问题

来自分类Dev

在Laravel中创建用户时遇到问题

来自分类Dev

创建库包时遇到问题

来自分类Dev

创建ESLint CLIEngine时遇到问题

来自分类Dev

在Clojure中创建Uberjar时遇到问题

来自分类Dev

我在创建Web应用程序的python Django1.11.4的反向URL中遇到问题

来自分类Dev

在React应用程序中定义图像的路径时遇到问题

来自分类Dev

创建库包时遇到问题

来自分类Dev

在Windows Phone中创建类的对象时遇到问题

来自分类Dev

为Parse iOS应用程序启用推送通知-设置时遇到问题

来自分类Dev

在应用程序中实施Google Maps时遇到问题

来自分类Dev

创建Laravel查询时遇到问题

来自分类Dev

创建ViewController实例时遇到问题

来自分类Dev

在Android中的SQLite中创建表时遇到问题

来自分类Dev

在Cordova / PhoneGap应用程序中添加平台时遇到问题?

来自分类Dev

创建isPrime函数时遇到问题

来自分类Dev

我在 Angular 中创建模块时遇到问题

来自分类Dev

创建特定模式时遇到问题

来自分类Dev

在 Google 日历中创建和活动时遇到问题

Related 相关文章

热门标签

归档