IPhone development-use UITextField to implement the search box

Source: Internet
Author: User

Because of the need for interface beautification, the original SearchBar control is too ugly and lazy to study the new style of the SearchBar control, so I decided to rewrite a search function control.

Creates a UITextField class object.


[Cpp] searchField = [[UITextField alloc] initWithFrame: CGRectMake (,)] autorelease];
SearchField. textColor = [UIColorcolor WithRed: 0.0/255.0 green: 103.0/255.0 blue: 155.0/255.0 alpha: 1.0];
SearchField. font = [UIFont systemFontOfSize: 16];
SearchField. backgroundColor = [UIColor blueColor];
SearchField. contentVerticalAlignment = uicontrolcontentverticalignmentcenter;
[SearchField addTarget: self action: @ selector (textFieldDidChange :) forControlEvents: UIControlEventEditingChanged]; // event when textField text changes
SearchField = [[[UITextField alloc] initWithFrame: CGRectMake (,)] autorelease];
SearchField. textColor = [UIColorcolor WithRed: 0.0/255.0 green: 103.0/255.0 blue: 155.0/255.0 alpha: 1.0];
SearchField. font = [UIFont systemFontOfSize: 16];
SearchField. backgroundColor = [UIColor blueColor];
SearchField. contentVerticalAlignment = uicontrolcontentverticalignmentcenter;
[SearchField addTarget: self action: @ selector (textFieldDidChange :) forControlEvents: UIControlEventEditingChanged]; // event when textField text changes
Set ReturnKeyType to UIRetuirKeySearch:
[Cpp] [searchField setReturnKeyType: UIReturnKeySearch];
[SearchField setReturnKeyType: UIReturnKeySearch];
Set the delegate of UITextField to self:
 
[Cpp] searchField. delegate = self;
SearchField. delegate = self;
Then write the function that responds to the event by clicking the search button:

[Cpp]-(BOOL) textFieldShouldReturn :( UITextField *) theTextField {
[TheTextField resignFirstResponder];
NSLog (@ "do something what you want ");
ReturnYES;
}
-(BOOL) textFieldShouldReturn :( UITextField *) theTextField {
[TheTextField resignFirstResponder];
NSLog (@ "do something what you want ");
ReturnYES;
} When the text content of textField changes, the event processing function
[Cpp]-(void) textFieldDidChange :( UITextField *) TextField {
NSLog (@ "textFieldDidChange ");
If (! [TextField. textisEqualToString: @ ""]) {
DelButton. hidden = NO; // copy the subfork behind the searchbar
} Else {
DelButton. hidden = YES;
}
}
-(Void) textFieldDidChange :( UITextField *) TextField {
NSLog (@ "textFieldDidChange ");
If (! [TextField. textisEqualToString: @ ""]) {
DelButton. hidden = NO; // copy the subfork behind the searchbar
} Else {
DelButton. hidden = YES;
}
}

Ha, of course I still refer to the following a high person idea: http://forums.macrumors.com/archive/index.php/t-523664.html

 

Note: This article was previously published on baidu, but it cannot match baidu's typographical style.

From the column zcl316369

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.