UIDatePicker non-continuous minutes - iphone

I set my UIDatepicker timeInterval to 30 mins, so it shows it as 30,60 (repeating). I really don't want it to repeat, just show 2 options (like AM/PM). Is there a way to do this without creating it from scratch?
Thanks!

Why don't you use UIPicker instead?
UIPickerView *pick=[[UIPickerView alloc] initWithFrame:CGRectMake(300, datep.frame.origin.y, datep.frame.size.width, datep.frame.size.height)];
[pick setDelegate:self];
[pick setDataSource:self];
[pick setShowsSelectionIndicator:YES];
[self.view addSubview:pick];
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component==0){
return 12;
}else{
return 2;
}
return 2;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component==1){
if(row==1)
return #"30";
else
return #"00";
}
else if(component==0){
return [NSString stringWithFormat:#"%d", row+1];
}else{
if(row==1)
return #"AM";
else
return #"PM";
}
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 50;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 50;
}

Related

How to make my UIPickerView display all elements of an array

I have a UIPickerView and an array though I can't seem to be able to input all the date from the array into the UIPicker.
I know that the syntax is meant to be like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
Where treatments is the array name though when I use this it comes up with this error:
-[Treatment isEqualToString:]: unrecognized
I have searched my entire project and can't find the phrase: isEqualToString
Treatment.h file:
#import <Foundation/Foundation.h>
#interface Treatment : NSObject {
NSString *treatmentid;
NSString *treatmentName;
NSString *treatmentPrice;
}
#property (nonatomic, strong) NSString *treatmentid;
#property (nonatomic, strong) NSString *treatmentName;
#property (nonatomic, strong) NSString *treatmentPrice;
#end
All picker codes:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [treatments count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
If you need any more code just say
Thanks in advance
you your Array contain Dictionar or NSMutableDictionar then you must specify value of array with it's key like bellow try to writtern you method like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[treatments objectAtIndex:row]valueForKey:#"YourKey"];
}
Here is basic Implementation of Picker view sample example code:-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thepickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thepickerView numberOfRowsInComponent:(NSInteger)component
{
return [treatments count];
}
customize displaying lable of UIpickerview like bellow:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
if (component == 0) {
label.font=[UIFont boldSystemFontOfSize:22];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:#"%d", row];
label.font=[UIFont boldSystemFontOfSize:22];
NSLog(#"%#",[yourpickerview selectedRowInComponent:component]);
}
return label;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"%#",[treatments objectAtIndex:row]);
}

How do I open one of the many View controllers on selection of a UIPickerPicker view

I am creating an app in IOS5 using storyboards. I created a UiPickerView and when an option is selected I want to open one of the many UIViewControllers. I just want the user to select which view controller he wants to use. How do I go about connecting multiple view controllers.
Thanks
Prerna
Please use the following delegates and data sources for your picker view
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if(button_Number == 1)
{
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 30)];
NSString *nameStr=[arr_countryName objectAtIndex:row];
// NSString *nameStr=[programNameArr objectForKey:#"programname"];
label.text = nameStr;
label.font = [UIFont boldSystemFontOfSize:14.0f];
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
}
if (button_Number == 2)
{
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 30)];
NSString *nameStr=[arr_currencyCode objectAtIndex:row];
// NSString *nameStr=[programNameArr objectForKey:#"programname"];
label.text = nameStr;
label.font = [UIFont boldSystemFontOfSize:18.0f];
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//return (NSString*)[clientListArray objectAtIndex:row];
if(button_Number == 1)
{
return (NSString*)[arr_countryName objectAtIndex:row];
}
if (button_Number == 2)
{
return (NSString*)[arr_currencyCode objectAtIndex:row];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(button_Number == 1)
{
return [arr_countryName count];
}
if (button_Number == 2)
{
return [arr_currencyCode count];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
selectedScrollIndex = row;
// clientNameTxtFld.text = [clientListArray objectAtIndex:row];
// LBL.text = [clientListArray objectAtIndex:row];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
}
if (buttonIndex == 1 && button_Number == 1)
{
countryTxtFld.text = [arr_countryName objectAtIndex:selectedScrollIndex];
//selected_Client_ID = [clientIDArray objectAtIndex:selectedScrollIndex];
selectedScrollIndex = 0;
}
if (buttonIndex == 1 && button_Number == 2)
{
currencyTxtFld.text = [arr_currencyCode objectAtIndex:selectedScrollIndex];
//selected_Client_ID = [clientIDArray objectAtIndex:selectedScrollIndex];
selectedScrollIndex = 0;
}
}
Create instance for all the view controllers in .h file and set the tag value for all of them as per the sequence of row-touch-title array in UIPickerView.
Once this is done, inherit UIPickerViewDelegate and implement required delegate method.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row
inComponent:(NSInteger)component {
// Handle the selection
switch (row) {
case 0:
[self presentModalViewController:viewController1 animated:YES];
break;
case 1:
[self presentModalViewController:viewController2 animated:YES];
break;
case 2:
[self presentModalViewController:viewController3 animated:YES];
break;
}
}

