如何像在 Skype 应用程序中一样创建用户名/密码登录?我知道这是一个分组表视图..但我该怎么做呢?
我搜索了网站,发现了以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *startDtLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)];
if (indexPath.row == 0)
startDtLbl.text = @"Username";
else {
startDtLbl.text = @"Password";
}
startDtLbl.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:startDtLbl];
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)];
passwordTF.delegate = self;
if (indexPath.row == 0)
passwordTF.tag = 0;
else {
passwordTF.tag = 1;
}
[cell.contentView addSubview:passwordTF];
}
return cell;
}
当我做:
NSString * username = [((UITextField*)[self.view viewWithTag:0]) text];
NSString * password = [((UITextField*)[self.view viewWithTag:1]) text];
它给了我这个错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView text]: unrecognized selector sent to instance 0x6c3c600
为什么是这样?
将视图控制器分配为 UITextField 委托后,您可以使用以下委托方法在各个点访问文本字段的值:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
例如,在任何这些方法中,您都可以获取textField.text
value 并将其分配给一个 NSString,该 NSString 可以根据需要传递给您的 LOGIN 方法。
假设您的登录文本字段的标签值为 1,密码文本字段的标签值为 2,您可以使用下面的委托方法获取这些值:
- (void) textFieldDidEndEditing:(UITextField *)textField {
switch (textField.tag) {
case 1:
loginString = textField.text;
break;
case 2:
passwordString = textField.text;
break;
default:
break;
}
}
您还可以使用上述任何方法来获取相应单元格和索引路径的引用:
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell*)[[textField superview] superview]];
然后到单元格:
UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
如果您的表单很长并且想要滚动到下一个字段,这样键盘就不会位于您需要键入的单元格的顶部,这些功能将特别有用。您还可以使用这些引用将键盘中的 RETURN 键移动到下一个 UITextField,或者在用户完成编辑表单的最后一个字段时退出第一响应者。
我希望这是有道理的。
干杯,
罗格
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)