dimanche 26 juin 2016

Create Custom UIView with Custom WebView and UISearchContoller

I'm working on an iOS app and I created an UIViewController where I put my components and it works fine . Now I'm trying to create a Custom UIView and to put my webview and my searchcontroller into . I spent a lot of time without success .

Here is my .m file and I hope some one can help me with :

#import "HomeViewController.h"


#define widthtScreen  [UIScreen mainScreen].bounds.size.width
#define heightScreen  [UIScreen mainScreen].bounds.size.height


@interface HomeViewController () <UISearchResultsUpdating,UISearchBarDelegate,UIBarPositioningDelegate,UITableViewDataSource,UITableViewDelegate,MapWebViewDelegate>

@property(strong,nonatomic) MapWebView *webView;

@property (nonatomic) UIButton  *btnGeolocate;


@property (nonatomic, strong) UISearchController *searchController;

@end

@implementation HomeViewController{
    NSMutableArray *placesList;
    BOOL isSearching;
}



-(void)loadView
{
    [super loadView];
    self.webView = [[MapWebView alloc] initWithFrame:CGRectMake(0, -100, widthtScreen, heightScreen+100)];
    self.webView.mapWebViewDelegate = self;
    [self.view addSubview:self.webView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.hidesBackButton = YES;
    mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    placesList = [[NSMutableArray alloc] init];
    [self initializeSearchController];
    mainDelegate.webView = self.webView;


    self.btnGeolocate = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-75,550,60,60)];
    self.btnGeolocate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.btnGeolocate setBackgroundImage:[UIImage imageNamed:@"geo.png"]
                                 forState:UIControlStateNormal];
    [self.btnGeolocate addTarget:self action:@selector(btnGeolocatePressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnGeolocate];

    mainDelegate.btnZoomIn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-80,620,30,30)];
    mainDelegate.btnZoomIn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [mainDelegate.btnZoomIn setBackgroundColor:[UIColor blackColor]];
    [mainDelegate.btnZoomIn addTarget:self action:@selector(btnZoomInPressed:) forControlEvents:UIControlEventTouchUpInside];
    mainDelegate.btnZoomIn.tag=1;
    UIImage *btnImage = [UIImage imageNamed:@"plus.png"];
    [mainDelegate.btnZoomIn setImage:btnImage forState:UIControlStateNormal];
    [self.view addSubview:mainDelegate.btnZoomIn];

    mainDelegate.btnZoomOut = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-40,620,30,30)];
    mainDelegate.btnZoomOut.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [mainDelegate.btnZoomOut setBackgroundColor:[UIColor blackColor]];
    [mainDelegate.btnZoomOut addTarget:self action:@selector(btnZoomOutPressed:) forControlEvents:UIControlEventTouchUpInside];
    mainDelegate.btnZoomOut.tag=1;
    UIImage *btnImage2 = [UIImage imageNamed:@"minus.png"];
    [mainDelegate.btnZoomOut setImage:btnImage2 forState:UIControlStateNormal];
    [self.view addSubview:mainDelegate.btnZoomOut];
}

- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
{
    return YES;
}

- (BOOL)slideNavigationControllerShouldDisplayRightMenu
{
    return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"number :%lu",(unsigned long)[placesList count]);
    return [placesList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    SuggestResultObject *sro = [SuggestResultObject new];
    sro = [placesList objectAtIndex:indexPath.row];
    cell.textLabel.text = sro.textPlace;
    return cell;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem =nil;
    return true;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.navigationItem.leftBarButtonItem = mainDelegate.leftBarButtonItem;
    self.navigationItem.rightBarButtonItem = mainDelegate.rightBarButtonItem;
    SuggestResultObject *sro = [SuggestResultObject new];
    sro = [placesList objectAtIndex:indexPath.row];
    self.searchController.active = false;
    NSString *function = [[NSString alloc] initWithFormat: @"MobileManager.getInstance().moveToLocation("%@","%@")", sro.latPlace,sro.lonPlace];
    [_webView evaluateJavaScript:function completionHandler:nil];
}



- (void)jsRun:(NSString *) searchText {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *function = [[NSString alloc] initWithFormat: @"MobileManager.getInstance().setSuggest("%@")", searchText];
        [_webView evaluateJavaScript:function completionHandler:nil];
    });
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearching = YES;
}

- (void)initializeSearchController {
    UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    searchResultsController.tableView.dataSource = self;
    searchResultsController.tableView.delegate = self;
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
    self.definesPresentationContext = YES;
    self.searchController.hidesNavigationBarDuringPresentation = false;
    self.searchController.accessibilityElementsHidden= true;
    self.searchController.dimsBackgroundDuringPresentation = true;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    self.navigationItem.titleView = self.searchController.searchBar;
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    self.navigationItem.leftBarButtonItem = mainDelegate.leftBarButtonItem;
    self.navigationItem.rightBarButtonItem = mainDelegate.rightBarButtonItem;
    NSLog(@"Cancel clicked");
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    [placesList removeAllObjects];
}


-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    [placesList removeAllObjects];
    if([searchController.searchBar.text length] != 0) {
        isSearching = YES;
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        [self jsRun:searchController.searchBar.text];
    }
    else {
        isSearching = NO;
        [((UITableViewController *)self.searchController.searchResultsController).tableView reloadData];
    }
}

-(void) btnGeolocatePressed : (id) sender{
}

-(void) btnZoomInPressed : (id) sender{
    [_webView evaluateJavaScript:@"MobileManager.getInstance().zoomIn();" completionHandler:nil];
}

-(void) btnZoomOutPressed : (id) sender{
    [_webView evaluateJavaScript:@"MobileManager.getInstance().zoomOut();" completionHandler:nil];
}

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

- (void)searchResult:(NSArray*)dataArray{
    SuggestResultObject *sro = [SuggestResultObject new];
    sro.textPlace = [dataArray objectAtIndex:0];
    sro.lonPlace = [dataArray objectAtIndex:1];
    sro.latPlace = [dataArray objectAtIndex:2];
    [placesList addObject:sro];
    [((UITableViewController *)self.searchController.searchResultsController).tableView reloadData];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
@end

Aucun commentaire:

Enregistrer un commentaire