无法在iOS中创建PopOver菜单

时间:2013-11-20 10:04:17

标签: ios objective-c

我想在按钮点击时显示弹出菜单。当我尝试这段代码时,它给了我一个例外,我无法弄明白。 这是popViewController的代码,它是UITableViewController的子类

#import <UIKit/UIKit.h>
@protocol popdelegate <NSObject>
@required
-(void) getdata;
@end
@interface popViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, weak) id<popdelegate> delegate;
@end

这是popViewController.m

的实现
#import "popViewController.h"
@interface popViewController ()

@end

@implementation popViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    _array = [NSMutableArray alloc];
    [_array addObject:@"New"];
    [_array addObject:@"Open"];
    [_array addObject:@"Shape"];
    NSInteger rowsCount = [_array count];
    NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView
                                           heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSInteger totalRowsHeight = rowsCount * singleRowHeight;

    CGFloat largestLabelWidth = 0;
    for (NSString *colorName in _array) {

        CGSize labelSize = [colorName sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]];
        if (labelSize.width > largestLabelWidth) {
            largestLabelWidth = labelSize.width;
        }
    }
    CGFloat popoverWidth = largestLabelWidth + 100;

    self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight);
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
cell.textLabel.text = [_array objectAtIndex:indexPath.row];
// Configure the cell...

return cell;
}
@end

接下来是我的ViewController.h的代码

#import <UIKit/UIKit.h>
#import "popViewController.h"
@interface ViewController : UIViewController<popdelegate>
@property (nonatomic, strong) popViewController *popmenu;
@property (nonatomic, strong) UIPopoverController *controller;
-(IBAction)pop:(id)sender;
@end

最后是ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)pop:(id)sender
{
if (_popmenu == nil) {
    _popmenu = [[popViewController alloc] initWithStyle:UITableViewStylePlain];
    _popmenu.delegate = self;
}

if (_controller == nil) {
    _controller = [[UIPopoverController alloc] initWithContentViewController:_popmenu];
    [_controller presentPopoverFromBarButtonItem:(UIBarButtonItem *)sender
                                permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
} else {
    [_controller dismissPopoverAnimated:YES];
    _controller = nil;
}
}
- (void) getdata{    
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

当我运行它并单击应该显示菜单的按钮时,那时我得到一个我无法解决的异常。请帮助我,我是ios编程的新手。

1 个答案:

答案 0 :(得分:0)

YourPopOverController * objPop = [[YourPopOverController alloc] initWithNibName:@"YourPopOverController" bundle:nil];

UIPopoverController   *popOverController = [[UIPopoverController alloc] initWithContentViewController:objPop];

 popOverController.delegate = self;

[popOverController setPopoverContentSize:CGSizeMake(266, 480)];

[popOverController presentPopoverFromRect:yourBtn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

但是Popover仅适用于Ipad ......

跳这会帮助你...