Thomas
Thomas

Reputation: 2386

UISearchController - Objective C to Swift Issue

I'm trying to subclass UISearchController so I can add a custom UISearchBar. I found a way to do this in Objective-C, but I'm struggling to do it in Swift. Here are the 2 files that accomplish this in Objective-C:

CustomSearchController.h

@interface CustomSearchController : UISearchController <UISearchBarDelegate>

@end

CustomSearchController.m

#import "CustomSearchController.h"
#import "CustomSearchBar.h"

@implementation CustomSearchController
{
    UISearchBar *_searchBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(UISearchBar *)searchBar {

    if (_searchBar == nil) {
        _searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectZero];
        _searchBar.delegate = self; // different from table search by apple where delegate was set to view controller where the UISearchController was instantiated or in our case where CustomSearchController was instantiated.
    }
    return _searchBar;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchBar.text length] > 0) {
        self.active = true;
    } else {
        self.active = false;
    }
}

/*
 Since CustomSearchController is the delegate of the search bar we must implement the UISearchBarDelegate method.
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"Became first responder");
    [searchBar resignFirstResponder];
}


@end

The issue I'm running into is specifically with this getter:

-(UISearchBar *)searchBar {

    if (_searchBar == nil) {
        _searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectZero];
        _searchBar.delegate = self; // different from table search by apple where delegate was set to view controller where the UISearchController was instantiated or in our case where CustomSearchController was instantiated.
    }
    return _searchBar;
}

In Swift I believe I would be have to do something like this:

var customSearchBar: CustomSearchBar?

override var searchBar: UISearchBar {
    get {
        if customSearchBar == nil {
            customSearchBar = CustomSearchBar()
            customSearchBar?.delegate = self
        }
        return customSearchBar!
    }
}

But is this the best way to do something like this?

Upvotes: 0

Views: 206

Answers (1)

jaetzold
jaetzold

Reputation: 1688

Try this:

lazy var customSearchBar: CustomSearchBar = {
    [unowned self] in
    let result = CustomSearchBar(frame:CGRectZero)
    result.delegate = self
    return result
}()

override var searchBar: UISearchBar {
    get {
        return customSearchBar
    }
}

The usage of lazy takes care of initializing the CustomSearchBar instance only when first accessed. Although I'm not sure that you really need that for what you are trying to accomplish.

Upvotes: 2

Related Questions