Skip to content

Commit

Permalink
Implemented automatic "next" buttons on keyboard to switch text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltyson committed Mar 12, 2013
1 parent 9ee481c commit 383478e
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 213 deletions.
7 changes: 1 addition & 6 deletions TPKeyboardAvoidingSample/FirstViewController.h
Expand Up @@ -10,11 +10,6 @@

@class TPKeyboardAvoidingScrollView;

@interface FirstViewController : UIViewController <UITextFieldDelegate>
@interface FirstViewController : UIViewController
@property (nonatomic, retain) IBOutlet TPKeyboardAvoidingScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UITextField *txtIggle;
@property (nonatomic, retain) IBOutlet UITextField *txtNiggle;
@property (nonatomic, retain) IBOutlet UITextField *txtOggle;
@property (nonatomic, retain) IBOutlet UITextField *txtBogle;
@property (nonatomic, retain) IBOutlet UITextField *txtSplat;
@end
45 changes: 0 additions & 45 deletions TPKeyboardAvoidingSample/FirstViewController.m
Expand Up @@ -11,12 +11,6 @@

@implementation FirstViewController
@synthesize scrollView;
@synthesize txtIggle;
@synthesize txtNiggle;
@synthesize txtOggle;
@synthesize txtBogle;
@synthesize txtSplat;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
Expand Down Expand Up @@ -50,11 +44,6 @@ - (void)didReceiveMemoryWarning
- (void)viewDidUnload
{
[self setScrollView:nil];
[self setTxtIggle:nil];
[self setTxtNiggle:nil];
[self setTxtOggle:nil];
[self setTxtBogle:nil];
[self setTxtSplat:nil];
[super viewDidUnload];

// Release any retained subviews of the main view.
Expand All @@ -65,41 +54,7 @@ - (void)viewDidUnload
- (void)dealloc
{
[scrollView release];
[txtIggle release];
[txtNiggle release];
[txtOggle release];
[txtBogle release];
[txtSplat release];
[super dealloc];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == txtIggle) {
[txtNiggle becomeFirstResponder];
}

else if (textField == txtNiggle) {
[txtOggle becomeFirstResponder];
}

else if (textField == txtOggle) {
[txtBogle becomeFirstResponder];
}

else if (textField == txtBogle) {
[txtSplat becomeFirstResponder];
}
else{
[textField resignFirstResponder];
}


return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[scrollView adjustOffsetToIdealIfNeeded];
}

@end
2 changes: 1 addition & 1 deletion TPKeyboardAvoidingSample/SecondViewController.h
Expand Up @@ -9,5 +9,5 @@
#import <UIKit/UIKit.h>


@interface SecondViewController : UITableViewController <UITextFieldDelegate>
@interface SecondViewController : UITableViewController
@end
9 changes: 1 addition & 8 deletions TPKeyboardAvoidingSample/SecondViewController.m
Expand Up @@ -99,10 +99,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 150, 30)] autorelease];
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
textField.borderStyle = UITextBorderStyleRoundedRect;
cell.accessoryView = textField;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

cell.textLabel.text = [NSString stringWithFormat:@"Order %d", indexPath.row];
Expand All @@ -111,11 +111,4 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
return cell;
}

#pragma mark - Text field delegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

