gpt4 book ai didi

iphone - 向单元格中的 uitextfield 添加方法?

转载 作者:行者123 更新时间:2023-12-03 18:42:03 24 4
gpt4 key购买 nike

我将此 Method 添加到我的代码中以格式化文本字段。我正在使用下面的代码尝试添加该方法,但它不起作用,我做错了什么?

.h 文件

NSString* phone_;
UITextField* phoneFieldTextField;
@property (nonatomic,copy) NSString* phone;

.m 文件

@synthesize phone = phone_;

ViewDidLoad{
self.phone = @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

// Make cell unselectable and set font.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:13];

if (indexPath.section == 0) {

UITextField* tf = nil;
switch ( indexPath.row ) {
case 3: {
cell.textLabel.text = @"Phone" ;
tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:@"xxx-xxx-xxxx"];
phoneFieldTextField.keyboardType = UIKeyboardTypePhonePad;

[self formatPhoneNumber:phoneFieldTextField.text deleteLastChar:YES];

[cell addSubview:phoneFieldTextField];
break ;
}
// Textfield dimensions
tf.frame = CGRectMake(120, 12, 170, 30);

// Workaround to dismiss keyboard when Done/Return is tapped
[tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];

}
}

// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {

//Section 1.
if ( textField == nameFieldTextField ) {
self.name = textField.text ;
} else if ( textField == addressFieldTextField ) {
self.address = textField.text ;
} else if ( textField == emailFieldTextField ) {
self.email = textField.text ;
} else if ( textField == phoneFieldTextField ) {
self.phone = textField.text ;
}else if ( textField == dateOfBirthTextField ) {
self.dateOfBirth = textField.text ;
}

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* totalString = [NSString stringWithFormat:@"%@%@",textField.text,string];

// if it's the phone number textfield format it.
if(textField.tag == 10 ) {
if (range.length == 1) {
// Delete button was hit.. so tell the method to delete the last char.
textField.text = [self formatPhoneNumber:totalString deleteLastChar:YES];
} else {
textField.text = [self formatPhoneNumber:totalString deleteLastChar:NO ];
}
return false;
}

return YES;
NSLog(@"Testing should change character in range");
}

-(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar {

if(simpleNumber.length == 0) return @"";
// use regex to remove non-digits(including spaces) so we are left with just the numbers
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\s-\\(\\)]" options:NSRegularExpressionCaseInsensitive error:&error];
simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:@""];

// check if the number is to long
if(simpleNumber.length>10) {
// remove last extra chars.
simpleNumber = [simpleNumber substringToIndex:10];
}

if(deleteLastChar) {
// should we delete the last digit?
simpleNumber = [simpleNumber substringToIndex:[simpleNumber length] - 1];
}

// 123 456 7890
// format the number.. if it's less then 7 digits.. then use this regex.
if(simpleNumber.length<7)
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d+)"
withString:@"($1) $2"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];

else // else do this one..
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d{3})(\\d+)"
withString:@"($1) $2-$3"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];

if (simpleNumber.length == 10 && deleteLastChar == NO) { [self resignFirstResponder];}

return simpleNumber;
NSLog(@"Testing format phone number");
}

#pragma mark - TextField
-(UITextField*) makeTextField: (NSString*)text
placeholder: (NSString*)placeholder {
UITextField *tf = [[UITextField alloc] init];
tf.placeholder = placeholder;
tf.text = text ;
tf.autocorrectionType = UITextAutocorrectionTypeNo ;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
tf.adjustsFontSizeToFitWidth = YES;
tf.returnKeyType = UIReturnKeyDone;
tf.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
return tf ;
}

最佳答案

您使用的方法:

-(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar

返回一个 NSString 对象。在您的情况下,您正确调用了该方法,但没有将 Returned NSString 对象设置为任何内容。它只是卡在那里。您需要将phoneFieldTextField 设置为格式化文本,如下所示:

phoneFieldTextField.text = [self formatPhoneNumber:phoneFieldTextField.text deleteLastChar:YES];

注意 - 如果您想了解有关返回方法的更多信息,请阅读以下内容:

如果您注意到大多数方法都是void类型。当你看到这样的方法时你就知道了:

- (void)someMethod {
int x = 10;
}

void 的意思是 someMethod 不会向您返回任何内容。它只是执行方法内的代码。现在返回对象或其他数据类型的方法如下所示:

- (int)returnSomething {
int x = 10;
return x;
}

您会注意到的第一件事是返回类型不再是 void,而是 int。这意味着该方法将返回整数类型。在这种情况下,代码将执行并返回 x 的值。

这只是返回方法主题的开始,但希望它能让您更清楚一些。

关于iphone - 向单元格中的 uitextfield 添加方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16571045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com