2011-08-15 50 views
5

嗨,我是一个初学者编程,并且一直被困在这个任务很长时间,似乎没有任何地方。当textfields为空时禁用按钮

基本上,我有几个文本域,当用户按下按钮时在不同的页面上生成输入信息。我希望该按钮被禁用,直到所有文本字段都充满了信息。

到目前为止,我有这样的:

- (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]; 
} 

如果有人知道我怎么可以让它工作或我需要添加,请帮助什么。

在此先感谢

回答

25

让每一个的UITextField的出口,并创建一个IBAction为你的.h:

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

- (IBAction)editingChanged; 

连接所有插座和连接IBAction为与editingChanged每一个文本框:

- (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]; 
} 
+0

谢谢你为我提供这个我现在会尝试 – Ayub

+0

好!感谢这个答案。如果使用UITextView,请尝试[this](http://stackoverflow.com/a/35674334/3411787)。 –

1

您应该使用“文本框应该改变chracters位于范围内”(见uitextviewdelegate苹果文档)。因为它会更新为用户输入的信息。“Textfield进行了结尾编辑”仅在用户完成编辑文本字段或输入时触发。在大多数情况下,有人会填写最后一个字段,并将光标放在那里,并期望按钮被启用......但这不会发生,只是因为他们输入了输入,所以使用“textfield did end editing”。

你也可以做到这一点,检查长度

if([self.textfiele.text isEqualToString:@""]) 
+0

如果你有时间,你能举个例子吗?我一直试图阅读无处不在,并尝试了许多不同的例子,但似乎无法得到它的工作 – Ayub

1

另外为每个UITextField使用IBOutlets,将IBOutletCollection声明为包含数组中的所有3个文本字段可能会有帮助。

@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; 
} 
+0

与Maxners答案相比,这有什么好处? – Ayub

+0

顺便说一句,你知道我怎么能在另一个xib上生成2个textfields,当我点击生成按钮时?我的第一篇文章显示了如何在其他xib上生成所有文本字段,但是 说我只想输出textfbookplace n textfbookyear在这个上面,你知道我该怎么做吗?我试图用格式创建另一个nsstring,它似乎没有工作。 – Ayub

6

如果你有一个按钮,你希望只启用它,如果有一个文本框或TextView的文本,落实后续方法

- (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; 
} 
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 
} 
相关问题