CS 696 Mobile Application DevelopmentFall Semester, 2010Doc 7 TextFieldSep 20, 2010Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.References2Beginning iPhone 3 Development: Exploring the iPhone SDK by Jeff LaMarche, and David Mark, Chapter 4 Various Apple iOS documentationExisting Views & Controls3ButtonsText FieldsText viewSwitchSliderImage viewSearch barPickersSegment ControlToolbarAlerts & ActionsheetsWeb viewViews, Controls & Controllers4ViewsSubclass of UIViewLabel, Web view, Toolbar, etcControlsSubclass of UIControlButton, date picker, slider, text field, switchControllersSubclass of UIViewControllerUICatalog5http://developer.apple.com/library/ios/#samplecode/UICatalog/index.htmlActive, Static and Passive (Views & Controls)6ActiveUser does somethingCode executes in repsonseButtonStaticDisplays somethingUser cannot interact with itProgram can change itLabelPassiveUser canTextField Example7The connections8@interface TextAlertViewController : UIViewController { }@property (nonatomic, retain) IBOutlet UITextField *sampleField;@property (nonatomic, retain) IBOutlet UITextField *echoField;- (IBAction) donePressed;- (IBAction) editDone;@endOutletssampleFieldechoFieldActionsdonePressed editDoneActions9#import "TextAlertViewController.h"@implementation TextAlertViewController@synthesize sampleField;@synthesize echoField;- (IBAction) donePressed { [self editDone];}- (IBAction) editDone { echoField.text = sampleField.text;}How to know when user is done editing?10TextField eventsEdit Did BeginWhen field gets focusEdit ChangedWhen any change is made Characters added/removed, cursor changes positionEdit Did EndWhen focus leaves fieldTab to another fieldDid End On ExitWhen user tabs on "return" or "done" key on keyboardHow to know when user is done editing?11Hard to tell by just text field eventsProvide some other way for user to indicate doneKeyboard12When textfield gets focus keyboard appearsYour code has to hide itMultiple keyboards availableFirst Responder13UI element the user in interacting withWindow currently the focus for user eventsHiding the Keyboard14When text field is notified that should stop being first responderSend resignFirstResponder to active text fieldHiding the Keyboard15- (IBAction) donePressed { [self editDone]; [sampleField resignFirstResponder]; [echoField resignFirstResponder];}Do we have to list all text fields?16- (IBAction) donePressed { [self editDone]; UIView * firstResponder = [[self view] firstResponder]; if( [firstResponder isKindOfClass:[UITextField class]] ) [firstResponder resignFirstResponder];}UIView class has method "firstResponder"But ...firstResponder is not in public API17Apple rejects apps that use non public API methodsQuestion for thought18How does one find out about methods not in public API?New UIView Method19#import <Foundation/Foundation.h>@interface UIView (FirstResponder) - (UIView *) getFirstResponder;@end@implementation UIView (FirstResponder)- (UIView *) getFirstResponder{ if (self.isFirstResponder) { return self; } for (UIView *subView in self.subviews) { UIView *firstResponder = [subView getFirstResponder]; if (firstResponder != nil) { return firstResponder; } } return nil;}@endAdapted from http://stackoverflow.com/questions/949806/is-there-a-way-to-detect-what-uiview-is-currently-visibleLegal Solution20- (IBAction) donePressed { [self editDone]; UIView * firstResponder = [[self view] getFirstResponder]; if( [firstResponder isKindOfClass:[UITextField class]] ) [firstResponder resignFirstResponder];}- (IBAction) editDone { echoField.text = sampleField.text;}But people often just list all the text fieldsTap Background21Hides the keyboard- (IBAction) backgroundTapped { [self hideKeyboard];}- (void) hideKeyboard { UIView * firstResponder = [[self view] getFirstResponder]; if( [firstResponder isKindOfClass:[UITextField class]] ) [firstResponder resignFirstResponder];}- (IBAction) donePressed { [self editDone]; [self hideKeyboard];}- (IBAction) editDone { echoField.text = sampleField.text;}Tap Background22You have to change the class of the View to be UIControlSome Keyboard Types23UIKeyboardTypeASCIICapable NumbersNumber PadURLKeyboard Type in IB24Keyboard Type in code25- (void)viewDidLoad { [super viewDidLoad]; sampleField.keyboardType = UIKeyboardTypeDefault; sampleField.returnKeyType = UIReturnKeyGo;}Keyboard Types26 UIKeyboardTypeDefault, UIKeyboardTypeASCIICapable, UIKeyboardTypeNumbersAndPunctuation, UIKeyboardTypeURL, UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad, UIKeyboardTypeNamePhonePad, UIKeyboardTypeEmailAddress, UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapableReturn Key Types27 UIReturnKeyDefault, UIReturnKeyGo, UIReturnKeyGoogle, UIReturnKeyJoin, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeyYahoo, UIReturnKeyDone, UIReturnKeyEmergencyCall,Other Keyboard Properties28autocapitalizationTypeautocorrectionTypeenablesReturnKeyAutomaticallykeyboardAppearancereturnKeyTypesecureTextEntrySee UITextInputTraits Protocol for more detailsTextField Delegate29 – textFieldShouldBeginEditing: – textFieldDidBeginEditing: – textFieldShouldEndEditing: – textFieldDidEndEditing: – textField:shouldChangeCharactersInRange:replacementString: – textFieldShouldClear: – textFieldShouldReturn:Messages that are part of TextField editing sequenceAllows one to control text field editsController as TextField Delegate30@interface TextAlertViewController : UIViewController <UITextFieldDelegate>{}@property (nonatomic, retain) IBOutlet UITextField *sampleField;@property (nonatomic, retain) IBOutlet UITextField *echoField;- (IBAction) backgroundTapped;- (IBAction) donePressed;- (IBAction) editDone;- (IBAction)textFieldDoneEditing:(id)sender;@endMulti-line text field31http://www.hanspinckaers.com/multi-line-uitextview-similar-to-smsAPI does not support multi-line text
View Full Document