OutLet Collection
如下图所示,注册界面必填项有姓名、密码和邮箱等内容;由于对三个必填项做同一操作,故用Collection把它们合在一起:
@IBOutlet var loginTextField: [UITextField]!
image.png
这里我们还用了SwiftNotice组件对三个选项为空时的提示,代码如下:
//检查必填
func checkRequeriedField(){
for textField in loginTextField{
if textField.text!.isEmpty{
self.errorNotice("必填项为空")
}
}
}
AJWValidator与UITextBox
这里我们用AJWValidator对三个必填项进行校验;譬如姓名,验证规则是最少3个有效字,最大15个有效字:
let v1 = AJWValidator(type: .String)
v1.addValidationToEnsureMinimumLength(3, invalidMessage:"用户名至少3位")
v1.addValidationToEnsureMaximumLength(15, invalidMessage:"最大15位")
self.user.ajw_attachValidator(v1)
为了在textfield栏中能及时显示状态,这里我们用了UITextBox的第三方类库,首先需要把所有的TextField的类变成UITextBox:
@IBOutlet var loginTextField: [UITextField]!
@IBOutlet weak var user: UITextBox!
@IBOutlet weak var pass: UITextBox!
@IBOutlet weak var mail: UITextBox!
@IBOutlet weak var region: UITextBox!
@IBOutlet weak var question: UITextBox!
@IBOutlet weak var answer: UITextBox!
这样我们在AJWValidator中定义validatorStateChangedHandler函数中UITextBox的highlight即可实现及时响应:
v1.validatorStateChangedHandler = {
(newState: AJWValidatorState) -> Void in
switch newState {
case .ValidationStateValid:
self.pass.highlightState = .Default
self.possibleInputs.unionInPlace(Inputs.pass)
default:
let errorMsg = v1.errorMessages.first as? String
self.pass.highlightState = UITextBoxHighlightState.Wrong(errorMsg!)
self.possibleInputs.subtractInPlace(Inputs.pass) }
}
效果如下:
image.png
image.png
OptionSet
这里我们用iOS的新特性OptionSet来判断按钮是否可以enable,链接为(此处对该特性有说明):
http://www.jianshu.com/p/ef14cf0cb37c
定义三个必填项:
import Foundation
struct Inputs: OptionSetType {
let rawValue: Int
static let user = Inputs(rawValue: 1) //1
static let pass = Inputs(rawValue: 1 << 1) //10
static let mail = Inputs(rawValue: 1 << 2) //100
}
//判断是否全部输入
extension Inputs {
func isAllOK() -> Bool {
let count = 3
var found = 0
for time in 0..<count where contains(Inputs(rawValue: 1<<time)) {
found += 1
}
return found == count
}
}
然后必填项满足条件时,添加进Input;不满足时减少:
//添加
self.possibleInputs.unionInPlace(Inputs.pass)
//减少
self.possibleInputs.subtractInPlace(Inputs.pass)
//判断按钮
self.doneButton?.enabled = self.possibleInputs.isAllOK()
AVCloud云存储
此处用LeanCloud对对象进行存储,在Appdelegate位置调用LeanCloud的API:
RCIM.sharedRCIM().userInfoDataSource = self
AVOSCloud.setApplicationId("hYqPVQixSM8LvH9Xj7fDSY8l-gzGzoHsz", clientKey: "bzCC5AMRmWKo6qNAPrflL3dr")
并在点击按钮时存储用户信息:
func doneButtonTap(){
//显示一个载入提示
self.pleaseWait()
//建立用户的 AVObject
let user = AVObject(className: "XBUser")
//把输入的文本框的值,设置到对象中
user["user"] = self.user.text
user["pass"] = self.pass.text
user["mail"] = self.mail.text
user["region"] = self.region.text
user["question"] = self.question.text
user["answer"] = self.answer.text
//查询用户是否已经注册
let query = AVQuery(className: "XBUser")
query.whereKey("user", equalTo: self.user.text)
//执行查询
query.getFirstObjectInBackgroundWithBlock {
(object, e) -> Void in
self.clearAllNotice()
//如果查询到相关用户
if object != nil {
self.errorNotice("用户已注册")
self.user.becomeFirstResponder()
self.doneButton?.enabled = false
}else {
//用户注册
user.saveInBackgroundWithBlock({
(succeed, error) -> Void in
if succeed{
self.successNotice("注册成功")
self.navigationController?.popViewControllerAnimated(true)
}else{
print(error)
}
})
}
}
}
网友评论