iPhone - UITextField not set to editable programmatically when using modal dialog?
I am kinda new with iPhone development and I'm having a bit of trouble with an apparently very simple thing.
- (IBAction) addButtonPressed:(id) sender {
AddDrinkViewController *addDrinkVC = [[AddDrinkViewController alloc] initWithNibName:@"DrinkDetailView" bundle:nil];
UINavigationController *addNavCon = [[UINavig开发者_高级运维ationController alloc] initWithRootViewController:addDrinkVC];
//the textview is not becoming editable here, even thou i'm setting the property to true.
addDrinkVC.ingredientsTextView.editable = YES;
[self presentModalViewController:addNavCon animated:YES];
[addDrinkVC release];
[addNavCon release];
}
Basically, the [ + ] button is tapped on a navigation controller and a modal view appears.
The view that appears is extended from a different view which originally has the textfields and textviews disabled from the nib itself.
I am trying to make the textviews and textfields editable when the [ + ] is tapped, but so far, I haven't had any luck.
I know it seems like a VERY simple thing to do, but I've been looking online and haven't been able to find an answer... maybe I'm missing something very basic on the behavior of modal views?
Thanks a lot in advance.
Two things:
Assuming the Text View was added to the View Controller using Interface Builder, it is possible that you forgot to Wire up the View in IB to the IBOutlet in your code.
To test this you could add a quick assertion:
NSAssert(addDrinkVC.ingredientsTextView,@"ingredientsTextView in AddDrinkViewController is nil. Did I forget to wire it?")
addDrinkVC.ingredientsTextView.editable = YES;
If the assertion fails, your program will crash and you will see that message on the console.
Secondly, I would move the logic that sets the editable property into the view controller itself (In your case the AddDrinkViewController class)
-(void) viewWillAppear:(BOOL) animated {
[super viewWillAppear:animated];
NSAssert(addDrinkVC.ingredientsTextView,@"ingredientsTextView in AddDrinkViewController is nil. Did I forget to wire it?")
addDrinkVC.ingredientsTextView.editable = YES;
}
-or-
-(void) viewDidLoad {
NSAssert(addDrinkVC.ingredientsTextView,@"ingredientsTextView in AddDrinkViewController is nil. Did I forget to wire it?")
addDrinkVC.ingredientsTextView.editable = YES;
}
Your problem is simple one.
When you are setting the property, that time nib is not loaded and all outlets are in freezed state so the property will not set to the text view. So you just place that part of code in viewDidLoad method of the AddDrinkViewController class.
-(void) viewDidLoad
{
[super viewDidLoad];
addDrinkVC.ingredientsTextView.editable = YES;
}
精彩评论