2013-07-27 112 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」 當你初始化這些變量。