0

因此,我能够将文本字段作为登录表视图中的子视图填充。现在我想在我的登录操作中引用用户名文本字段和密码文本字段但是该操作找不到文本字段的变量(用户名和密码)请帮助!这一直让我发疯!这是我的代码。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (indexPath.row == 0) {
        UITextField *username = [[UITextField alloc] init];
        username.frame = CGRectMake(10, 6, 280, 30);
        username.placeholder = @"Username";
        cell.tag = 0;
        [username addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:username];
    } else {
        UITextField *password = [[UITextField alloc] init];
        password.frame = CGRectMake(10, 6, 280, 30);
        password.placeholder = @"Password";
        cell.tag = 1;
        [password addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
        [password setSecureTextEntry:YES];
        [cell.contentView addSubview:password];
    }




    return cell;


}

如您所见,我创建了文本字段用户名和密码。然而,变量没有在动作中被引用!每当出现“用户名”或“密码”一词时,它都会显示“使用未声明的标识符用户名”/“使用未声明的标识符密码”。对你的帮助表示感谢!

-(IBAction)login:(id)sender {

    if ([username.text isEqualToString:@""] || [password.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Please fill in all the fields!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }

    {
        NSString *strURL = [NSString stringWithFormat:@"http://www.mysite.com/myfile.php?username=%@&password=%@",username.text, password.text];

        NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

        NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

        NSLog(@"%@", strResult);

        NSString *cont11 = [NSString stringWithFormat:@"http://www.mysite.com/myfile.php?username=%@&password=%@",username.text, password.text];

        NSData *cont12 = [NSData dataWithContentsOfURL:[NSURL URLWithString:cont11]];


        NSString *cont13 = [[NSString alloc] initWithData:cont12 encoding:NSUTF8StringEncoding];

        NSLog(@"%@", cont13);

        if ([strResult isEqualToString:@"1"])
        {


            UIStoryboard *mainStoryboard=[UIStoryboard
                                          storyboardWithName:@"Storyboard" bundle:nil];

            AdminPanel *mainView=[mainStoryboard
                                              instantiateViewControllerWithIdentifier:@"admin"];

            mainView.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

            [self presentViewController:mainView animated:YES completion:nil];

        }else
        {
            // invalid information
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"You must have entered something wrong! Try again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            return;

        }
    }

}
4

1 回答 1

0

您必须在 .m 文件的 @interface 下创建变量:

@interface YourViewController ()
{
       UITextField *username;
       UITextField *password;
}

然后在初始化这些变量时取出“UITextField”。

于 2013-07-27T09:35:04.827 回答