I have one application in which when user clicks a button i want to open uipickerview as as subview to main view and after user selects an item it should be removed from main view.(drop down kind of functionality).For that i have written code as below:
-(void)showPrefPicker:(id)sender
{
UIView *subView=[[UIView alloc] init];
subView.frame=CGRectMake(180, 120, 150, 150);
pickerView = [[UIPickerView alloc] init];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.frame=CGRectMake(190, 130, 100, 100);
subView.backgroundColor=[UIColor blackColor];
[subView addSubview:pickerView];
[self.view addSubview:subView];
[pickerView release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selected object is %#",[arraypref objectAtIndex:row]);
//[pickerView ]
//mlabel.text= [arrayNo objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arraypref count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arraypref objectAtIndex:row];
}
but is only showing subview in the main view not the pickerview.how can i do that any tutorial or source code for that?
Better you use
to hide
pickerview.hidden=YES;
to show
pickerview.hidden=NO;
Try changing the frame of the pickerView as
pickerView.frame = CGRectMake(10,10,100,100);
in your code subView.frame=CGRectMake(180, 120, 150, 150);
and pickerView.frame=CGRectMake(190, 130, 100, 100);
picker view is a subview of your 'subView'
here pickerview's frame starts out of bounds of subview ie origin x is 190. but the width of the subview is only 150.
so the correct code is
pickerView.frame=CGRectMake(0, 0, 100, 100);
since pickerview is a subview of a custom view. it should be in the frame of its parent view.
Related
I m new to objective-C and I need to create a UIPickerView when a particular textfield is touched that would be populated with the items of NSMutableArray..I have implemented this code but couldnot see the pickerView addded up.I can still see the keyboard popping up when a textfield is touched...Where Am I gng wrong..???
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
if( textField == txtstate)
{
[txtstate resignFirstResponder];
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[pickerView selectRow:1 inComponent:0 animated:NO];
txtstate.text= [[arr objectAtIndex:row] valueForKey:#"Code"];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arr count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arr objectAtIndex:row];
}
My Array looks like this :
(
{
Code = Wisconsin;
Id = "6215b339-2959-4a3d-9430-f5bddb0a4eb9";
},
{
Code = Wyoming;
Id = "7972f963-9b3b-4ba6-a30c-d68bb9f59145";
}
)
Any help would be appreciated...
u can add button on UITextField and add your picker at that button action .
make sure your button should be custom type button and put that button over text field.
if you not give the delegate to UITextField name txtstate then give the delegate like bellow in viewDidLoad: method
txtstate.delegate = self;
I have implemented the UIPicker view concept on action sheet, it is working properly but one thing it is not displaying properly that is row index. There is no highlighted row in the picker view.
I have uploaded the image. Please give me the solution for it.
Following is the code which I am using:
-(IBAction)clickformedtype:(id)sender
{
UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:#"Select medication type" delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select", nil];
[asheet showInView:[self.view superview]]; //note: in most cases this would be just self.view, but because I was doing this in a tabBar Application, I use the superview.
[asheet setFrame:CGRectMake(0, 117, 320, 383)];
[asheet release];
}
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {
medicationtypepicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
medicationtypepicker.delegate = self;
medicinetypearray = [[NSMutableArray alloc]initWithObjects:#"ml",#"capsules",#"eyedrops", nil];
//medicationtypepicker = [[NSMutableArray alloc]initWithObjects:#"ml",#"capsules",#"eyedrops", nil];
//medicationtypepicker.datePickerMode = ui;
//Configure picker...
// [medicationtypepicker setMinuteInterval:1];
[medicationtypepicker setTag: kDatePickerTag];
//Add picker to action sheet
[actionSheet addSubview:medicationtypepicker];
// [medicationtypepicker release];
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
UIPickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [medicinetypearray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [medicinetypearray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
medicinetype.text=[medicinetypearray objectAtIndex:row];
//medicationtypepicker.hidden = YES;
}
Thanks in advance.
UIPickerView has a property called "showsSelectionIndicator" - set this to YES also set autoresizingMask to UIViewAutoresizingFlexibleWidth as follows...
mTimePicker.showsSelectionIndicator = YES;
mTimePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Try to set property showsSelectionIndicator to YES:
medicationtypepicker.showsSelectionIndicator = YES;
You can scroll an UIPickerView both by swiping and by tapping items under or above the selection belt.
You can populate an UIPickerView by either specifying NSString as titles or reusable UIViews for each row.
What I've noticed is that when I switched from providing strings to providing view, tap to scroll no longer worked. Is this expected behavior. Should my views forward some touch events to the UIPickerView?
Thanks
EDIT:
Here's a code sample from my implementation:
// Creating the picker
screenPicker_ = [UIPickerView new];
screenPicker_.showsSelectionIndicator = YES;
screenPicker_.delegate = delegate_;
screenPicker_.dataSource = delegate_;
[self addSubview:screenPicker_];
[screenPicker_ release];
// Row view creation delegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
PickerRowView *pickerRowView = (PickerRowView*)view;
if(view == nil)
{
pickerRowView = [[PickerRowView alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, PICKER_ROW_HEIGHT)];
}
[pickerRowView SetTitle:#"some title"];
return pickerRowView;
}
// initailizer of the PickerRowView class (an UIView subclass)
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
CGFloat titleHeight = frame.size.height * CONTENT_TO_FRAME_RATIO;
title_ = [[UILabel alloc] initWithFrame:CGRectMake(TITLE_X, (frame.size.height - titleHeight) / 2, frame.size.width, titleHeight)];
[title_ setFont:[UIFont fontWithName:#"StainlessExt-Light" size:titleHeight]];
title_.backgroundColor = [UIColor clearColor];
[self addSubview:title_];
[title_ release];
self.userInteractionEnabled = NO;
}
return self;
}
Setting userInteractionEnabled property to NO for your row views must solve your problem.
In your PickerRowView class, define the following method:
- (void)didMoveToSuperview {
if ([[self superview] respondsToSelector:#selector(setShowSelection:)])
{
[[self superview] performSelector:#selector(setShowSelection:) withObject:NO];
} }
and that should get rid of the highlight
I'm trying to add a UIPicker as a subview, but can't quite figure it out. I've added a second UIView control with a UIPickerview to my nib (NewWorkoutViewController.xib). My code is as follows:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [list count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"Selected item: %# index of selected item: %i", [list objectAtIndex:row], row);
}
-(void)viewDidLoad
{
[super viewDidLoad];
counterInt = 0;
list = [[NSMutableArray alloc] init];
[list addObject:#"red"];
[list addObject:#"blue"];
[list addObject:#"yellow"];
[list addObject:#"pink"];
}
-(IBAction)routePicker
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,216)];
[myView addSubview:thePickerView];
[self.view.superview addSubview:myView];
}
My subview is not popping up as expected.
It is not clear why do you add the picker in code if you've already added it in IB... Regarding the code you posted, try to add picker to controller's view, not to the superview (and do not forget to set its delegate):
-(IBAction)routePicker
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,216)];
thePickerView.delegate = self;
[myView addSubview:thePickerView];
[self.view addSubview:myView];
}
Edit: You initialize you myView instance with a frame that is likely to be outside of the visible area (frame's origin y coordinate is 480), try to set it to 0:
// Bad
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
// Good?
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
Check out this example from Apple
Date Cell Picker
I have the following code to create a UIPickerView:
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f, 416.0f - height, 320.0f, height)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[pickerView setSoundsEnabled:YES];
I would like to change the component widths and change the text size in each component. Is it possible to do this?
Thanks!
You can change the width by an appropriate delegate method
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
switch(component) {
case 0: return 22;
case 1: return 44;
case 2: return 88;
default: return 22;
}
//NOT REACHED
return 22;
}
As for a custom text size, you can use the delegate to return custom views with whatever sized text you want:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
}
retval.text = #"Demo";
retval.font = [UIFont systemFontOfSize:22];
return retval;
}
Of course you will need modify these to have appropriate values for your app, but it should get you where you need to go.