My Blog List

Android FirstAid Coding

A Small Help From a Small Heart
Powered by Blogger.

A software professional, who still likes to code, likes to blog and loves gadgets.

Tuesday 28 May 2013

Showing UIPickerView inside UIPopoverController


ViewController.h
=============
#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>{
    int selectedProductTypeSrcIndex;  
    NSString *selectedProductType;  
    IBOutlet UIButton *productTypeOption;  
}
@property(nonatomic,retain) NSArray *productTypeSrc;
@property(nonatomic,retain) UIPickerView *myPickerView;
@property(nonatomic,retain) UIPopoverController *popoverController;
- (IBAction)showOptionAction:(id)sender;

@end



ViewController.m
================

//
// ViewController.m
// PickerDemo
//
// Created by Rajendra on 27/05/13.
// Copyright (c) 2013 RajuAndroid.blogspot.com. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


@synthesize myPickerView,popoverController;
@synthesize productTypeSrc;
- (void)viewDidLoad

{

[super viewDidLoad];
self.productTypeSrc = [NSArray arrayWithObjects:@"Collison Damage Waive",@"Deposit Waiver",@"Excess Reduction Waiver",@"Malaysian Travel Charges Waiver",@"Penalty Waiver",nil];
selectedProductTypeSrcIndex=0;

}

- (IBAction)showOptionAction:(id)sender{
UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 260)];
UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:@"Title"];
[pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
[masterView addSubview:pickerToolbar];
CGRect pickerFrame = CGRectMake(0, 40, 300, 216);
UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
[myPicker setDataSource: self];
[myPicker setDelegate: self];
myPicker.tag=1;
[myPicker selectRow:0 inComponent:0 animated:NO];
[myPicker setShowsSelectionIndicator:YES];
self.myPickerView=myPicker;
[masterView addSubview:myPicker];
UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController.view = masterView;
viewController.contentSizeForViewInPopover = viewController.view.frame.size;
self.popoverController =[[UIPopoverController alloc] initWithContentViewController:viewController];
UIButton *button = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:button.bounds
inView:button
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];

}

- (UIToolbar *)createPickerToolbarWithTitle:(NSString *)title {

CGRect frame = CGRectMake(0, 0, 300, 44);
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:frame];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [self createButtonWithType:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [self createButtonWithType:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[barItems addObject:flexSpace];
if (title){
UIBarButtonItem *labelButton = [self createToolbarLabelWithTitle:title];
[barItems addObject:labelButton];
[barItems addObject:flexSpace];
}
UIBarButtonItem *doneButton = [self createButtonWithType:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone:)];
[barItems addObject:doneButton];
[pickerToolbar setItems:barItems animated:YES];
return pickerToolbar;

}

- (IBAction)actionPickerDone:(id)sender {

//[self notifyTarget:self.target didSucceedWithAction:self.successAction origin:[self storedOrigin]];

if (self.popoverController && self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];

}

- (IBAction)actionPickerCancel:(id)sender {

if (self.popoverController && self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];

}

- (UIBarButtonItem *)createToolbarLabelWithTitle:(NSString *)aTitle {

UILabel *toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = aTitle;
UIBarButtonItem *buttonLabel = [[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
return buttonLabel;

}

- (UIBarButtonItem *)createButtonWithType:(UIBarButtonSystemItem)type target:(id)target action:(SEL)buttonAction {

return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:target action:buttonAction];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

{

return 1;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

{

if ([pickerView tag] == 1) {
return [self.productTypeSrc count];
}
return 0;

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

{
if ([pickerView tag] == 1) {
return [self.productTypeSrc objectAtIndex:row];

}

return @"";

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

if ([pickerView tag] == 1) {
selectedProductTypeSrcIndex=row;
selectedProductType=[self.productTypeSrc objectAtIndex:selectedProductTypeSrcIndex];
[productTypeOption setTitle:selectedProductType forState:UIControlStateNormal];

}

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


 
 

7 comments:

  1. Can you pls do for Android too thanks

    ReplyDelete
    Replies
    1. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'

      Delete
  2. Hi grate tutorial working fine for mr. Thanksssssss.

    ReplyDelete
  3. Thanks... iv been looking for this

    ReplyDelete
  4. 2015-02-23 10:47:46.890 gittt-hub[1148:31024] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'
    *** First throw call stack:
    (
    0 CoreFoundation 0x000000010e231f35 __exceptionPreprocess + 165
    1 libobjc.A.dylib 0x000000010decabb7 objc_exception_throw + 45
    2 CoreFoundation 0x000000010e231e6d +[NSException raise:format:] + 205
    3 UIKit 0x000000010ec0d5cb -[UIPopoverController _initWithContentViewController:popoverControllerStyle:] + 253
    4 gittt-hub 0x000000010d99aee4 -[ViewController showOptionAction:] + 1236
    5 UIKit 0x000000010e61f8be -[UIApplication sendAction:to:from:forEvent:] + 75
    6 UIKit 0x000000010e726410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    7 UIKit 0x000000010e7257df -[UIControl touchesEnded:withEvent:] + 522
    8 UIKit 0x000000010e665308 -[UIWindow _sendTouchesForEvent:] + 735
    9 UIKit 0x000000010e665c33 -[UIWindow sendEvent:] + 683
    10 UIKit 0x000000010e6329b1 -[UIApplication sendEvent:] + 246
    11 UIKit 0x000000010e63fa7d _UIApplicationHandleEventFromQueueEvent + 17370
    12 UIKit 0x000000010e61b103 _UIApplicationHandleEventQueue + 1961
    13 CoreFoundation 0x000000010e167551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14 CoreFoundation 0x000000010e15d41d __CFRunLoopDoSources0 + 269
    15 CoreFoundation 0x000000010e15ca54 __CFRunLoopRun + 868
    16 CoreFoundation 0x000000010e15c486 CFRunLoopRunSpecific + 470
    17 GraphicsServices 0x00000001118139f0 GSEventRunModal + 161
    18 UIKit 0x000000010e61e420 UIApplicationMain + 1282
    19 gittt-hub 0x000000010d99c2a3 main + 115
    20 libdyld.dylib 0x00000001107c1145 start + 1
    21 ??? 0x0000000000000001 0x0 + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb)
    ,,get this error plz help me

    ReplyDelete
    Replies
    1. iam new at ios and need a popup uipicker

      Delete