如何保存自定义单元格文本字段的数据?

时间:2012-05-10 07:23:31

标签: iphone objective-c ios5

enter image description here

我是iPhone开发新手,我正在开发一个应用程序,我有自定义单元格的tableview,我的自定义单元格包含标签和textfiled。这是我的注册屏幕,如何保存文本字段的数据,以及如何对文本进行验证。在textfield结尾,我们可以保存数据。但如果第一个文件的用户输入名称,然后他点击完成按钮,它将显示请输入名字,因为我在didendediting中保存数据。我想在用户在文本字段时保存数据。

3 个答案:

答案 0 :(得分:0)

使控制器成为单元格textField的委托,然后在控制器中实现这些方法。

- (void)textFieldDidEndEditing:(UITextField *)textField
{    
    NSInteger tag = [textField tag];
if(tag == 0) {
//save first name text value
}
else if (tag == 1){
//save last name text value
}
else if etc....
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

确保在

中返回单元格时正确设置标记

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

您还可以进行一些研究,了解如何让键​​盘像下一个/上一个按钮的表单处理程序一样

答案 1 :(得分:0)

Register.h

@interface Register : NSObject {
    NSString *_firstNameTxt;
    NSString *_lastNameTxt;
NSString *_emailTxt;
    NSString *_passwordTxt;
}

@property (nonatomic, retain) NSString * firstNameTxt;
@property (nonatomic, retain) NSString * lastNameTxt;
@property (nonatomic, retain) NSString * emailTxt;
@property (nonatomic, retain) NSString * passwordTxt;
@end

Register.m

@implementation Register
@synthesize firstNameTxt=_firstNameTxt;
@synthesize lastNameTxt =_lastNameTxt ;
@synthesize emailTxt=_emailTxt;
@synthesize passwordTxt= _passwordTxt;

- (void)dealloc{
[super dealloc];
 [_firstNameTxt release];
 [_lastNameTxt  release];
 [_emailTxt  release];
 [_passwordTxt  release];
 }
 @end

RegisterViewController.h

@class Register;

@interface RegisterViewController : UIViewController {
    Register *objRegister;
}

RegisterViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
  objRegister = [[Register alloc] init];
}

tableview委托

[txtFirstName setText:objRegister.firstNameTxt];
[txtLastName setText:objRegister.lastNameTxt];

-(void) textFieldDidEndEditing:(UITextField *)textField{
// do the same code as Warren Burton Post

 }

答案 2 :(得分:0)

您可以使用此方法跟踪当前的文本字段文本,借助模型类对象来存储文本。

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *characterSet = nil; 
    switch (theTextField.tag) 
    {
        case 1:
            characterSet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
            [sharedInstance.registrationDetails setFirstName:theTextField.text];


            default:
            characterSet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?/!@#$&*.,-:; _";
            break;
    }
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:characterSet];
    for (int i = 0; i < [string length]; i++)
    {
        unichar c = [string characterAtIndex:i];
        if (![myCharSet characterIsMember:c]) 
        {
            return NO;
        }
    }
    return YES;
}