-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCH_Username.swift
166 lines (123 loc) · 4.54 KB
/
CH_Username.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// CH_Username.swift
// Dac
//
// Created by Lyndon Samual McKay on 1/19/16.
// Copyright © 2016 Lyndon Samual McKay. All rights reserved.
//
import UIKit
import Parse
protocol SidebarDelegate {
func queryNotiis()
func userInfoQuery()
}
class CH_Username: UIViewController, UITextFieldDelegate {
@IBOutlet var usernameTX: UITextField! = nil
@IBOutlet var diko: UIButton!
@IBOutlet var invalidLabel: UILabel!
let cUser = PFUser.currentUser()
var theUsername : String?
var theSay : String?
var delegate = SidebarDelegate?()
override func viewDidLoad() {
super.viewDidLoad()
usernameTX.delegate = self
usernameTX.becomeFirstResponder()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func goChange(sender: AnyObject) {
self.theUsername = self.usernameTX.text
while theUsername!.characters.last == " "{
// if theTopic?.characters.last == " "{
print("remove")
// theTopic = newTopicTX.text
print("\(theUsername)ttt")
var toko = theUsername!.substringToIndex(theUsername!.endIndex.predecessor())
self.theUsername = toko
print("\(theUsername)ttt")
}
if usernameTX?.text!.characters.count < 5{
print("Username is too Short!")
//Show Short water later
self.invalidLabel.text = "Username must contain at least 5 letters"
self.invalidLabel.alpha = 1
theSay = "no"
}else{
theSay = "yes"
if theSay == "yes"{
// self.theUsername = self.usernameTX.text
let _ = checkForUsername(theUsername!)
}else{
// show INVALID LABEL
print("Invalid username")
self.invalidLabel.text = "Sorry, this Username has been taken"
self.invalidLabel.alpha = 1
}
}
}
func chUserName(){
if let currentUser = PFUser.currentUser(){
currentUser.username = self.theUsername!
//set other fields the same way....
currentUser.saveInBackground()
self.sendher()
}else{
print("name change error")
}
}
func checkForUsername(textfield : String){
print(textfield)
let uC = PFQuery(className: "_User")
//var iii = 0
if theUsername != nil{
uC.whereKey("username", equalTo: textfield)
uC.findObjectsInBackgroundWithBlock({ (results : [PFObject]?, error: NSError?) -> Void in
if error == nil{
// self.checker.removeAll()
if results!.count > 0{
self.cThat()
}else{
let _ = self.chUserName()
}
}
})
}
//sleep(1)
}
func cThat(){
// show that this username is taken
}
func sendher(){
if self.theUsername == nil{
self.theUsername = self.usernameTX.text
}
if let delegate = self.delegate{
let _ = delegate.queryNotiis()
let _ = delegate.userInfoQuery()
}
// let yy = SidebarViewController()
// yy.userInfoQuery()
// yy.viewDidLoad()
if self.theUsername != nil{
//diko.sendActionsForControlEvents(.TouchUpInside)
seggy()
}
}
func seggy(){
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: SWRevealViewController = storyboard.instantiateViewControllerWithIdentifier("homeR") as! SWRevealViewController
self.presentViewController(vc, animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}