문제

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

도움이 되었습니까?

해결책

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

다른 팁

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

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
}

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:@""])

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top