How to prevent UIPickerView from closing when the user selects a field? - ios

I am trying to prevent my UIPickerview from closing when a field is selected by a user. I want the user to be able to select a field without UIPickerview dismissing automatically. I have tried so many things such as trying the following but none of them helped:
_txtfield.hidden=NO;
[_pickerView endEditing:NO];
[pickerView endEditing:NO];
[_txtfield endEditing:NO];
[self endEditing:NO];
_pickerView.hidden=NO;
-----------------Here is more code----------------
#implementation FieldWithPickerView {
void(^pickerCallback)(NSInteger row);
CGRect myFrame;
}
-(void)viewDidAppear{
}
-(void)commonInit:(CGRect)frame{
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[view]|" options:0 metrics:nil views:#{#"view":self.InputViewPicker}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:#{#"view":self.InputViewPicker}]];
[self.layer setCornerRadius:10.0f];
[self setClipsToBounds:YES];
_pickerView = [[UIPickerView alloc] init];
_pickerView.delegate=self;
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(removePicker)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[self.txtfield setInputAccessoryView:toolBar];
self.txtfield.inputView = _pickerView;
[_txtfield.inputView setBackgroundColor:[UIColor clearColor]];
[_txtfield setBackgroundColor:[UIColor clearColor]];
_txtfield.text=#"--Select---";
_label_view.textColor=[UIColor whiteColor];
_label_view.backgroundColor=SECURUSBLUE;
_label_view.textAlignment = NSTextAlignmentCenter;
_txtfield.textAlignment = NSTextAlignmentCenter;
}
-(void)removePicker
{
[_txtfield resignFirstResponder];
}
-(void)pickerListenner:(void(^)(NSInteger row))handler
{
pickerCallback=handler;
}
- (id)initWithFrame:(CGRect)frame
{
// NSLog(#"initwithframe picker");
self = [super initWithFrame:frame];
if (self)
{
[self commonInit:frame];
}
return self;
}
#pragma mark - Picker View Data source
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [_pickerData count];;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if(_pickerData.count == 0)
return #"There is nothing";
return [_pickerData objectAtIndex:row];
}
#pragma mark -
#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// Code logic
NSLog(#"selected row --->%ld!!!!",(long)row);
pickerCallback(row);
// _txtfield.text=[_pickerData objectAtIndex:row];
_txtfield.hidden=NO;
}
- (NSInteger)selectedRowInComponent:(NSInteger)component
{
return component;
}

You need to implement the UIPickerView delegate methods, so add <UIPickerViewDelegate> to your ViewController.
in your viewDidLoad method conform to the protocol like so:
pickerView.delegate = self;
Then add the following method to detect when a row has been selected:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// do nothing because it will not dismiss
}
You will need to implement the numberOfRowsInComponent, numberOfComponents, and titleForRow delegate methods as well, but once those are implemented your picker view should not dismiss when a row is selected. You will need to manually dismiss the picker view in your didSelectRow delegate method.

Related

PickerView is not dismissing when Done button is pressed

