How to Add Methods to a Modal View Controller in Your iPad App

If your iPad app has a Modal view, you need to add methods to the SettingsViewController.m (implementation) file. These methods manage the iPad app's Modal view and perform any user-input actions.


It helps to know what each section of the template-provided code does, and it's especially helpful if you use # pragma mark statements to mark off each section so that you can quickly jump to the relevant section when needed. The following example code, which is part of an app called Deep Thoughts, includes these statements.


#import "SettingsViewController.h"
#import "DeepThoughtsViewController.h"
#import "Constants.h"
@implementation SettingsViewController
@synthesize delegate, wordsOfWisdom, slider ;
#pragma mark -
#pragma mark View life cycle
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
slider.value = + kMaxSpeed - ((DeepThoughtsViewController*) (self.parentViewController)).speed;
}
#pragma mark -
#pragma mark textField
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[textField setReturnKeyType:UIReturnKeyNext];
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
wordsOfWisdom = textField.text;
}
#pragma mark -
#pragma mark Controls
- (IBAction) speedChanged: (id) sender {
[delegate changeSpeed: [(UISlider *)sender value] ];
}
- (IBAction)done {
if(! [theTextField.text isEqualToString: @"" ]) wordsOfWisdom = theTextField.text;
[self.delegate settingsViewControllerDidFinish:self];
}
#pragma mark -
#pragma mark Orientation
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end

Okay, here's what the preceding code does:




  • Although the @property declaration in the header file tells the compiler that there are accessor methods, you need an @synthesize statement for a property to create those methods. The @synthesize statement tells the compiler to create accessor methods for you — one for each @property declaration.




  • The viewDidLoad method sets the background and speed for the slider (which controls the speed of words scrolling down the iPad screen).




  • The UITextFieldDelegate protocol defines the messages sent to a text field delegate as part of the sequence of editing its text. When the user performs an action that would normally start an editing session, the text field calls the textFieldShouldBeginEditing: method first to see whether editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed.




  • The text field calls the textFieldShouldReturn: method whenever the user taps the Return button on the keyboard to find out whether it should process the Return. You can use this method to implement any custom behavior when the Return button is tapped, but for your purposes, you simply return YES (which is the default), although you could return NO to ignore the Return button.




  • After saying "yes" to this and that, the real action happens with the textFieldDidEndEditing: method, which is called after the text field resigns its first responder status to tell the delegate that editing has stopped for the specified text field, so that you now have the edited wordsOfWisdom (which are the words that scroll down the iPad's screen in this app).




  • Next, you provide a speedChanged method (of type IBAction) to handle a change in speed, which uses the delegate's changeSpeed method to immediately change the speed of the animation in the view when the user changes it in the Modal view.




  • You also supply a done method that handles the possibility of a blank text field. The code assigns the text field's text to wordsOfWisdom only if the field is not theTextField.text isEqualToString: @"".












dummies

Source:http://www.dummies.com/how-to/content/how-to-add-methods-to-a-modal-view-controller-in-y.html

No comments:

Post a Comment