当文本字段为空时禁用按钮
你好,我是一名编程初学者,多年来一直坚持这项任务,但似乎一事无成。
基本上我有几个文本字段,当用户按下按钮时,它们会在不同的页面上生成输入信息。我希望禁用该按钮,直到所有文本字段都填满信息。
到目前为止,我有这样的问题:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为每个 UITextField 创建一个 Outlet 并在 .h 中创建一个 IBAction:
连接所有的 Outlet 并将 IBAction 连接到每个带有 EditingChanged 的文本字段:
请注意,您还可以使用
[textfield.text isEqualToString:@""] 并在其前面放置一个
!
(!
表示“不”)以识别空文本字段,并说“如果文本字段为空,则...”和:
Make an Outlet for every UITextField and create an IBAction in your .h:
Connect all the outlets and connect the IBAction to every textfield with editingChanged:
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:
如果您有一个按钮,并且只想在文本字段或文本视图中有文本时启用它,请实现 follow 方法
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
这些帖子上也提供了很少的解决方案
是使用(扩展)UITextFieldDelegate 类及其关联函数
Few solutions are available on these posts as well
Key here is using (extending) UITextFieldDelegate class and it's associated function
您应该使用“文本字段应该更改范围内的字符”(有关 uitextviewdelegate,请参阅苹果文档)。因为它会随着用户输入信息而更新。“文本字段确实结束编辑”仅在用户完成编辑文本字段或按 Enter 时触发。在大多数情况下,有人会填写最后一个字段并将光标留在那里并期望按钮被启用......但是使用“文本字段确实结束编辑”不会发生这种情况,仅仅因为他们输入了输入。
您还可以执行此操作来检查长度
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
除了为每个 UITextField 提供 IBOutlet 之外,声明一个包含数组中所有 3 个文本字段的 IBOutletCollection 可能会有所帮助。
@property (nonatomic, keep) IBOutletCollection(UITextField) NSArray *textFields;
然后在您的实现中只需
@synthesize
textFields
即可快速循环它包含的文本字段,检查文本。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.