I have created a text field that upon entry will open a picker view with a toolbar that contains a done button. However, when the done button is pressed the picker view doesn't dismiss. Everything else works just as I want except this. I've tried several options to no avail. Please review and let me know what I'm missing.
My code is below:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{IBOutlet UITextField *productDescription; IBOutlet UIPickerView *productPicker; NSArray *productListArray}
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)addPickerView{
productListArray = [[NSArray alloc]initWithObjects:
#"myArray", nil];
productDescription.delegate = self;
[self.view addSubview:productDescription];
[productDescription setPlaceholder:#"Product Description"];
productPicker = [[UIPickerView alloc]init];
productPicker.dataSource = self;
productPicker.delegate = self;
productPicker.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self action:#selector(resignFirstResponder)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(50, 320, 50, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
productDescription.inputView = productPicker;
productDescription.inputAccessoryView = toolBar;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self addPickerView];
}
#pragma mark - Text field delegates
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
([textField.text isEqualToString:#""]);
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [productListArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
[productDescription setText:[productListArray objectAtIndex:row]];
}
- (void)doneButton:(UIBarButtonItem *)sender{
NSLog(#"Done Touched");
[productPicker setHidden:YES];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)row forComponent:(NSInteger)component{
return [productListArray objectAtIndex:row];
}
#end
.M File
Xib file in take Textfield and set delegate with connect.
#import "YourViewController.h"
#interface YourViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView *productPicker;
NSArray *productListArray;
IBOutlet UITextField *productDescription;
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
[self addPickerView];
}
-(void)addPickerView
{
productListArray = [[NSArray alloc]initWithObjects:#"myArray",#"Rohit",#"Change",#"Your view", nil];
[productDescription setPlaceholder:#"Product Description"];
productPicker = [[UIPickerView alloc]init];
productPicker.dataSource = self;
productPicker.delegate = self;
productPicker.showsSelectionIndicator = YES;
UIToolbar* toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
toolBar.tintColor = nil;
[toolBar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButton:)];
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
productDescription.inputView = productPicker;
productDescription.inputAccessoryView = toolBar;
}
- (IBAction)doneButton:(id)sender
{
NSLog(#"Done Touched");
[productPicker removeFromSuperview];
[productPicker resignFirstResponder];
[self.view endEditing:YES];
}
#pragma mark - Text field delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {
productDescription.inputView = productPicker;
}
#pragma mark - Text field delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {
productDescription.inputView = productPicker;
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [productListArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[productDescription setText:[productListArray objectAtIndex:row]];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [productListArray objectAtIndex:row];
}
I hope this will help you great.

Not able to use two UIPickerView in the same Controller

I'm trying to use more than two UIPickerViews together in one ViewController. Each UIPickerView has different data array. I am using interface builder to link the pickers up. What I want is, after button click, my PickerView will show up and user can select the data from it.That selected data will come in the text field. It is working fine for first PickerView but when I click for the second PickerView, the value is coming from first array and I am not able to write condition. Here's all my code:
.h file
#import <UIKit/UIKit.h>
#interface Tabbar2ViewController : UIViewController<UITextViewDelegate,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{
IBOutlet UIPickerView *genderPickerView;
IBOutlet UIPickerView *pickerViewoption;
IBOutlet UIButton *BrandPickerBtn;
IBOutlet UIButton *genderPickerfBtn;
NSArray *brands;
NSArray *gender;
BOOL isCheckedpickerView;
IBOutlet UILabel *BrandLabelfield;
IBOutlet UILabel *GenderLabelfield;
}
- (IBAction)action:(id)sender;
- (IBAction)genderAction:(id)sender;
#property(nonatomic,retain)NSArray *brands;
#property(nonatomic,retain)NSArray *gender;
#end
.m file
#import "ttViewController.h"
#interface ttViewController ()
#end
#implementation ttViewController
#synthesize countries,size,brands,gender,activity,condition,color;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
isCheckedpickerView=FALSE;
pickerViewoption.hidden=YES;
genderPickerView.hidden=YES;
activityPickerView.hidden=YES;
conditionPickerView.hidden=YES;
sizeandquantityPickerView.hidden=YES;
BrandLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 812, 253, 35)];
BrandLabelfield.textColor = [UIColor lightGrayColor];
BrandLabelfield.text=#"Brand";
BrandLabelfield.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"header_01.png"]];
BrandLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:BrandLabelfield];
//GenderTextfield with Label //
GenderLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 865, 253, 35)];
GenderLabelfield.textColor = [UIColor lightGrayColor];
GenderLabelfield.text=#"Gender";
GenderLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:GenderLabelfield];
//ColorTextfield with Label //
ConditionLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 1113, 253, 35)];
ConditionLabelfield.textColor = [UIColor lightGrayColor];
ConditionLabelfield.text=#"Color";
ConditionLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:ConditionLabelfield];
brands=[[NSArray alloc]initWithObjects:#"Nike",#"Bata",#"Adidas", nil];
gender=[[NSArray alloc]initWithObjects:#"Male",#"Female",#"Unisex", nil];
activity=[[NSArray alloc]initWithObjects:#"BASEBALL",#"BASKETBALL",#"FOOTBALL",#"GOLF",#"RUNNING",#"SKATING",#"SOCCER",#"SPORTS & OUTDOOR",#"TENNIS",#"TRAINING",#"VOLLEY BALL",#"WRESTLING", nil];
// Custom PICKER VIEWS //
pickerViewoption=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 852, 290, 41)];
pickerViewoption.showsSelectionIndicator = YES;
pickerViewoption.delegate = self;
pickerViewoption.dataSource=self;
pickerViewoption.hidden=YES;
[pickerViewoption setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:pickerViewoption];
genderPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 906, 290, 41)];
genderPickerView.showsSelectionIndicator = YES;
genderPickerView.delegate = self;
genderPickerView.dataSource=self;
genderPickerView.hidden=YES;
[genderPickerView setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:genderPickerView];
activityPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 954, 290, 41)];
activityPickerView.showsSelectionIndicator = YES;
activityPickerView.delegate = self;
activityPickerView.dataSource=self;
activityPickerView.hidden=YES;
[activityPickerView setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:activityPickerView];
pickerViewoption.tag=1;
genderPickerView.tag=2;
activityPickerView.tag=3;
BrandPickerBtn.tag=000;
genderPickerfBtn.tag=111;
activityPickerBtn.tag=222;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component{
NSString *string;
if (pickerViewoption.tag==1) {
[tabView1 addSubview:pickerViewoption];
string=[self.brands objectAtIndex:row];
}
else {
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (genderPickerView.tag==2){
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (activityPickerView.tag==3){
[tabView1 addSubview:activityPickerView];
string=[self.activity objectAtIndex:row];
}
return string;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (pickerViewoption.tag==1){
BrandLabelfield.text=#"";
BrandLabelfield.text=[brands objectAtIndex:row];
BrandLabelfield.textColor=[UIColor whiteColor];
pickerViewoption.hidden=YES;
}
else if (genderPickerView.tag==2){
GenderLabelfield.text=#"";
GenderLabelfield.text=[gender objectAtIndex:row];
GenderLabelfield.textColor=[UIColor whiteColor];}
else if (activityPickerView.tag==3){
ActivityLabelfield.text=#"";
ActivityLabelfield.text=[activity objectAtIndex:row];
ActivityLabelfield.textColor=[UIColor whiteColor];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if (pickerViewoption.tag==1)
{
return [brands count];
}
else if (genderPickerView.tag==2)
{
return [gender count];
}
else if (activityPickerView.tag==3)
{
return [activity count];
}
return 0;
}
- (IBAction)action:(id)sender; {
if (isCheckedpickerView==FALSE)
{
pickerViewoption.hidden=NO;
isCheckedpickerView=TRUE;
}
else
{
isCheckedpickerView=FALSE;
pickerViewoption.hidden=YES;
}
}
- (IBAction)genderAction:(id)sender {
if (isCheckedpickerView==FALSE)
{
genderPickerView.hidden=NO;
isCheckedpickerView=TRUE;
}
else
{
isCheckedpickerView=FALSE;
genderPickerView.hidden=YES;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Currently you are checking wrong condition in delegate methods, replace following 3 methods in .m file :
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component{
NSString *string;
if (pickerView.tag==1) {
[tabView1 addSubview:pickerViewoption];
string=[self.brands objectAtIndex:row];
}
else if (pickerView.tag==2){
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (pickerView.tag==3){
[tabView1 addSubview:activityPickerView];
string=[self.activity objectAtIndex:row];
}
else {
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
return string;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (thePickerView.tag==1){
BrandLabelfield.text=#"";
BrandLabelfield.text=[brands objectAtIndex:row];
BrandLabelfield.textColor=[UIColor whiteColor];
pickerViewoption.hidden=YES;
}
else if (thePickerView.tag==2){
GenderLabelfield.text=#"";
GenderLabelfield.text=[gender objectAtIndex:row];
GenderLabelfield.textColor=[UIColor whiteColor];
}
else if (thePickerView.tag==3){
ActivityLabelfield.text=#"";
ActivityLabelfield.text=[activity objectAtIndex:row];
ActivityLabelfield.textColor=[UIColor whiteColor];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if (pickerView.tag==1)
{
return [brands count];
}
else if (pickerView.tag==2)
{
return [gender count];
}
else if (pickerView.tag==3)
{
return [activity count];
}
return 0;
}

How do I add a UIPickerView when tapping inside a text field?

I have this code to add a datepicker:
// Initiate datepicker and assign to due date
_datePicker = [[UIDatePicker alloc] init];
[_datePicker addTarget:self action:#selector(dateChanged:)
forControlEvents:UIControlEventValueChanged];
_datePicker.minuteInterval = 15;
_dueDate.inputView = _datePicker;
I tried to do the same thing with a pickerview but I had no success, could somebody explain why?
Here is my code:
// Initaite the pickerview and assign to priority text field
_priorityPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
// Array otions for priority picker
NSArray *array = [[NSArray alloc] initWithObjects:#"High",#"Medium",#"Low", nil];
self.priorityPickerData = array;
// Connect text field to pickerview
_priority.inputView = _priorityPickerView;
Here is the data set in the picker view:
// Picker View functions
-(NSInteger)numberOfComponantsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [_priorityPickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.priorityPickerData objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
int select = row;
if(select == 0){
_priority.text = #"High";
}else if(select == 1){
_priority.text = #"Medium";
}else if(select == 2){
_priority.text = #"Low";
}
}
Many thanks in advance,
Peter
Set the delegate and datasource to self:
_priorityPickerView.delegate = self;
_priorityPickerView.dataSource = self;
Also at the top of your .m file:
#interface YourViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

UIPickerView not working in sub UIView

I have a UIPickerView that works perfectly when placed on the main view. However as soon as I place it into a sub UIView (To help with visual placement of elements) the interaction stops completely. The UIPickerView still receives all data correctly, just no interaction.
If I take the UIPickerView back out of the UIView, using storyboard, it works right away.
- (void)viewDidLoad
{
[super viewDidLoad];
// Add provinces into array
[self.provinceView setHidden:YES];
isVisible = NO;
self.provinceArray = [[NSArray alloc] initWithObjects:#"Alberta",#"British Columbia",#"Manitoba",#"New Brunswick",#"Newfoundland and Labrador",#"Northwest Territories",#"Nova Scotia",#"Nunavut",#"Ontario",#"Prince Edward Island",#"Quebec",#"Saskatchewan",#"Yukon",nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.provinceArray count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0,(pickerView.frame.size.width-40), 44)];
label.textColor = [UIColor colorWithRed:0.035 green:0.094 blue:0.345 alpha:1.0];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
label.text = [self.provinceArray objectAtIndex:row];
return label;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.provinceLabel.text = [self.provinceArray objectAtIndex:row];
}
Check this post
-->
UIPickerView in UITableView doesn't react to touch in lower half of cell
did u bring the subview to foreground?

Can't find the cause of my app crashing

I am working on a UIPickerController. I have everything working but when I tap the back button to go to my previous view the app is crashing.
I am thinking it could be catching a memory leak.
Here is my code. If anyone sees something I have missed or may have a clue on what cause this please inform me. Nothing displays in the Debugger.
#synthesize colorTextField, pickerView, pickerToolbar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[colorTextField release];
[pickerView release];
[pickerToolbar release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
colorsArray = [[NSMutableArray alloc] init];
[colorsArray addObject:#"Red"];
[colorsArray addObject:#"Orange"];
[colorsArray addObject:#"Yellow"];
[colorsArray addObject:#"Green"];
[colorsArray addObject:#"Blue"];
[colorsArray addObject:#"Indigo"];
[colorsArray addObject:#"Violet"];
}
- (void)setColor
{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, actionSheet.bounds.size.width, 44)];
[pickerToolbar setBarStyle:UIBarStyleBlack];
[pickerToolbar sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(pickerDoneClicked)];
[pickerToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
[doneButton release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
- (void)pickerDoneClicked
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self setColor];
return NO;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [colorsArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [colorsArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Color: %#. Index of selected color: %i", [colorsArray objectAtIndex:row], row);
[colorTextField setText:(NSString *)[colorsArray objectAtIndex:row]];
}
- (void)viewDidUnload
{
[self setColorTextField:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
You're double-releasing pickerView and pickerToolbar, once in setColor and again in dealloc.
A good pattern when releasing views held in ivars is to set the ivar to nil after releasing the view, this will prevent double-releases.
I think your release some of your items mor ethan once
EG:
in the dealloc you have two
[pickerView release];
[pickerToolbar release];
that are release already
eg:
[actionSheet addSubview:pickerView];
[pickerView release];
so it has nothing to release me thinks, so remove the duplicate releases??

Resources