2013-07-27 70 views
0

所以我能够获取文本字段作为子视图登录我的表视图。现在我想在我的登录操作中引用用户名文本字段和密码文本字段,但操作无法找到文本字段的变量(用户名和密码)请帮助!这一直让我疯狂!这是我的代码。表格单元格/ UITextField/UIControlEvent

-(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; 

     } 
    } 

} 
+0

你想获得用户名和密码获取用户名和密码值? –

+0

发件人将是结束编辑的textField,因此您可以将username.text替换为sender.text - 为此,您还应该将方法的签名更改为 - (IBAction)login:(UITextField *)sender – rdelmar

回答

0

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

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

然后拿出 “的UITextField” 当你初始化这些变量。