将字符串从UISearchBar传递到另一个viewController

时间:2013-10-18 20:47:59

标签: ios objective-c uisearchbar

我正在尝试从UISearchBar存储用户的输入,然后在另一个视图控制器中使用该字符串。这是我的代码:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    [searchBar resignFirstResponder];

    JobDetailViewController *detail =[[JobDetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    self.jobSearchString = self.searchBar.text;

    [detail searchMethod];

    NSLog(@"%@",self.jobSearchString);
}

我已经将字符串存储在我在头文件中声明的text属性中,然后我推送到另一个控制器并调用方法。这是我尝试使用存储字符串的下一个视图:

JobbyViewController *mainView = [[JobbyViewController alloc] init];

NSString *searchString = [self.searcherString stringByReplacingOccurrencesOfString:@" " withString:@"-"];

它不断出现(null)。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

@property上创建JobDetailViewController并设置

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    [searchBar resignFirstResponder];

    JobDetailViewController *detail =[[JobDetailViewController alloc] init];
    detail.searchString = self.searchBar.text
    [self.navigationController pushViewController:detail animated:YES];
    self.jobSearchString = self.searchBar.text;

    [detail searchMethod];

    NSLog(@"%@",self.jobSearchString);
}