当文本字段为空时禁用按钮

发布于 2024-11-29 12:33:23 字数 1615 浏览 0 评论 0原文

你好,我是一名编程初学者,多年来一直坚持这项任务,但似乎一事无成。

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

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

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

问题是“submitButton”出现错误,需要用什么来代替它? 我尝试将按钮“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];
}

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

提前致谢

hi im a beginner to programming and have been stuck on this task for ages and seem to be getting nowhere.

basically i have several textfields that generates the input information on a different page when the user presses a button. i would like the button to be disabled until all text fields are filled with information.

so far i have this:

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

the problem is the 'submitButton' is coming up with an error, what needs to go in its place?
i tried to put my button 'bookbutton'in it instead but its not working.

this is my function for the 'generate' button

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

if anybody knows how i can make it work or what i need to add please help.

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

×眷恋的温暖 2024-12-06 12:33:23

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

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

- (IBAction)editingChanged;

连接所有的 Outlet 并将 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];
}

Make an Outlet for every UITextField and create an IBAction in your .h:

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

- (IBAction)editingChanged;

Connect all the outlets and connect the IBAction to every textfield with editingChanged:

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

Note that you can also use [textfield.text isEqualToString:@""] and put a ! in front of it (!means 'not') to recognize the empty textField, and say 'if the textField is empty do...'

And:

- (void)viewDidLoad {
    [super viewDidLoad];
    [button setEnabled:NO];
}
咆哮 2024-12-06 12:33:23

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

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

If you have a button and you want to enable it only if there is a text in a textfield or textview, implement the follow method

- (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;
}
英雄似剑 2024-12-06 12:33:23

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

是使用(扩展)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
}

Few solutions are available on these posts as well

Key here is using (extending) UITextFieldDelegate class and it's associated function

//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
}
つ低調成傷 2024-12-06 12:33:23

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

您还可以执行此操作来检查长度

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

You should use" textfield should change chracters in range" (see apple docs for uitextviewdelegate). because it updates as users type information." Textfield did end editing" only fires when the users finishes editing a textfield or hits enter. In most cases, someone will fill in the last field and leave the cursor sitting there and expect the button to become enabled...but that wont happen using "textfield did end editing" just because they entered input.

You can also do this to check the length

if([self.textfiele.text isEqualToString:@""])
岁月打碎记忆 2024-12-06 12:33:23

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

@property (nonatomic, keep) 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;
}

In addition to having IBOutlets for each UITextField, it might be helpful to have an IBOutletCollection declared that contains all 3 text fields in an array.

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

Then in your implementation just @synthesize textFields and you can quickly loop the text fields it contains, checking for text.

- (IBAction)editingChanged
{
    BOOL buttonShouldBeEnabled = YES;

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

    button.enabled = buttonShouldBeEnabled;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文