Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to make PopOver menu in iOS
    text
    copied!<p>I want to display a popover menu on button click. It is giving me exception when i tried this code, i am not able to figure it out. here is the code of popViewController which is subclass of UITableViewController</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @protocol popdelegate &lt;NSObject&gt; @required -(void) getdata; @end @interface popViewController : UITableViewController @property (nonatomic, strong) NSMutableArray *array; @property (nonatomic, weak) id&lt;popdelegate&gt; delegate; @end </code></pre> <p>This is the implementation of popViewController.m</p> <pre><code>#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 &gt; 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 </code></pre> <p>Next is the code of my ViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "popViewController.h" @interface ViewController : UIViewController&lt;popdelegate&gt; @property (nonatomic, strong) popViewController *popmenu; @property (nonatomic, strong) UIPopoverController *controller; -(IBAction)pop:(id)sender; @end </code></pre> <p>And finally the ViewController.m</p> <pre><code>#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 </code></pre> <p>When i run this and click on button which is supposed to show the menu, at that time i am getting an exception which i am not able to resolve. Please help me i am new to ios programming.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload