我的textFieldShouldReturn上有以下代码(与所调用的方法不在同一类中):

  - (BOOL)textFieldShouldReturn:(UITextField *)textField {
     LoginViewController *loginViewController = [[LoginViewController alloc]init];

     if (textField.returnKeyType == UIReturnKeyJoin) [loginViewController logIn];
     return (textField.returnKeyType == UIReturnKeyDone);
}

到现在为止,一切工作都很完美,这是“ login ”方法的代码:
  - (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在变量上显示空内容。
我想念什么?

最佳答案

我认为您有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
  • (void)登录:(NSString *)用户名密码:(NSString *)密码;

  • 在您的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内部的Keyboard交互的一个小解决方案。但是,只能使用“下一步”和“完成返回”键进行处理。要处理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
    }
    

    您可以download an example with the 4 options here:

    关于ios - textFieldShouldReturn忽略变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24857196/

    10-12 14:33