UIPopoverController 在iOS9之后被废弃了,,,
iOS8 新控件UIPopoverPresentationController可运用在iphone和iPad上,使用基本同 UIPopoverController
- - (void)iPadAndIphonePopOver {
MenuViewController *menuVC = [[MenuViewController alloc] init];
menuVC.modalPresentationStyle = UIModalPresentationPopover;
menuVC.popoverPresentationController.sourceView = self.view;
menuVC.popoverPresentationController.sourceRect = self.testButton.frame;
menuVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
[self presentViewController:menuVC animated:YES completion:nil];
}
code2: * - (IBAction)popOver:(id)sender { AddressViewController *contentVC = [[AddressViewController alloc] init]; contentVC.modalPresentationStyle = UIModalPresentationPopover; contentVC.popoverPresentationController.sourceView = self.view; contentVC.popoverPresentationController.sourceRect = CGRectMake(100, 100, 100, 100); contentVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; [self presentViewController:contentVC animated:YES completion:nil]; } * AddressViewController.m * #import "AddressViewController.h" @interface AddressViewController () <UITableViewDelegate, UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *masterTableView; // 省份tableView @property (weak, nonatomic) IBOutlet UITableView *detailTableView; // 地区tableView @property (nonatomic, strong) NSArray *masterDataSource; // 省份数据源 @property (nonatomic, strong) NSArray *detailDataSource; // 地区数据源 @property (nonatomic, assign) int masterIndex; // 选中的省份 @end @implementation AddressViewController - (void)viewDidLoad { [super viewDidLoad]; _masterIndex = 0; self.preferredContentSize = CGSizeMake(150, 300); } - (NSArray *)masterDataSource { if (_masterDataSource == nil) { _masterDataSource = @[@"河南", @"河北", @"江苏", @"浙江", @"广东"]; } return _masterDataSource; } - (NSArray *)detailDataSource { if (_detailDataSource == nil) { _detailDataSource = @[@[@"郑州", @"洛阳", @"周口", @"开封"], @[@"石家庄", @"石家庄2"], @[@"南京", @"苏州", @"淮安", @"淮北", @"句容"], @[@"杭州", @"温州", @"台州", @"宁波", @"瑞安", @"苍南"], @[@"广州", @"深圳"] ]; } return _detailDataSource; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.masterTableView) { return self.masterDataSource.count; } if (tableView == self.detailTableView) { return [self.detailDataSource[_masterIndex] count]; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.masterTableView) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"master"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"master"]; } cell.textLabel.text = self.masterDataSource[indexPath.row]; return cell; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detail"]; } cell.textLabel.text = self.detailDataSource[_masterIndex][indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == _masterTableView) { _masterIndex = (int)indexPath.row; self.preferredContentSize = CGSizeMake(300, 300); [_detailTableView reloadData]; } if (tableView == _detailTableView) { self.preferredContentSize = CGSizeMake(150, 300); [_detailTableView reloadData]; } } @end
注意:如果要去掉箭头 permittedArrowDirections = 0; 并且要在设置了 sourceView, sourceRect 之后才会生效
时间: 2024-10-14 05:26:30