5

嗨,我是编程的初学者,多年来一直坚持这项任务,似乎无处可去。

基本上我有几个文本字段,当用户按下按钮时,它们会在不同的页面上生成输入信息。我希望在所有文本字段都填满信息之前禁用该按钮。

到目前为止,我有这个:

    - (void)textFieldDidEndEditing:(UITextField *)textField
{
    // make sure all fields are have something in them

if ((textfbookauthor.text.length  > 0)&& (textfbookedition.text.length > 0)&& (textfbookplace.text.length > 0)&& (textfbookpublisher.text.length > 0) && (textfbookpublisher.text.length > 0) && (textfbooktitle.text.length > 0)  && (textfbookyear.text.length > 0)) {
        self.submitButton.enabled = YES;
    }
    else {
        self.submitButton.enabled = NO;
    }
}

问题是“提交按钮”出现错误,需要用什么代替它?我试图将我的按钮“bookbutton”放入其中,但它不起作用。

这是我的“生成”按钮的功能

    -(IBAction)bookbutton:(id)sender;
{
    NSString* combinedString = [NSString stringWithFormat:
                                @"%@ %@ %@.%@.%@:%@.", 
                                textfbookauthor.text,
                                textfbookyear.text,
                                textfbooktitle.text,
                                textfbookedition.text,
                                textfbookplace.text,
                                textfbookpublisher.text];
    BookGenerate*bookg = [[BookGenerate alloc] init];
    bookg.message = combinedString;
    bookg.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:bookg animated:YES];
    [BookGenerate release];
}

如果有人知道我如何使它工作或我需要添加什么,请帮助。

提前致谢

4

5 回答 5

25

为每个 UITextField 创建一个 Outlet 并在您的 .h 中创建一个 IBAction:

IBOutlet UITextField *textField1;
IBOutlet UITextField *textField2;
IBOutlet UITextField *textField3;
IBOutlet UIButton *button

- (IBAction)editingChanged;

使用editingChanged连接所有出口并将IBAction连接到每个文本字段:

 - (IBAction)editingChanged {
    if ([textfield1.text length] != 0 && [textfield2.text length] != 0 && [textfield3.text length] != 0) {
         [button setEnabled:YES];
     }
     else {
         [button setEnabled:NO];
     }
 }

请注意,您也可以使用并在其前面[textfield.text isEqualToString:@""]放一个(表示“不”)来识别空的文本字段,并说“如果文本字段为空,请...”!!

和:

- (void)viewDidLoad {
    [super viewDidLoad];
    [button setEnabled:NO];
}
于 2011-08-15T17:02:05.910 回答
6

如果您有一个按钮,并且您只想在文本字段或文本视图中有文本时启用它,请实现以下方法

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{    
    if (textView.text.length > 1 || (text.length > 0 && ![text isEqualToString:@""]))
    {
        self.button.enabled = YES;
    }
    else
    {
        self.button.enabled = NO;
    }

    return YES;
}
于 2012-12-20T16:04:45.400 回答
2

这些帖子上也提供很少的解决方案

这里的关键是使用(扩展) UITextFieldDelegate 类及其相关函数

//Need to have the ViewController extend UITextFieldDelegate for using this feature
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    // Find out what the text field will be after adding the current edit
    let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

    if !text.isEmpty{//Checking if the input field is not empty
        button.userInteractionEnabled = true //Enabling the button
    } else {
        button.userInteractionEnabled = false //Disabling the button
    }

    // Return true so the text field will be changed
    return true
}
于 2015-07-28T03:04:47.263 回答
1

您应该使用“文本字段应该更改范围内的字符”(有关 uitextviewdelegate,请参阅苹果文档)。因为它会随着用户输入信息而更新。“文本字段结束编辑”仅在用户完成编辑文本字段或按回车时触发。在大多数情况下,有人会填写最后一个字段并将光标留在原处并期望按钮启用......但使用“文本字段确实结束编辑”不会发生这种情况,因为他们输入了输入。

您也可以这样做来检查长度

if([self.textfiele.text isEqualToString:@""])
于 2011-08-15T15:17:34.750 回答
1

除了为每个 UITextField 设置 IBOutlet 之外,声明一个包含数组中所有 3 个文本字段的 IBOutletCollection 可能会有所帮助。

@property (nonatomic, retain) IBOutletCollection(UITextField) NSArray *textFields;

然后在您的实现中@synthesize textFields,您可以快速循环它包含的文本字段,检查文本。

- (IBAction)editingChanged
{
    BOOL buttonShouldBeEnabled = YES;

    for (UITextField *field in self.textFields)
        if (!field.text.length)
            buttonShouldBeEnabled = NO;

    button.enabled = buttonShouldBeEnabled;
}
于 2011-08-16T04:56:33.023 回答