我有一个ViewController,它包含我的应用程序用户填写并提交的一系列UITextFields(表单)(通过点击UIButton)。数据发布到外部数据库(MySQL),一切都很好。但是,我希望所有的UITextField都是“必需的”;换句话说,除非填写所有字段(包含值),否则“提交”按钮不应发布数据。有谁知道我会怎么做呢?请参阅下面的代码。
ViewController.h
-(IBAction)addParty:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *firstname;
@property (strong, nonatomic) IBOutlet UITextField *lastname;
@property (strong, nonatomic) IBOutlet UITextField *guestphone;
@property (strong, nonatomic) IBOutlet UITextField *guestemail;
@property (strong, nonatomic) IBOutlet UITextField *guestsize;
@end
ViewController.m
- (IBAction)addParty:(id)sender
{
NSString *strURL = [NSString stringWithFormat:@"guestfirst=%@&guestlast=%@&guestphone=%@&guestemail=%@&guestsize=%@", firstname.text, lastname.text, guestphone.text, guestemail.text, guestsize.text];
NSLog(@"%@", strURL);
NSData *postData = [strURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"postgl.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
[thankyou setHidden:NO];
}
else
{
[thankyou setHidden:YES];
NSLog(@"Connection could not be made");
}
}
答案 0 :(得分:1)
一种解决方案是将textfields的委托设置为UIViewController。
在委托方法
中实现以下内容- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
实现此功能后,您应该拥有如下方法:
- (BOOL)enableSubmitButton
{
for (UITextField *textField in @[self.firstname, self.lastname, self.guestphone, self.guestemail, self.guestsize]) {
if (textfield.text.length == 0) {
return NO;
}
}
return YES;
}
然后你应该有一个按钮组的IBOutlet,你可以在shouldChangeCharactersInRange内部调用enableSubmitButton并将按钮设置为启用,即
。@property (nonatomic, weak) IBOutlet UIButton *submitButton;
or could be UIBarButtonItem
@property (nonatomic, weak) IBOutlet UIBarButtonItem *submitButton;
self.submitButton.enabled = [self enableSubmitButton];
答案 1 :(得分:1)
对于未来的观众来说,值得注意的是,使用KVO收集运营商可以非常轻松地完成这项工作。
int system (constant char *command)
答案 2 :(得分:0)
嵌入if / else语句最终为我工作(例如,如果填写3/5字段,除非5/5完成,否则数据不会发布)。谢谢大家的帮助!
- (IBAction)addParty:(id)sender
{
for (UITextField *textFielda in @[self.firstname]) {
if (textFielda.text.length == 0) {
[pleasefinish setHidden:NO];
}
for (UITextField *textFieldb in @[self.lastname]) {
if (textFieldb.text.length == 0) {
[pleasefinish setHidden:NO];
}
for (UITextField *textFieldc in @[self.guestphone]) {
if (textFieldc.text.length == 0) {
[pleasefinish setHidden:NO];
}
for (UITextField *textFieldd in @[self.guestemail]) {
if (textFieldd.text.length == 0) {
[pleasefinish setHidden:NO];
}
for (UITextField *textFielde in @[self.guestsize]) {
if (textFielde.text.length == 0) {
[pleasefinish setHidden:NO];
}
else {
[pleasefinish setHidden:YES];
NSString *strURL = [NSString stringWithFormat:@"guestfirst=%@&guestlast=%@&guestphone=%@&guestemail=%@&guestsize=%@", firstname.text, lastname.text, guestphone.text, guestemail.text, guestsize.text];
NSLog(@"%@", strURL);
NSData *postData = [strURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"postgl.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
[thankyou setHidden:NO];
}
else
{
[thankyou setHidden:YES];
NSLog(@"Connection could not be made");
}
}
}
}
}
}
}
}
答案 3 :(得分:-1)
在发送请求之前,您可以执行嵌套的if语句,检查空字符串值。
所以如果你有3个需要的文本字段,那么3个嵌套的if语句可以解决问题。