So I have a UITableView in my view controller (as well as many different things) and I wanted to add a search option, so UISearchBar will be probably the best way to do it.
Is it possible to add a search function in my existing UITableView or do I have to rewrite all my view controller?
Implement searchbar delegate method
UISearchBarDelegate
SearchDisplayController Apple Sample code iOS9
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
// called only once
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[searchBar setPlaceholder:#"Search your languages"];
// called twice every time
[searchBar setShowsCancelButton:NO animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setPlaceholder:#"Select languages"];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length > 0) {
//write your searching code and reload based on search result
//[self searchproduct:searchText];
}
else
{
//arrayLanguage=[searchResultArray mutableCopy];
//reload with original data array when no searching
}
if (searchText.length == 0) {
//when no search reload with original data array
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[_searchBar resignFirstResponder];
}
Thanks
Related
I am using the iPad master / detail project template and I'm trying to update some UILabels in the detailViewController when the app is first run.
Here is My Code :
(void)setObject:(id)newObject
{
if (_object != newObject) {
[_object release];
_object = [newObject retain];
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
[self updateDetails];
}
- (void) updateDetails
{
NSLog(#"Details = %#", self.details);
NSLog (#"detailLabel %#", self.detailLabel);
self.detailLabel.text = [self.details objectForKey:#"aKey"];
}
- (IBAction)refresh:(UIBarButtonItem *)sender {
[self updateDetails];
}
setEvent is called from the Master View Controller's viewDidLoad method as selecting it's tableview's 1st row as a default.
if (![self.tableView indexPathForSelectedRow])
{
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
[self.detailViewController setObject:[self.sortedObjects objectAtIndex:0]];
}
When I run the code the detailLabel is not changed and the NSLog says self.detailLabel is (null). If 'refresh' is called later from a button click detailLabel is not null and updates correctly. How can I do this to make sure the detailLabel has been created and is not (null)?
The IBOutlet objects will be created at the time of viewDidLoad() of that object(detail view, but not master view).
call the [self updateDetails]; in viewdidLoad() method of detail view controller to avoid this problem.
I have a UITextField, and when pushed a UIPickerView comes up to choose a value. How do I get the UIPickerView to dismiss once a value is chosen. Someone in another thread told me to resignFirstResponder the textfield, but my code isn't working. Any ideas? NOTE: I have two text fields and UI Pickers, that's why I have the 'if' 'else' statement.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
[agePickerView removeFromSuperview];
return YES;
}
else
{
[relationshipTextField resignFirstResponder];
[relationshipPickerView removeFromSuperview];
return YES;
}
}
Try the following code which may solve your problem.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
}
else
{
[relationshipTextField resignFirstResponder];
}
[pickerView removeFromSuperview];
return YES;
}
or resign your keyboard on following method of UIPickerView.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Please try resigning the UITextField in UIPickerViewDelegate method as follows.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self.ageTextField resignFirstResponder];
pickerView.hidden = YES; //Show the pickerview again when you neeed it
}
Implement the UIPickerViewDelegate protocol and implement the following method:
- (void) pickerView: (UIPickerView*) pickerView
didSelectRow: (NSInteger) row
inComponent: (NSInteger) component
{
[self.ageTextField resignFirstResponder];
}
I have a UITextField in my first view that accepts search criteria however my search box is in my second view. The idea is to pass the textfield data that the user enters into the search box that filters a table view in the second view controller. I have tried setting the secondviewcontroller.searchText = self.search.text however it doesn't seem to be updating the search box in the second view controller with the new data. My code is as follows:
First View.m
- (IBAction)search:(id)sender
{
PlaceList *placelist = [[PlaceList alloc] initWithNibName:#"PlaceList" bundle:nil];
NSLog(#"%#", search.text);
placelist.searchBar.text = search.text;
[self.navigationController pushViewController:placelist animated:YES];
[placelist release];
}
PlaceList.m
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar’s cancel button while in edit mode
useSearchData = YES;
self.searchBar.showsCancelButton = YES;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
self.searchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText isEqualToString:#""] && [__searchBar.text isEqualToString:#""]){ //if nothing is in the search bar show normal table
useSearchData = NO;
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
return;
}
else
{
searchText = __searchBar.text;
useSearchData=YES;
NSPredicate * p = [NSPredicate predicateWithFormat:#"name contains[cd] %#",searchText]; //comparing stored locations to searchText
self.searchResults = [CoreDataBasicService fetchResultsForEnity:#"Place" WithPredicate:p andSortDiscriptor:#"name" Ascending:YES];
[self.tableView reloadData];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
useSearchData = NO;
[self.searchResults removeAllObjects];
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)_searchBar
{
[searchBar resignFirstResponder];
}
When the search IBAction is pressed I want to push to the placeList view and update the search with the text the user entered in the textfield on the first view controller.
Thanks in advance.
Try initializing PlaceList(VC) with the search Text, saving it to an instance variable. Then, in your PlaceList(VC)'s ViewDidLoad Method, set the text there
I have an NSView and basically, even when my mouse doesn't leave the defined frame, just moves within it, mouseExited function is called. Is this how is it suppose to be or am I doing something wrong? There are several subviews of this NSView and it's custom and here's the code for it:
- (id)initWithDelegate:(id)del {
if (self = [super init]) {
[del retain];
delegate = del;
}
return self;
}
- (void)dealloc {
[delegate release];
[super dealloc];
}
- (void)viewDidMoveToWindow {
[self addTrackingRect:[self bounds]
owner:self
userData:nil
assumeInside:NO];
}
- (void)mouseEntered:(NSEvent *)theEvent {
[delegate mouseEntered];
}
- (void)mouseExited:(NSEvent *)theEvent {
NSLog(#"mouse exited");
[delegate mouseExited];
}
- (void)mouseDown:(NSEvent *)theEvent {
[delegate mouseDown];
}
- (NSView *)hitTest:(NSPoint)aPoint {
return self;
}
Thanks.
I figuered it out. After adding a tracking area, i was changing the frame of my view so I needed to recalculate the tracking area. Found this method that will automatically be called whenever tracking area needs to be updated:
- (void)updateTrackingAreas {
Just simply recalculate your tracking area here.
Im getting a little trouble here. I have a UISearchBar that searchs for me some content, that are included on UITableView. Perfect.
When my view loads, i set a 110 height to my table view row. Nice. When my view appears, it comes with 110 px. of height. Nice.
But when i click on UISearchBar, my rows loses the height setted.
I dont know what can be.
Any help, thanks!
- (void)viewDidLoad {
[super viewDidLoad];
theTableView.rowHeight = 110;
theTableView.backgroundColor = [UIColor clearColor];
}
And the search code:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// All the actions goes here, like parsing xml.
[self.tableData removeAllObjects];
[theTableView reloadData];
}
Thanks!