Na 3 jaar iPhone gebruiker te zijn kan ik toch best snel sms-en op dat ding. Maar de haat-liefde verhouding begon natuurlijk wel slecht. Ook in het programmeer werk hadden wij een moeilijke start. Maar uiteindelijk kun je best leuke dingen doen met het toetsenbord. Ik laat hier wat code samples zien die ik links en rechts op het web vond, die mij erg hebben geholpen. Het is niet hapklaar gepresenteerd, excuses daarvoor.
3 samples laat ik zien:
- Hoe gebruik je een “tekstfield delegate” (ik snap nog steeds niet goed wat dat is).
- Hoe kun je je schermen ophogen als het toetsenbord de invoervelden verbergd.
- Bonus: hoe kun je, inplaats van een toetsenbord, een picker te voorschijn toveren.
Allereerst de credits:
Dan de Samples.
Sample 1 is een beetje warrig. Maar hier staat wel hoe je zo’n UITekstfield delegate in zowel de .h als de .m file insteld. In de boeken die ik heb staat vaak alleen de methode met de Interface Builder. De truukjes echter, staan juist vaak alleen in de “delegate”-modus.
A delegate can be any class that implements the required functions. Typically you’ll make your viewController the delegate for something, but it can be anything.
You don’t subclass UITextField for instance. You set another object as it’s delegate and declare that object as implementing the UITextFieldDelegate.
In your header file:
@interface LoginViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *userNameField;
IBOutlet UITextField *passwordField;}
Then in your source file implement whatever methods of the UITextFieldDelegate that you are interested in and they will be called when those events are occur on the textField.
- (void)viewDidLoad
{
userNameField.delegate = self;
passwordField.delegate = self;
}- (void)textFieldDidBeginEditing
UITextField *)textField
{
[self setViewMovedUp:50];
}
Sample 2 is zeer uitgebreid. Waarschijnlijk staat er meer in dan je nodig hebt. Maar het staat er zeer duidelijk en makkelijk.
For showing the textfields without being hidden by the keyboard, the standard way is to move up/down the view having textfields whenever the keyboard is shown
here is a sample code
-(void)textFieldDidBeginEditing:(UITextField *)sender { if ([sender isEqual:_textField]) { //move the main view, so that the keyboard does not hide it. if (self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } } } //method to move the view up/down whenever the keyboard is shown/dismissed -(void)setViewMovedUp:(BOOL)movedUp { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; // if you want to slide up the view CGRect rect = self.view.frame; if (movedUp) { // 1. move the view's origin up so that the text field that will be hidden come above the keyboard // 2. increase the size of the view so that the area behind the keyboard is covered up. rect.origin.y -= kOFFSET_FOR_KEYBOARD; rect.size.height += kOFFSET_FOR_KEYBOARD; } else { // revert back to the normal state. rect.origin.y += kOFFSET_FOR_KEYBOARD; rect.size.height -= kOFFSET_FOR_KEYBOARD; } self.view.frame = rect; [UIView commitAnimations]; } - (void)keyboardWillShow:(NSNotification *)notif { //keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately if ([_textField isFirstResponder] && self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (![_textField isFirstResponder] && self.view.frame.origin.y < 0) { [self setViewMovedUp:NO]; } } - (void)viewWillAppear:(BOOL)animated { // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window]; } - (void)viewWillDisappear:(BOOL)animated { // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; }define kOFFSET_FOR_KEYBOARD to a value as needed. like
#define kOFFSET_FOR_KEYBOARD 60.0Hope this helps
Sample 3 is een bonus. Zonder Sample 1 en 2 lukt ie mij niet. Maar het is wel erg cool. Die Pickers zijn normaal van die ondingen op je scherm.
First, here is a screencapture showing how this looks.
Implement UITextFieldDelegate and display a “popup” containing a UIPickerView.
- (void)textFieldDidEndEditing:(UITextField *)textField { UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 244, 320, 270)]; picker.delegate = self; picker.dataSource = self; [self.view addSubview:picker]; [picker release]; }When the keyboard disappears, a picker view is then visible.
If you want to take this a bit further, you can animate the UIPickerView “slide in” like the keyboard.
- (void)viewDidLoad { //picker exists in the view, but is outside visible range picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 480, 320, 270)]; picker.delegate = self; picker.dataSource = self; [self.view addSubview:picker]; [picker release]; } //animate the picker into view - (void)textFieldDidEndEditing:(UITextField *)textField { [UIView beginAnimations:@"picker" context:nil]; [UIView setAnimationDuration:0.5]; picker.transform = CGAffineTransformMakeTranslation(0,-236); [UIView commitAnimations]; } //animate the picker out of view - (void)textFieldDidBeginEditing:(UITextField *)textField { [UIView beginAnimations:@"picker" context:nil]; [UIView setAnimationDuration:0.5]; picker.transform = CGAffineTransformMakeTranslation(0,236); [UIView commitAnimations]; } //just hide the keyboard in this example - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; }
Einde Post.
UITextField *)textField