Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kalle Kod
37 PointsForce user to enter text
Can I forse a user to enter a textfield to click save?
10 Answers
Cameron Armstrong
9,406 PointsOne method you can try is to have the save button disabled by default (self.yourButton.enabled = NO;) and use the UITextFieldDelegate protocol in your view controller header file to make it look something like this:
@interface YourViewController : UIViewController
<UITextFieldDelegate>
Then in your viewDidLoad method set the textfield's delegate to self:
self.yourButton.delegate = self;
And then implement this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([self.textField.text isEqualToString:@""]) {
self.button.enabled = NO;
} else {
self.button.enabled = YES;
}
return YES;
}
This code gets called as soon as you begin editing. So as soon as you begin typing, the button will get enabled. The only issue with this code is if you erase the textfield, the button will remain enabled unless you push the return button.
Cameron Armstrong
9,406 Pointsdid you set all of those textfields to delegate = self at viewDidLoad? and did you put <UITextFieldDelegate> in the .h file?
Cameron Armstrong
9,406 PointsSorry. Can someone tell me how to get line breaks working? The code I pasted looks awful.
Kalle Kod
37 PointsThanks. But it dosent work for me. :(
Cameron Armstrong
9,406 PointsDo you have the button and textfield in outlets?
Kalle Kod
37 PointsThis is my code. And I want the user to atleast fill up one of the fields
<p>
// Create PFObject with information
PFUser *profile = [PFUser currentUser];
[profile setObject:nameTextField.text forKey:@"name"];
[profile setObject:titleTextField.text forKey:@"title"];
[profile setObject:locationTextField.text forKey:@"location"];
[profile setObject:phoneTextField.text forKey:@"contactPhone"];
[profile setObject:emailTextField.text forKey:@"contactEmail"];
[profile setObject:urlTextField.text forKey:@"contactUrl"];
NSData *imageData = UIImageJPEGRepresentation(profileImageView.image, 0.8);
NSString *filename = [NSString stringWithFormat:@"%@", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:@"profileimageFile"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Uploading";
[hud show:YES];
// Upload to Parse
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[hud hide:YES];
if (!error) {
// Show success message
[self performSegueWithIdentifier:@"setupProfileText" sender:self];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Something Is Missing" message:@"Please enter all required fields" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
</p>
```
Kalle Kod
37 PointsIt is self.yourButton.delegate = self; i get error from. Any ideas?
YOL O!
57 PointsDid you get it to work?
YOL O!
57 PointsI would love to do something like that. But can get it to work either...
h
@property (strong, nonatomic) IBOutlet UIBarButtonItem *saveButton;
m
self.join.delegate = self;
if(usernameTextField.text.length==0 && emailTextField.text.length==0 && emailTextField.text.length==0) isEqualToString:@""]) {
self.join.enabled = NO;
} else {
self.join.enabled = YES;
{
[[[UIAlertView alloc] initWithTitle:@"" message:@"Please Fill Atleast One Field" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
{
if(phoneTextField.text.length==0 && emailTextField.text.length==0 && urlTextField.text.length==0)
{
[[[UIAlertView alloc] initWithTitle:@"" message:@"Please Fill Atleast One Field" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
}
else{
[self performSegueWithIdentifier:@"setupProfileText" sender:self];
}
}
}
}];
```
YOL O!
57 PointsI cant get it to work :( Cameron Armstrong