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.
Related
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;
}
}
I Learned how to use Picker views, singles and doubles. I would like to learn how to do a Picker view with images. Instead of numbers or words the scroll have different images to select one. Thanks. I think is using this code but it osent run me, I cant find the mistake. Thanks for your help.
#synthesize picker, Array, image;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [self.Array count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.Array objectAtIndex:row];
}
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view
{
NSString *img_src = [NSString stringWithFormat:#"6-12AM.png", row];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]];
return image;
}
EDIT: I confused the function you need to implement in my initial answer. Sorry for that.
You need to implement the
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
method of your UIPickerView delegate to return an UIImageView for a given row.
Reference page here.
for example (very dumb implementation without reusing views)
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSString * img_src = [NSString stringWithFormat:#"image_%d.png", row];
UIImageView * retval = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]] autorelease];
return retval;
}
assuming you have images named image_[num].png in your resources.
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;
}
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];
}
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.