2016-06-27 15 views
0

私はiOSアプリケーションで作業しています。私はコンポーネントを配置したところでUIViewControllerを作成しました。 今私はカスタムUIViewを作成し、WebViewと私のSearchControllerを入れようとしています。私は成功せずに多くの時間を費やしました。ここでカスタムWebViewとUISearchContollerを使用してカスタムUIViewを作成

は私.mファイルであり、私はいくつかのいずれかがで私を助けることを願って:

#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 
+0

こんにちはOPをそれをテストし、それが役立つだろう。 "ここに私のコード、ヘルプ "です。 – Brandon

+0

問題は、uisearchcontrollerを使ってカスタムWebビューをカスタムuiviewに配置しようとしているのですが、uiviewcontrollerで呼び出すことができませんでしたが、ビューではナビゲーションコントローラを使用できないため、手動では動作しません –

+0

@OP; NavigationControllerに埋め込まれているUIViewControllerに埋め込まれたUIWebViewとUISearchControllerが必要ですか?または、UIViewに埋め込まれているNavigationControllerにUIWebViewとUISearchControllerを埋め込みますか? – Brandon

答えて

0

スクリーンショット(StackOverflowの上で画像を拡大縮小する方法がわからない... OSXは、網膜の形式で画像を取り、彼らはかなり大きいです!事前に申し訳ありません):!

http://i.imgur.com/15qxDpc.png

http://i.imgur.com/QHduP07.png

どのように動作しますか? UIViewと、2つのサブビュー:UITableViewUIWebViewを作成します。正しく拘束してください。

initメソッドのパラメータとしてnil SearchResultsController でUISearchControllerを作成します。

これにより、結果が現在のコントローラ/ビューに表示される になることが検索コントローラに通知されます。

次に、UISearchControllerの代理人を設定し、フィルタリングの機能を に作成します。

ビューが作成されたので、UIViewController〜 のテストが必要です。私たちは、

以下

が、私はUINavigationControllerに追加することを選択した...がある場合NavigationControllerへのtableViewヘッダーまたは に検索バーを追加することができますどちらかと私は は、上のナビゲーションバーを非表示にしないためにUISearchControllerに語りました プレゼンテーション。

このようにして、ナビゲーションバーの を隠すことなく、結果が現在のビューに表示されます。

その後、しかし、良いアイデアではなくUIWebViewの Javascriptを実行するためにJSContextを使用することですあなたは...

のためにそれを使用しているJavaScriptのどんな検索 を行うには画面外に隠れているのWebViewを使用してすることができます。 UIWebView の利点は、HTMLを解析してJSContext が許可しないDOMを変更できることです。とにかく

..ここ

は、私がUINavigationControllerに埋め込まれ UIViewControllerに追加し、その後 UISearchControllerUIWebViewが含まれています。..とUIViewのために書いたコードです。

// 
// SearchView.h 
// StackOverflow 
// 
// Created by Brandon T on 2016-06-26. 
// Copyright © 2016 XIO. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@class SearchView; 

@protocol SearchViewDelegate <UISearchBarDelegate> 
- (void)didSelectRowAtIndexPath:(SearchView *)searchView tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath; 
@end 

@interface SearchView : UIView 
@property (nonatomic, weak) id<SearchViewDelegate> delegate; 
- (UISearchBar *)getSearchBar; 
- (UIWebView *)getWebView; 
@end 


// 
// SearchView.m 
// StackOverflow 
// 
// Created by Brandon T on 2016-06-26. 
// Copyright © 2016 XIO. All rights reserved. 
// 

#import "SearchView.h" 

#define kTableViewCellIdentifier @"kTableViewCellIdentifier" 

@interface SearchView() <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating> 
@property (nonatomic, strong) UIWebView *webView; 
@property (nonatomic, strong) UISearchController *searchController; 
@property (nonatomic, strong) UITableView *tableView; 
@property (nonatomic, strong) NSArray *dataSource; 
@property (nonatomic, strong) NSArray *searchResults; 
@end 

@implementation SearchView 

- (instancetype)init { 
    if (self = [super init]) { 

     [self setupData]; 
     [self initControls]; 
     [self themeControls]; 
     [self registerCells]; 
     [self doLayout]; 
    } 
    return self; 
} 

- (UISearchBar *)getSearchBar { 
    return _searchController.searchBar; 
} 

- (UIWebView *)getWebView { 
    return _webView; 
} 

- (void)setDelegate:(id<SearchViewDelegate>)delegate { 
    _delegate = delegate; 
    _searchController.searchBar.delegate = delegate; 
} 

- (void)setupData { 
    //Begin fake data 

    _dataSource = @[@"Cat", @"Dog", @"Bird", @"Parrot", @"Rabbit", @"Racoon", @"Rat", @"Hamster", @"Pig", @"Cow"]; 

    //End fake data 


    _searchResults = [_dataSource copy]; 
} 

