Dynamically modifying views
I am implementing a custom keyboard in my app. I want the keyboard to be applicable to multiple 'types' of textFields (all numeric, but some that can have negative values vs strictly positive, some whole-numbers vs some decimal values). I want to accomplish this by hiding one or two buttons depending on the 'type' of textField.
I have built the keyboard, and can assign it as the inputView of one of my textFields (xValue). I have also written a method in my keyboard's ViewController that takes integers as inputs and should modify the keyboard buttons. At the moment, this method works in the viewDidLoad method of my ViewController, but I want to call this from the ViewController class where the keyboard is initialised. When I do try to call it, the hidden properties aren't changing.
In the viewDidLoad of the ViewController that contains my text field I have put the following:
DCKeyboard *dckvc = [[DCKeyboard alloc] initWithNibName:@"DCKeyboard" bundle:nil];
[dckvc modifyKeyboardType:0 doneNextValue:0]; // Has no effect
xValue.inputView = dckvc.view;
and in DCKeyboard.m I have defined:
@synthesize doneNextButton;
@synthesize decimalButton;
@synthesize posNegButton;
-(void) modifyKeyboardType:(int)buttonTypeNumber doneNextValue:(int)doneNextNumber {
switch (buttonTypeNumber) {
case 0: // Case 0: All buttons available
decimalButton.hidden = NO;
posNegButton.hidden = NO;
break;
case 1: // Case 1: No decimal button
decimalButton.hidden = YES;
posNegButton.hidden = NO;
break;
case 2: // Case 2: No positive/negative button
decimalButton.hidden = NO;
posNegButton.hidden = YES;
break;
case 3: // Case 3: No decimal or positive/negative button
decimalButton.hidden = YES;
posNegButton.hidden = YES;
break;
default:
break;
}
switch (doneNextNumber) {
case 0: // Case 0: Done
[doneNextButton setImage:[UIImage imageNamed:@"DoneButton.png"] forState:UIControlStateNormal];
[doneNextButton setImage:[UIImage imageNamed:@"DoneButtonDown.png"] forState:UIControlStateHighlighted];
break;
case 1: // Case 1: Next
[doneNextButton setImage:[UIImage imageNamed:@"NextButton.png"] forState:UIControlStateNormal];
[doneNextButton setImage:[UIImage imageNamed:@"NextButtonDown.png"] forState:UIControlStateHighlighted];
break;
default:
break;
}
}
When I try to call this as above, the
As mentioned above, if I call this from DCKeyboard's viewDidLoad method as follows, then it works - so I'm confide开发者_StackOverflow中文版nt that I've set everything up correctly and the code works. I must just be missing a fundamental concept!
- (void)viewDidLoad {
[super viewDidLoad];
[self modifyKeyboardType:3 doneNextValue:1]; // This works
}
The underlying views may not be created until actually needed. In this case, that's when you access the property dckvc.view
. As such, when you call your modifyKeyboardType:doneNextValue:
method, none of the views actually exist to be hidden.
You could either defer this call until you're sure the views are loaded, or cache the values you pass in for the keyboard type, using them in a viewDidLoad
override to update the keyboard.
That's simply because your IBOutlets
are not pointing to anything unless the view is not loaded on the window. So if you try to modify any of there properties then nothing will happen.
This happens because your xib files is archived and none of the objects are accessible until they are presented on the screen. So you can call that method of yours in viewDidLoad which is first method that is called after the view is loaded on the screen or you can call that method after calling the methods for presenting your view on the screen like presentModalViewController:animated:
or pushViewController:animated:
精彩评论