我使用Parse.com提供的默认注册/登录视图控制器。
我有它工作,但我希望textFields以小写形式回复所有条目,如果可能的话,使用下划线(_)表示空格。
过去一天我一直在调查这个问题,但在网上找不到任何有用的东西。
class ViewController: UIViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, UITextFieldDelegate {
var logInViewcontroller: PFLogInViewController! = PFLogInViewController()
var signUpViewController: PFSignUpViewController! = PFSignUpViewController()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if (PFUser.currentUser() == nil){
self.logInViewcontroller.fields = PFLogInFields.UsernameAndPassword | PFLogInFields.LogInButton | PFLogInFields.SignUpButton | PFLogInFields.PasswordForgotten | PFLogInFields.DismissButton
self.logInViewcontroller.delegate = self
self.signUpViewController.delegate = self
self.logInViewcontroller.signUpController = self.signUpViewController
self.logInViewcontroller.logInView?.usernameField!.delegate = self
} else {
self.performSegueWithIdentifier("logedIn", sender: self) //use found: moving forward
}
presentViewController(self.logInViewcontroller, animated: true, completion: nil)
}
func signUpViewController(signUpController: PFSignUpViewController, shouldBeginSignUp info: [NSObject : AnyObject]) -> Bool {
return true
}
func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser) {
self.dismissViewControllerAnimated(true, completion: nil)
}
我已经设置了UITextFieldDelegate,但不知道如何连接这两者。 我想我想用这个......
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
if (textField == self.logInViewcontroller.logInView?.usernameField) {
textField.text = textField.text.lowercaseString
return false // false or true?
}
}
还有其他人遇到过这个问题吗?
答案 0 :(得分:1)
您可以在文本字段中添加目标,以便更改控件事件编辑,而不是使用UITextField委托:
在viewDidAppear
中而不是这个“
self.logInViewcontroller.logInView?.usernameField!.delegate = self
添加:
self.logInViewcontroller.logInView?.usernameField.addTarget(self, action: "textChanged:", forControlEvents: .EditingChanged)
并添加您的方法来操纵字符串:
func textChanged(sender: UITextField) {
sender.text = sender.text!.lowercaseString.stringByReplacingOccurrencesOfString(" ", withString: "_")
}
BTW Leo在这里。 :)
答案 1 :(得分:0)
最后不要进行修改,因为用户可能不会注意到您可能会进行一些更改,这意味着他们无法使用刚刚创建的帐户。用户应始终确切地知道登录详细信息。因此,在键入每个字符时进行更改。使用textField:shouldChangeCharactersInRange:replacementString:
执行此操作。要连接代理人,您应该从logInView
获取logInViewcontroller
,PFLogInView
是usernameField
的实例,并且具有usernameField
等属性(您正在使用,但是您需要检查是否已创建public static boolean startOther(String left, String right) {
if (left == null || right == null || left.equals(right)) {
return false;
}
int rightSubstringInLeft = left.indexOf(right);
int leftSubstringInRight = right.indexOf(left);
if(rightSubstringInLeft != -1) {
return left.substring(rightSubstringInLeft).equals(right);
} else if(leftSubstringInRight != -1) {
return right.substring(leftSubstringInRight).equals(left);
} else {
return false;
}
}
并确保视图在尚未加载时加载。