The content of this lecture is very simple:
1. First, create a Single View Application, open MainStoryboard_iphone.storyboard, and put two lable and two TextFiled:
2. Start to write the code:
ViewController. h:
# Import <UIKit/UIKit. h>
@ Interface ViewController: UIViewController {
UITextField * nameField;
UITextField * numberField;
}
@ Property (nonatomic, retain) IBOutlet UITextField * nameField;
@ Property (nonatomic, retain) IBOutlet UITextField * numberField;
-(IBAction) backgroundTap :( id) sender;
-(IBAction) textFiledReturnEditing :( id) sender;
@ End
# Import <UIKit/UIKit. h> @ interface ViewController: UIViewController {UITextField * nameField; UITextField * numberField;} @ property (nonatomic, retain) IBOutlet UITextField * nameField; @ property (nonatomic, retain) IBOutlet UITextField * numberField;-(IBAction) backgroundTap :( id) sender;-(IBAction) textFiledReturnEditing :( id) sender; @ end
ViewController. m:
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
@ Synthesize nameField;
@ Synthesize numberField;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(Void) viewDidUnload
{
[Super viewDidUnload];
// Release any retained subviews of the main view.
}
// Events triggered when a blank view is clicked
-(IBAction) backgroundTap :( id) sender {
[NameField resignFirstResponder]; // The notification text loses the first responder status.
[NumberField resignFirstResponder];
}
// The Event www.2cto.com triggered when you click return
-(IBAction) textFiledReturnEditing :( id) sender {
[Sender resignFirstResponder];
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
If ([[UIDevice currentDevice] userInterfaceIdiom] = UIUserInterfaceIdiomPhone ){
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
} Else {
Return YES;
}
}
@ End
# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController @ synthesize nameField; @ synthesize numberField;-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} -(void) viewDidUnload {[super viewDidUnload]; // Release any retained subviews of the main view .} // The event triggered when a blank view is clicked-(IBAction) backgroundTap :( Id) sender {[nameField resignFirstResponder]; // notification text loss of the first responder status [numberField resignFirstResponder];} // event producer triggered when you click return-(IBAction) textFiledReturnEditing :( id) sender {[sender entity];}-(BOOL) entity :( UIInterfaceOrientation) interfaceOrientation {if ([[UIDevice currentDevice] userInterfaceIdiom] = entity) {return (interfaceOrientati On! = UIInterfaceOrientationPortraitUpsideDown);} else {return YES; }}@ end
3. Connect the operation and the output port.
:
Set the category of the background view to UIControl, so that we can process the screen event and connect the touch down output of the Control to the backgroundTap event, because clicking the soft keyboard triggers did end on exit, we connect the two textFiled did end on exit outputs to the textFiledReturnEditing event. Of course, do not forget to connect the output of the two textFiled controls with the corresponding control interfaces of ViewController.
4. Run the program to see the effect:
When you click textFiled:
When you click return or the interface is blank:
From Henry Morgan's column