Double picker view

Hello im working with a double picker view with images, one side text and the other images, but I cant make it work. Here is the picker components. There are 3 arrays, Array and Array1 content text, and ImagesArray content images. X its a variable that depends on what button you pressed. Thanks.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
if (X == 1) {
if (component == 0) {
return [self.Array count];
}
if (component == 1)
{
return [self.ImagesArray count];
}
}
if (X == 2) {
if (component == 0) {
return [self.Array1 count];
}
if (component == 1) {
return [self.ImagesArray count];
}
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
if (component == 1){
return [self.ImagesArray objectAtIndex:row];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (X == 1){
if (component == 0)
{
return [self.Array objectAtIndex:row];
}
}
if (X == 2) {
if (component == 0){
return [self.Array1 objectAtIndex:row];
}
}
}
Let me point out a few issues with your code.
You have a ; at the end of your numberOfRows method before starting the method block. That won't work.
You are using capitalized first letters for instance variables - bad idea.
You are not using else clauses, causing tests to be run when they are no longer necessary.
The way you nest your if statements also creates repetitive code.
You are probably not seeing any images, because of what is in your images array. This array, the way you are using it, should contain UIImageViews. However, this would defeat the purpose of reusable views. Rather, you should have an array with images or images names and create the view only if necessary.
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view{
if (component!=1) return nil;
UIImageView *imageView = nil;
UIImage *image = [UIImage imageNamed:[self.imagesArray objectAtIndex:row]];
if (view==nil) {
imageView = [[UIImageView alloc] initWithImage:image];
}
else {
imageView = (UIImageView *)view;
imageView.image = image;
}
view = imageView;
return view;
}
If it still does not work, please be more specific about what is not working.

Getting a "?" when populating my pickerview on load

I've searched here, but didn't see nothing like this error.
I'm trying to populate my pickerview on ViewDidLoad, but when I run my program, the pickerView is just populated with "?" instead my strings... what's going on here? Thanks in advance.
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping", #"eating", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:#"awsome",#"sad",#"happy",#"ambivalent",#"nauseous",#"psyched",#"confused",#"hopeful",#"anxious",nil];
}
You need to implement the UIPickerViewDelegate and UIPickerViewDataSource protocols:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0)
return [activities count];
else
return [feelings count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
return [activities objectAtIndex:row];
else
return [feelings objectAtIndex:row];
}

How to give this picker view a datasource

Here I am adding a pickerview programaticly
- (void)viewDidLoad {
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,280,321,200);
UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure
//Set up the picker view as you need it
//Set up the display frame
myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions
//Add the picker to the view
[self.view addSubview:myPickerView];
}
But now I need to actually have it display content and somehow find out when it changes and what value it has changed to. How do I do this?
in .h file place this code
#interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
assign the datasource and delegate to the picker
// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;
use the following delegate and datasouce methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = #"";
if (pickerView == myPickerView)
{
returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:#"nodeContent"];
}
return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == myPickerView)
{
return [levelPickerViewArray count];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
You need to create a class that implements the UIPickerViewDataSource protocol and assign an instance of it to myPickerView.dataSource.