本文介绍了在视图中设置 searchBar.text 确实加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从视图 1 推送到视图 2 时,我正在尝试设置我的 searchBar.text.我成功地将一个字符串传递到视图 2 中,但是我想将该字符串设置为 searchBar.text 来过滤我的表格.到目前为止,这是我的代码,但似乎不起作用.

I'm trying to set my searchBar.text when I push from view 1 too view 2. I am passing a string into view 2 successfully however I want set the string as searchBar.text to filter my table. This is my code so far but doesn't seem to be working.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"Places", nil);
    self.managedObjectContext = [self getContext];
    UIBarButtonItem * sortButton = [[UIBarButtonItem alloc] initWithTitle:@"Sort" style:UIBarButtonItemStyleBordered target:self action:@selector(sort)];

    self.navigationItem.rightBarButtonItem = sortButton;
    [sortButton release];

    //HIT WS TO SEE LAST UPDATED AND COMPARE TO STORED LAST UPDATED DATE
    //IF WS HAS NEWER DATE,BLOW CD AWAY AND CALL getRSSDAta

    self.searchBar.delegate = self;
    self.title = @"Results";
    self.navigationController.navigationBarHidden = NO;

    __tableView.delegate = self;
    __tableView.dataSource = self;
    searchedData = [[NSMutableArray alloc]init];
    tableData = [[NSMutableArray alloc]init];
    [tableData addObjectsFromArray:_PlaceArray];//on launch it should display all the records 

    [recurringArray initWithObjects:@"Alphabetical",@"Rating Low to High", @"Rating Hight to Low", nil];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background_texture.png"]];

    self.view.backgroundColor = background;

    [background release];


    recurringDate = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 190, 320, 200)];
    recurringDate.showsSelectionIndicator = YES;
    recurringDate.delegate = self;
    [recurringDate selectRow:0 inComponent:0 animated:YES];
    [self.view addSubview:recurringDate];

    recurringDate.hidden = YES;

    NSLog(@"search text is %@", self.search);

    searchBar.text = self.search;
    [self searchBarSearchButtonClicked:searchBar];

}

- (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];
}

推荐答案

可能听起来很傻,但请确保您在 IB 中将其连接起来,我在处理一些代码时忘记这样做

May sound silly but make sure you wired it up in IB, I have forgotten to do this when I am jamming through some code

这篇关于在视图中设置 searchBar.text 确实加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 04:40