在非结构或联合的东西中请求成员'uitextfield'?

时间:2011-05-11 02:57:37

标签: iphone uitableview uitextfield

解决了我在此时遇到的其他几个问题。我已经学到了很多关于我想要做的事情,但是这个我完全被我难以接受了。

我正在为我的应用程序上的登录编写一个文本字段格式,并且因为苹果没有任何类似于文本字段掩码的东西我决定编写我自己的自定义单元格,看起来它有一个掩码在它上面但我将要做的就是在后台连接文本字段。

但是我遇到了一个问题。我正在尝试调用textField:shouldChangeCharactersInRange:replacementString:我的Subclassed UITableViewCell中的UITextField,如下面的代码所示。但是我收到成员'uitextfield'的请求,而不是结构或联合错误...任何帮助将不胜感激。

////的.h

@interface RegisterDeviceViewController : UIViewController <UITableViewDelegate, UITextFieldDelegate> {

    RegisterDeviceViewController *registerDeviceViewController;

    //UITextFields for the registration cell
    UITextField *regFieldOne;
    UITextField *regFieldTwo;
    UITextField *regFieldThree;
    UITextField *regFieldFour;

    UITableViewCell *myRegistrationField;
    UITableViewCell *mySubmitButton;

}
//UITextFields for the registration cell
@property (nonatomic, retain) IBOutlet UITextField *regFieldOne;
@property (nonatomic, retain) IBOutlet UITextField *regFieldTwo;
@property (nonatomic, retain) IBOutlet UITextField *regFieldThree;
@property (nonatomic, retain) IBOutlet UITextField *regFieldFour;

@property (nonatomic, retain) IBOutlet UITableViewCell *myRegistrationField;
@property (nonatomic, retain) IBOutlet UITableViewCell *mySubmitButton;

@end

/////。米

#import "RegisterDeviceViewController.h"


@implementation RegisterDeviceViewController

//Custom registration cell fields
@synthesize regFieldOne;
@synthesize regFieldTwo;
@synthesize regFieldThree;
@synthesize regFieldFour;

@synthesize myRegistrationField;
@synthesize mySubmitButton;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    self.title = @"Registration";
    [super viewDidLoad];
}




//Sets number of sections in the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

// Sets the number of rows in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//Loads both Custom cells into each section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"mythingy"];
    if (cell == nil) {
        cell = myRegistrationField;
    }


    UITableViewCell *cellButton = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"mummy"];
    if (cellButton == nil) {
        cellButton = mySubmitButton;
    }

    if (indexPath.section == 0) {
        return cell;

        cell.regFieldOne.delegate = self; //This is where the error is.
    }
    return cellButton;

}

//This delegate method is not being called.
//textField:shouldChangeCharactersInRange:replacementString:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int length = [regFieldOne.text length] ;
    if (length >= MAXLENGTH && ![string isEqualToString:@""]) {
        regFieldOne.text = [regFieldOne.text substringToIndex:MAXLENGTH];
        return NO;
    }
    return YES;
}

..........

1 个答案:

答案 0 :(得分:2)

regFieldDone不是单元格的成员(表格视图单元格)。它是您的类注册设备视图控制器的成员。如果您尝试将regFieldDone的委托设置为self,则将该语句更改为regFieldDone.delegate = self

编辑:您可以直接从xib文件中将所有文本字段的委托设置为RegisterDeviceViewController(文件所有者)。

您只将regFieldDone的委托设置为self,其他textField的委托怎么办?因此,当您编辑其他textField时,将不会调用委托方法。

您应该注意到编辑regFieldDone时调用了shouldChangeCharactersInRange ...方法,并且在编辑其他textField时没有调用。我建议你将所有textField的委托设置为self,无论是以编程方式还是从xib文件。