0
votes

Question is similar to this one: Setting searchBar.text in view did load

I have my keyword from V1 passing to V2 without issues; I'm trying to set it as search bar text & search from previous segue.

String is called searchText.

I've tried

    self.searchBar.text = self.searchText;

in viewWillAppear & viewDidLoad of V2 but nothing is showing up in my search bar.

How can I get my searchText into the search bar and filtering my table after segue from V1 to V2?

1

1 Answers

1
votes

Are you sure the string is not empty before setting it as searchbar text?

The following code works perfectly for me.

@interface ViewController (){
    NSString *searchText;
}

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    searchText = @"I love ice cream!";
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.searchBar.text = searchText;
}

This is a method how you can check if the string is empty:

self.searchBar.text = (searchText == nil || searchText == (id)[NSNull null] || searchText.length == 0) ? @"To bad, an empty string" : searchText;