- (void)initControls { 
    _webView = [[UIWebView alloc] init]; 
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; 
} 

- (void)themeControls { 
    [_webView setHidden:YES]; 

    [_tableView setDelegate:self]; 
    [_tableView setDataSource:self]; 

    _searchController.searchResultsUpdater = self; 
    _searchController.dimsBackgroundDuringPresentation = false; 
    _searchController.definesPresentationContext = true; 
    _searchController.hidesNavigationBarDuringPresentation = false; 
} 

- (void)registerCells { 
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellIdentifier]; 
} 

- (void)doLayout { 
    [self addSubview:_webView]; 
    [self addSubview:_tableView]; 

    NSDictionary *views = @{@"webView":_webView, @"tableView": _tableView}; 
    NSMutableArray *constraints = [[NSMutableArray alloc] init]; 

    [constraints addObject:[NSString stringWithFormat:@"H:|-(%d)-[webView]-(%d)-|", 0, 0]]; 
    [constraints addObject:[NSString stringWithFormat:@"H:|-(%d)-[tableView]-(%d)-|", 0, 0]]; 

    [constraints addObject:[NSString stringWithFormat:@"V:|-(%d)-[webView(%d)]-(%d)-[tableView]-(%d)-|", -100, 100, 0, 0]]; 

    for (NSString *constraint in constraints) { 
     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]]; 
    } 

    for (UIView *view in self.subviews) { 
     [view setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    } 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return _searchController.active && _searchController.searchBar.text.length > 0 ? [_searchResults count] : [_dataSource count]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 50; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier forIndexPath:indexPath]; 

    if (_searchController.active && _searchController.searchBar.text.length > 0) { 
     cell.textLabel.text = _searchResults[indexPath.row]; 
    } 
    else { 
     cell.textLabel.text = _dataSource[indexPath.row]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectRowAtIndexPath:tableView:indexPath:)]) { 
     [self.delegate didSelectRowAtIndexPath:self tableView:tableView indexPath:indexPath]; 
    } 
} 


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController { 
    [self filterResults:searchController.searchBar.text scope:nil]; 
} 

- (void)filterResults:(NSString *)searchText scope:(NSString *)scope { 
    _searchResults = [_dataSource filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { 

     NSString *object = [evaluatedObject uppercaseString]; 
     return [object rangeOfString:[searchText uppercaseString]].location != NSNotFound; 
    }]]; 

    [_tableView reloadData]; 
} 

@end 

あなたは、実際に問題が代わりに何であったかを語ってくれたなら、私は.. UINavigationControllerに埋め込まれたUIViewController以下で

// 
// ViewController.m 
// StackOverflow 
// 
// Created by Brandon T on 2016-06-26. 
// Copyright © 2016 XIO. All rights reserved. 
// 

#import "ViewController.h" 
#import "SearchView.h" 


@interface ViewController()<SearchViewDelegate> 
@property (nonatomic, strong) SearchView *searchView; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self initControls]; 
    [self themeControls]; 
    [self doLayout]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 


- (void)initControls { 
    _searchView = [[SearchView alloc] init]; 
} 

- (void)themeControls { 
    self.edgesForExtendedLayout = UIRectEdgeNone; 
    self.navigationItem.titleView = [_searchView getSearchBar]; 

    [_searchView setDelegate:self]; 
} 

- (void)doLayout { 
    [self.view addSubview:_searchView]; 

    NSDictionary *views = @{@"searchView":_searchView}; 
    NSMutableArray *constraints = [[NSMutableArray alloc] init]; 

    [constraints addObject:[NSString stringWithFormat:@"H:|-%d-[searchView]-%d-|", 0, 0]]; 
    [constraints addObject:[NSString stringWithFormat:@"V:|-%d-[searchView]-%d-|", 0, 0]]; 

    for (NSString *constraint in constraints) { 
     [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]]; 
    } 

    for (UIView *view in self.view.subviews) { 
     [view setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    } 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    self.navigationItem.leftBarButtonItems = nil; 
    self.navigationItem.rightBarButtonItems = nil; 
} 

- (void)didSelectRowAtIndexPath:(SearchView *)searchView tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath { 

    [[searchView getWebView] stringByEvaluatingJavaScriptFromString:@"SomeJavascriptHere"]; 

} 
@end 
+0

大きな感謝の男性 - (UISearchBar *)getSearchBar { return _searchController.searchBar; } これは私のコードでは見逃されている機能です –

+0

検索バーをクリックしてタイプするといくつかのテストの後に。それは消える –

+0

私が掲示したコードは、 '_searchController.hidesNavigationBarDuringPresentation = false;'と検索バーがナビゲーションバーにあるため、検索バーを隠さない。 – Brandon

関連する問題