iOS多视图控制器

时间:2015-08-18 07:58:24

标签: ios objective-c

我是iOS编程的新手。

我必须基于第三方地图API实施位置搜索,但我认为我搞砸了多个视图控制器。

情景:

我有mapviewController来显示地图视图。我的搜索栏有searchControllerresultviewController中有一个searchController属性用于显示搜索建议。

我曾尝试将搜索VC添加为子VC来映射VC,但是一旦我点击搜索栏就会返回"应用程序试图以模态方式呈现一个活动控制器"。

然后我从地图VC&中删除搜索VC搜索栏未激活。

因此,我想询问如何处理这些VC,并避免地图视图覆盖搜索栏和建议列表的情况。

非常感谢你。

更新: 我在代码中构建搜索栏。我的目标是在iOS中构建以下样式。

like this http://i1.tietuku.com/cd4efa23d97af75ft.jpg

我将地图viewController从菜单推送到导航控制器。

[self.navigationController pushViewController:mapviewController animated:YES];

尝试向其添加searchController。

- (void)viewDidLoad {
      [super viewDidLoad];

    _resultsTableController = [[UITableViewController alloc] init];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
    _searchController.searchResultsUpdater = self;
    [_searchController.searchBar sizeToFit];
    _resultsTableController.tableView.tableHeaderView = _searchController.searchBar;
self.searchController.searchBar.delegate = self;

    _resultsTableController.tableView.delegate = self;
    _searchController.delegate = self;
    _searchController.dimsBackgroundDuringPresentation = NO; 
    _searchController.searchBar.delegate = self; 

    mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, kHeight(100), kScreen_Width , kScreen_Height-kHeight(100))];

    [self addChildViewController:_searchController];
    [self.view addSubview: _searchController.view];

    [self.view addSubview:mapView];
}

2 个答案:

答案 0 :(得分:0)

据我说,你应该只使用一个有地图的视图控制器。在地图上,您可以简单地添加自定义搜索视图,在输入/点击搜索时,您可以使用UITableview显示搜索结果,并使用一些删除动画保持一些最大帧固定,最后在录制任何结果时删除此UITableView并显示地图的位置。如果时间允许,我会为您创建一个样品,但不能保证,但会尝试。同时您也可以查看Maps reference可能会有所帮助

答案 1 :(得分:0)

对于UISearchController,您无需将其添加为子视图控制器,并且即时使用UITableViewController使用UITableView它使您和 下面是.m文件的代码 免费运行该代码并且不使用故事板。

@interface ViewController()

{

UITableView *tableview;

UISearchController *searchcontroller;

NSMutableArray *array;}

@end

@implementation ViewController

  • (void)viewDidLoad

{

[super viewDidLoad];


array = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three",@"four", nil];


 tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];


 [self.view addSubview:tableview];

tableview.dataSource = self;

tableview.delegate =self;

searchcontroller = [[UISearchController alloc]initWithSearchResultsController:nil]; // Create Reference of searchController

searchcontroller.searchResultsUpdater = self;

searchcontroller.searchBar.backgroundColor = [UIColor redColor];

searchcontroller.searchBar.barStyle = UIBarStyleBlack;

searchcontroller.searchBar.barTintColor = [UIColor whiteColor];

searchcontroller.dimsBackgroundDuringPresentation = false;

[searchcontroller.searchBar sizeToFit];

tableview.tableHeaderView = searchcontroller.searchBar;


MKMapView *mapview = [[MKMapView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(tableview.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];

[self.view addSubview:mapview];

}

  • (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning]; //处理可以重新创建的任何资源。

} - (void)updateSearchResultsForSearchController:(UISearchController *)searchController;

{

NSLog(@"update ");

} - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //如果未实现,默认值为1

{

return 1;

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return array.count;

}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *identifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil)

{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];

}

cell.textLabel.text = array[indexPath.row];

return cell;

}

相关问题