UIPopoverController只能在ipad設備上面使用;作用是用於顯示臨時內容,特點是總是顯示在當前視圖最前端,有一個箭頭可以指示從哪一個button彈出來的,當單擊界面的其他地方時自動消失。
(1)創建兩個UIViewController類(ListViewController和oneViewController) ListViewController作為一個彈出的控制器視圖顯示 (2)建好彈出視圖顯示些什麼,就是彈出一個表。 [objc] #import <UIKit/UIKit.h> @interface ListViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> @property (strong,nonatomic)UITableView *favoriteTableView;//收藏夾table view @property (strong,nonatomic)NSMutableArray *list;//收藏夾list @end [objc] #import "ListViewController.h" #import "threeViewController.h" @interface ListViewController () @end @implementation ListViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.list = [[NSMutableArray alloc] initWithObjects:@"willingseal",@"http://blog.csdn.net/willingseal?viewmode=contents",@"有些事不是因為看到了希望才去堅持,而是堅持了才會看到了希望",@"愛情はひとりの詐欺師に戀をしていたR鹿な童話…バカ矍椁悉窯趣辘臥p欺に戀をしていたR鹿な童", nil nil]; [self addTableview]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //初始化table view -(void) addTableview { self.favoriteTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 400,500) style:UITableViewStylePlain];//初始化tabview // self.favoriteTableView.center =CGPointMake(self.view.center.x, self.view.center.y-70);//tableview的中心位置 self.favoriteTableView.delegate = self; self.favoriteTableView.dataSource=self; self.favoriteTableView.scrollEnabled=YES;//tabview是否滑動 // self.favoriteTableView.layer.cornerRadius=15;//圓角大小 // _loginTableView = TableView; self.favoriteTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]; [self.view addSubview:self.favoriteTableView]; } #pragma mark - Table view data source //行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } //多少個section - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //section裡面有多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex { NSLog(@"list is :%lu",[self.list count]); return [self.list count];; } //cell內容 - (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]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } NSInteger row = [indexPath row]; cell.textLabel.text = [self.list objectAtIndex:row]; return cell; } //點擊某個table view cell - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString * selectStr =[[NSString alloc] initWithFormat:@"%@",[self.list objectAtIndex:indexPath.row]]; NSLog(@"%@",[self.list objectAtIndex:indexPath.row]); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"willingseal" message:selectStr delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil nil]; [alert show]; } @end (3)點擊button,彈出表 [objc] #import <UIKit/UIKit.h> #import "ListViewController.h" @interface oneViewController : UIViewController <UIPopoverControllerDelegate>{ UIPopoverController *popoverController; } - (IBAction)tap:(UIButton *)sender; @end [objc] - (IBAction)tap:(UIButton *)sender { ListViewController *listVC =[[ListViewController alloc] init];//初始化ListViewController popoverController = [[UIPopoverController alloc] initWithContentViewController:listVC];//初始化popoverController-UIPopoverController該控制器的內容必須由一個控制器ViewController提供 [popoverController setPopoverContentSize:CGSizeMake(400, 500)];//設置popoverController的內容大小 [popoverController setDelegate:self];//設置popoverController代理 [popoverController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];//-第三個參數permittedArrowDirections是設置那個款箭頭的方向,可以向上/下/左/右不同方向彈出--彈出帶箭頭的窗口,這一種方法是用在view上彈出窗口,另一種用在導航欄(UIBarButtonItem)彈出窗口- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; } #pragma mark - popoverController - (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController { return YES; } //讓它消失 - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { NSLog(@"關閉了 PopverController "); }