vikmalhotra
vikmalhotra

Reputation: 10071

iPhone: How to make a UITextField, UITextView uneditable when switching views

I have two views - one a table view and other a detail view in my iphone app. When a row in table is selected, the detail view is displayed.

I am using the nib for detail view for editing a record, adding a new record as well as displaying a record. The detail view has a UITextField and a UITextView which need to be made uneditable when I am just displaying the record. In the didSelectRowAtIndexPath method of tableview I tried this...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    MemoDetailViewController *memoDetailViewController = [[MemoDetailViewController alloc] initWithNibName:@"MemoDetailViewController" bundle:nil];

    memoDetailViewController.memo = [self.resultsController objectAtIndexPath:indexPath];

// making the text field and text view uneditable - DID NOT WORK???
        memoDetailViewController.memoTitleText.enabled = NO;
        memoDetailViewController.memoTextView.editable = NO;

    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:memoDetailViewController animated:YES];
    [memoDetailViewController release];
}

This does not work. The textfield and uitextview remain editable - the keyboard appears when I tap on them. What am I missing here?

Upvotes: 2

Views: 2848

Answers (6)

user1316896
user1316896

Reputation:

You should try with this:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{

}

Upvotes: 0

vishiphone
vishiphone

Reputation: 750

You also use this in both view controller in different-2 time

 -(void)viewDidAppear:(BOOL)animated 
 {
   memoDetailViewController.memoTextView.editable = NO;
 }

 -(void)viewDidDisappear:(BOOL)animated 
 {
   memoDetailViewController.memoTextView.editable = YES;
 }

Upvotes: 0

pradeepj
pradeepj

Reputation: 516

You can even disable USER INTERACTION ENABLED in XIB of particular UI Element

Upvotes: 4

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

The code is not working because the view hasn't been loaded from the XIB and hence those properties are nil and messaging to nil has zero effect. If you alter the order to pushViewController first then it should work

MemoDetailViewController *memoDetailViewController = [[MemoDetailViewController alloc] initWithNibName:@"MemoDetailViewController" bundle:nil];

memoDetailViewController.memo = [self.resultsController objectAtIndexPath:indexPath];

[self.navigationController pushViewController:memoDetailViewController animated:YES];

memoDetailViewController.memoTitleText.enabled = NO;
memoDetailViewController.memoTextView.editable = NO;

[memoDetailViewController release];

While this works, I would suggest declaring a BOOL property. Set it to YES or NO and later use its value in viewWillAppear: method to enable or disable the fields.

Upvotes: 1

Chris Gregg
Chris Gregg

Reputation: 2382

Try this in the view controller for the view that should be uneditable:

-(void)viewWillAppear:(BOOL)animated {
     memoDetailViewController.memoTextView.editable = NO;
}

-(void)viewWillDisappear:(BOOL)animated {
    memoDetailViewController.memoTextView.editable = YES;
}

Upvotes: 0

justin
justin

Reputation: 5831

Probably the easiest way to go about this is to use their respective delegate methods. They are the same, only with different names. Using UITextField as an example, you're basic approach would be this

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (someCaseWhereYouWantToEdit) {
        return YES;
    } else {
        return NO;
    }
}

This lets you specify when you want to let it continue to edit. You of course need to have the textField and textView instances set with self as their delegate as well as importing the delegate protocols in your header

Upvotes: 7

Related Questions