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",@"有些事不是因为看到了希望才去坚持,而是坚持了才会看到了希望",@"爱情はひとりの诈欺师に恋をしていた馬鹿な童话…バカ愛情はひとりの詐欺師に恋をしていた馬鹿な童話", 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 ");
}