@end
140 changes: 44 additions & 96 deletions TPKeyboardAvoidingSample/en.lproj/FirstView.xib
Expand Up @@ -51,6 +51,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 30}, {280, 31}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="89122733"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
Expand Down Expand Up @@ -87,6 +88,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 281}, {280, 31}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="283243135"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
Expand Down Expand Up @@ -114,14 +116,15 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 337}, {280, 37}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="364961226"/>
<bool key="IBUIOpaque">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<int key="IBUIContentHorizontalAlignment">0</int>
<int key="IBUIContentVerticalAlignment">0</int>
<int key="IBUIButtonType">1</int>
<string key="IBUINormalTitle">Do A Thing</string>
<object class="NSColor" key="IBUIHighlightedTitleColor">
<object class="NSColor" key="IBUIHighlightedTitleColor" id="460154988">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MQA</bytes>
</object>
Expand Down Expand Up @@ -150,6 +153,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 94}, {280, 31}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="141163670"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
Expand Down Expand Up @@ -177,6 +181,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 156}, {280, 31}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="331828899"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
Expand Down Expand Up @@ -204,6 +209,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 218}, {280, 31}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="325075817"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
Expand Down Expand Up @@ -231,7 +237,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 400}, {280, 31}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSNextKeyView"/>
<reference key="NSWindow"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
Expand All @@ -258,6 +264,7 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 500}, {280, 31}}</string>
<reference key="NSSuperview" ref="206876602"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="1024068394"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
Expand All @@ -283,6 +290,7 @@
</object>
<string key="NSFrameSize">{320, 411}</string>
<reference key="NSSuperview" ref="191373211"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="696973480"/>
<bool key="IBUIClipsSubviews">YES</bool>
<bool key="IBUIMultipleTouchEnabled">YES</bool>
Expand All @@ -291,12 +299,9 @@
</object>
<string key="NSFrame">{{0, 20}, {320, 411}}</string>
<reference key="NSSuperview"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="206876602"/>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MCAwIDAgMAA</bytes>
<string key="IBUIColorCocoaTouchKeyPath">groupTableViewBackgroundColor</string>
</object>
<reference key="IBUIBackgroundColor" ref="460154988"/>
<object class="IBUISimulatedStatusBarMetrics" key="IBUISimulatedStatusBarMetrics"/>
<object class="IBUISimulatedTabBarMetrics" key="IBUISimulatedBottomBarMetrics"/>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
Expand All @@ -321,94 +326,6 @@
</object>
<int key="connectionID">21</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">txtIggle</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="696973480"/>
</object>
<int key="connectionID">22</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">txtNiggle</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="89122733"/>
</object>
<int key="connectionID">23</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">txtOggle</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="141163670"/>
</object>
<int key="connectionID">24</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">txtBogle</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="1024068394"/>
</object>
<int key="connectionID">25</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">txtSplat</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="325075817"/>
</object>
<int key="connectionID">26</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="696973480"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">13</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="325075817"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">14</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="89122733"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">16</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="141163670"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">18</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="1024068394"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">20</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">delegate</string>
<reference key="source" ref="364961226"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">28</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
Expand Down Expand Up @@ -552,7 +469,38 @@
<nil key="sourceID"/>
<int key="maxID">29</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes"/>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBPartialClassDescription">
<string key="className">FirstViewController</string>
<string key="superclassName">UIViewController</string>
<object class="NSMutableDictionary" key="outlets">
<string key="NS.key.0">scrollView</string>
<string key="NS.object.0">TPKeyboardAvoidingScrollView</string>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
<string key="NS.key.0">scrollView</string>
<object class="IBToOneOutletInfo" key="NS.object.0">
<string key="name">scrollView</string>
<string key="candidateClassName">TPKeyboardAvoidingScrollView</string>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/FirstViewController.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
<string key="className">TPKeyboardAvoidingScrollView</string>
<string key="superclassName">UIScrollView</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/TPKeyboardAvoidingScrollView.h</string>
</object>
</object>
</object>
</object>
<int key="IBDocument.localizationMode">0</int>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
Expand Down
13 changes: 3 additions & 10 deletions TPKeyboardAvoidingScrollView.h
Expand Up @@ -7,14 +7,7 @@

#import <UIKit/UIKit.h>

@interface TPKeyboardAvoidingScrollView : UIScrollView {
UIEdgeInsets _priorInset;
BOOL _priorInsetSaved;
BOOL _keyboardVisible;
CGRect _keyboardRect;
CGSize _originalContentSize;
CGPoint _originalContentOffset;
}

- (void)adjustOffsetToIdealIfNeeded;
@interface TPKeyboardAvoidingScrollView : UIScrollView
- (BOOL)focusNextTextField;
- (void)scrollToActiveTextField;
@end

0 comments on commit 383478e

Please sign in to comment.