ios之UIPopoverCtroller

不知道为什么网上很多文章对于UIPopoverCtroller这个控件很少详细的描述。

下面我就把我遇到的这个问题说一下

刚开始接触UIPopoverCtroller这个的时候,只知道它是个浮窗。类似于一个下拉列表。

本人很笨用了一天的时间才知道这个怎么用。满满都是泪呀。什么都不说了。

还是用故事版来决定我们的布局。

我们现在看到了故事版中我们有个ViewController和一个tableviewController这个tableviewController就是我们的PopoverCtroller.

因为我们的PopoverController包含着TableviewController,我们再这里就比较方便使用了。

我们要创建一个ViewController的头文件(.h)和(.m)文件还有一个就是tableviwController的PopControler的.h和.m文件

在ViewController中我们一定要实现写上这个协议,否则的话我们就无法点击选中文本后让PoP消失了,这个坑可把我害苦了。

@interface UIViewController : UIViewController<UIPopoverControllerDelegate>
- (IBAction)showPop:(id)sender;//这个就是我们用的图中的button你也可以用导航按钮,我还是比较喜欢用普通按钮来代替呢。
@property(nonatomic,strong)UIPopoverController *pop; //这里一定要声明这是在.m文件里实现的

- (IBAction)showPop:(id)sender {

    UIPopController *popoverViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PopController"];
    
    if (self.pop == nil) {
        
        self.pop  = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
        
    }
//这里呢就是我们要定位,要让pop在那里显示,我们定位到的在一个文本上显示,忽略我脑子笨,在viewCtronnler中忘记了放lable
       [self.pop presentPopoverFromRect:_typeText.frame inView:_typeText.superview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

//这里就是来接受我们在popctrol中的值显示在文本上呢
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNewVal:) name:@"update" object:nil];
    

}
//这个方法就是我们给文本赋值的呢
-(void) setNewVal:(NSNotification*)notifi
{
    
    self.typeText.text = [notifi.userInfo objectForKey:@"Value"]; //这句就是选中之后pop就消失了,这句话可把我害苦了。脑子太笨。。。。

        [self.pop dismissPopoverAnimated:YES];
}
我们看到了一个UIPopController 这个类那我们接下来哟创建这个文件了。刚才说pop里面包含tableviwectroller这里用法和tableviewController一样的呢。就不细说了。
@interface UIPopController : UITableViewController

@property (nonatomic,strong) NSArray *listData;//这里是我们声明的数组,显示的下拉列表
@property (nonatomic,strong)NSString * titleString;//这里是我们要用到的传值呢


@end
.m实现文件
- (void)viewDidLoad
{
    [super viewDidLoad];
     self.listData = [[NSArray alloc] initWithObjects:@"全部类型",@"视频",@"音频",@"PPT",@"Word",@"其它", nil];
     //设置控制器在popovwe中的尺寸

    CGFloat maxH=MIN(480, self.listData.count*44);//我比较喜欢这种设置它的大小,不要吧pop的size设置固定,否则不好看。
    self.preferredContentSize=CGSizeMake(150, maxH);
    self.clearsSelectionOnViewWillAppear=NO;//这里是我们选中之后,再打开便记住了它上次选中的位置。确实比android上好用一百倍。给个赞。
   }
关于tableview要必须实现的两个方法我在这里就不写了呢。不懂的话,还是多看下大神的tableview的用法,这个还是很重要的呢。我这里主要介绍下我们在pop选中的文本之后的情况呢
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.titleString= [self.listData objectAtIndex:indexPath.row];//当我们点击pop中的每一项,我们要知道选中了什么接受过来
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.titleString,@"Value",nil];//这里是因为我们的viewctroller要显示出我们选中的文本,所以实验了很多方法还是用NOtification比较靠谱。大爱呀。我们要把获取到的东西放在一个字典里,然后用notification给它传递过去。
    [[NSNotificationCenter defaultCenter]postNotificationName:@"update" object:self userInfo:dic];
 
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章