Bradley
Bradley

Reputation: 617

Passing a string to another view

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.

Upvotes: 0

Views: 191

Answers (1)

Luke Mcneice
Luke Mcneice

Reputation: 2720

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

Upvotes: 1

Related Questions