我在textFieldShouldReturn
上有这个代码(与调用的方法不在同一个类中):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
LoginViewController *loginViewController = [[LoginViewController alloc]init];
if (textField.returnKeyType == UIReturnKeyJoin) [loginViewController logIn];
return (textField.returnKeyType == UIReturnKeyDone);
}
到目前为止,一切都很完美,这是" 登录"的代码。方法:
- (IBAction)logIn{
NSString *username = [[self.usernameLoginField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
NSString *password = [[self.passwordLoginField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
NSLog(@"Username: %@ - Password: %@ ",username,password);
//Whatever
}
在我的view
上,我有一个调用该方法的按钮,当我使用该按钮时NSLog
显示UITextField
在那一刻包含的内容,否则,如果方法是来自" 加入"从键盘输入(返回键),NSLog
在变量上显示空内容
我错过了什么?。
答案 0 :(得分:2)
在我看来,你有3个可能的解决方案:
选项1:最佳选择恕我直言。
您可以将UIAlertView与UIAlertViewStyle = UIAlertViewStyleLoginAndPasswordInput
一起使用self.alertView = [[UIAlertView alloc] initWithTitle:nil
message:@"LogIn"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Cancel", @"Join", nil];
self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[self.alertView show];
然后与代表:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == self.alertView)
{
if (buttonIndex == 1)
{
[self logIn:[[alertView textFieldAtIndex:0] text] andPassword:[[alertView textFieldAtIndex:1] text]];
}
else if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
}
请务必添加:UIAlertViewDelegate
选项2:
使用[self performSegueWithIdentifier:@"identifier" sender:self]
和
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
#pragma mark - UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.usernameLoginField)
{
[self.usernameLoginField resignFirstResponder];
[self.passwordLoginField becomeFirstResponder];
}
else if (textField == self.passwordLoginField)
{
[self.passwordLoginField resignFirstResponder];
//This Identifier is in the Storyboard
[self performSegueWithIdentifier:@"option2Segue" sender:self];
}
return true;
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[(YourViewController *)segue.destinationViewController setUsername:self.usernameLoginField.text];
[(YourViewController *)segue.destinationViewController setPassword:self.passwordLoginField.text];
}
在YourViewController.h中,您需要:
@property (nonatomic, strong) NSString* username;
@property (nonatomic, strong) NSString* password;
请务必添加:UITextFieldDelegate
选项3
要发回信息,您应该使用委托:
在firstViewController.h
:
#import "DelegateViewController.h"
@interface FirstViewController:UIViewController
在firstViewController.m
:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"option3Delegate"])
{
((DelegateViewController *) segue.destinationViewController).delegate = self;
}
}
在delegateViewController.h
:
@protocol Option3Delegate <NSObject>
- (void) logIn: (NSString *) username password: (NSString *) password;
@end
@interface DelegateViewController : UIViewController
{
id myDelegate;
}
@property (nonatomic, assign) id<Option3Delegate> delegate;
@end
在delegateViewController.m
:
#pragma mark - UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.usernameLoginField)
{
[self.usernameLoginField resignFirstResponder];
[self.passwordLoginField becomeFirstResponder];
}
else if (textField == self.passwordLoginField)
{
[self.passwordLoginField resignFirstResponder];
//This Identifier is in the Storyboard
if ([_delegate respondsToSelector:@selector(logIn:password:)])
{
[_delegate logIn:self.usernameLoginField.text password:self.passwordLoginField.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
return true;
}
请务必添加:UITextFieldDelegate
选项4:使用KeyboardController
KeyboardController是一个小型解决方案,用于处理UITextFields
内的键盘交互。但是只能处理Next和Done Return键。要处理Join键,您可以在ViewController中执行此操作。在您分配KeyboardController
的视图控制器中:
@property (strong, nonatomic) IBOutlet UITextField *usernameLoginField;
@property (strong, nonatomic) IBOutlet UITextField *passwordLoginField;
@property (strong, nonatomic) KeyboardController *keyboardController;
- (void)viewDidLoad
{
[super viewDidLoad];
id fields = @[self.usernameLoginField, self.passwordLoginField];
self.keyboardController = [KeyboardController controllerWithFields:fields];
//Important
self.passwordLoginField.delegate = self;
}
#pragma mark - UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.passwordLoginField)
{
[self.passwordLoginField resignFirstResponder];
[self logIn];
}
return true;
}
- (IBAction)logIn
{
NSLog(@"Username: %@ - Password: %@", self.usernameLoginField.text, self.passwordLoginField.text);
//Whatever
}