How to connect UIPickerView datasource and delegate in storyboard - iphone

I have created a new project in Ios5 using storyboards and my project has a UIPickerView.
I created a datasource and delegate and I cannot find the files owner to make connections in IOs5 . What should I make the connections to?

One difference between storyboards and loading .xib files is that the view controller for each "scene" in a storyboard file is included in that file. So, assuming that your view controller is also the data source and delegate for your picker (this is common), connect the view controller to those outlets instead of the file's owner proxy. (With .xib files, it's typical to instantiate the view controller in code and give it a .xib file from which to load its view, so the view controller is the file's owner.)

- (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;
}
}

Related

UISearchBar same as like iMac Finder Search Functionality

I met with an requirement in which I had UISearchBar as like follows.
Step : 1 (Initial look of the SearchBar)
Step : 2 (User types string and immediate Search)
Step : 3 (Selecting any of them in the Search List)
Step : 4 (Adding the button on the SearchBar)
Step : 5 (Finally Action on the Button)
This might continue. I mean to say is he can enter the text further more but the functionality should be the same.
Could anyone please help me ? Please, I am in a need.
UPDATE
You can watch this same functionality on your iMac Finder Search
This is what i have tried.
#import "ViewController.h"
#import "customPopOverController.h"
#import <QuartzCore/QuartzCore.h>
#interface ViewController ()
#property (strong, nonatomic) UIView *searchView;
#property (strong, nonatomic) UITableView *sampleView;
#property (strong, nonatomic) NSMutableArray *arrayForListing;
#property (strong, nonatomic) UITextField *txtFieldSearch;
#end
#implementation ViewController
#synthesize searchView = _searchView;
#synthesize customPopOverController = _customPopOverController;
#synthesize txtFieldSearch = _txtFieldSearch;
#synthesize sampleView = _sampleView;
#synthesize arrayForListing = _arrayForListing;
#synthesize btnforExtra = _btnforExtra;
- (void)viewDidLoad
{
[super viewDidLoad];
self.arrayForListing = [[NSMutableArray alloc]init];
[self loadTheSearchView];
[self applyUIStyle];
}
- (void)loadTheSearchView
{
self.searchView = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 280, 44)];
[self.searchView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:self.searchView];
[self placeTheTextView];
}
- (void)placeTheTextView
{
self.txtFieldSearch = [[UITextField alloc]initWithFrame:CGRectMake(25, 9, 230, 30)];
[self.txtFieldSearch setDelegate:self];
[self.searchView addSubview:self.txtFieldSearch];
}
- (void)applyUIStyle
{
self.searchView.layer.cornerRadius = 20.0f;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length!=0)
{
[self.arrayForListing removeAllObjects];
[self.arrayForListing addObject:[NSString stringWithFormat:#"File Contains %#",textField.text]];
[self callThePop];
}
return YES;
}
- (void)callThePop
{
UIViewController *contentViewController = [[UIViewController alloc] init];
contentViewController.contentSizeForViewInPopover = CGSizeMake(self.searchView.frame.size.width, 50);
self.sampleView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, contentViewController.contentSizeForViewInPopover.width, contentViewController.contentSizeForViewInPopover.height)];
[self.sampleView setDelegate:self];
[self.sampleView setDataSource:self];
[self.sampleView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];
[self.sampleView setBackgroundColor:[UIColor clearColor]];
[contentViewController.view addSubview:self.sampleView];
self.customPopOverController = [[customPopOverController alloc]initWithContentViewController:contentViewController];
self.customPopOverController.delegate = self;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(callYourMethod:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.sampleView addGestureRecognizer:swipeRight];
[self.customPopOverController presentPopoverFromRect:self.searchView.frame inView:self.view permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown| UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight) animated:YES];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table View Delegate Methods
#pragma mark
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayForListing.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifer = #"Cell";
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
}
cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.textLabel setTextAlignment:UITextAlignmentCenter];
[cell.textLabel setNumberOfLines:0];
[cell.textLabel setLineBreakMode:UILineBreakModeCharacterWrap];
cell.textLabel.text = [self.arrayForListing objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont fontWithName:#"TrebuchetMS" size:14]];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.customPopOverController)
{
[self.customPopOverController dismissPopoverAnimated:YES];
}
UITableViewCell * cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if (self.txtFieldSearch.text.length == 0)
{
self.txtFieldSearch.text = cell.textLabel.text;
}
else
{
self.btnforExtra = [[UIButton alloc]initWithFrame:CGRectMake(10+(buttonsCount*45), 8, 45, 25)];
[self.btnforExtra setBackgroundColor:[UIColor colorWithRed:0.503 green:0.641 blue:0.794 alpha:1.000]];
[self.btnforExtra setTitle:self.txtFieldSearch.text forState:UIControlStateNormal];
[self.btnforExtra setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.btnforExtra.layer setCornerRadius:8.0f];
[self.txtFieldSearch setFrame:CGRectMake(self.btnforExtra.frame.origin.x+self.btnforExtra.frame.size.width+10, 9, 230, 30)];
self.txtFieldSearch.text = #"";
[self.searchView addSubview:self.btnforExtra];
buttonsCount++;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.editing == UITableViewCellEditingStyleDelete)
{
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.arrayForListing removeObjectAtIndex:indexPath.row];
[tableView endUpdates];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"IndexPath.row == %i", indexPath.row);
}
#pragma mark - custom PopOver Delegate Methods
#pragma mark
- (BOOL)popoverControllerShouldDismissPopover:(customPopOverController *)thePopoverController
{
return YES;
}
- (void)popoverControllerDidDismissPopover:(customPopOverController *)thePopoverController
{
self.customPopOverController = nil;
}
#end
DISCLAIMER: this solution is deeply flawed, probably in many ways. It's the first solution that I came up with and I've done VERY little optimization on it (aka none).
So, I was bored, and I came up with something that I think fits the bill. Keep in mind that this only does the display of what you're asking about (and it does it somewhat poorly at that), it does not handle the actual search functionality as I'm not sure what you're searching. It also has some static values programmed in (such as file types array, and I dropped the view controller into a UINavigationController and did some static sizes based on that) that you'll want to change. I make use of WYPopoverController, which can be found here: WYPopoverController. Let me know how it works for you- feel free to critique the hell out if it.
ViewController.h
#interface ViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>
{
}
ViewController.m
#import "ViewController.h"
#import "WYPopoverController.h"
#import "SearchButton.h"
#import <QuartzCore/QuartzCore.h>
#interface ViewController () <WYPopoverControllerDelegate>
{
UITextField *searchField;
UIView *searchesView;
UIScrollView *scrollView;
WYPopoverController *newSearchController;
WYPopoverController *existingSearchController;
NSMutableArray *buttonsArray;
SearchButton *activeButton;
NSArray *kindsArray;
NSMutableArray *matchedKinds;
// using UITableViewController instead of just UITableView so I can set preferredContentSize on the controller
UITableViewController *searchTable;
UITableViewController *existingTable;
}
#end
#implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
searchField = [[UITextField alloc] initWithFrame:CGRectMake(20, 75, 280, 30)];
searchField.borderStyle = UITextBorderStyleRoundedRect;
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.delegate = self;
[self.view addSubview:searchField];
searchesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, searchField.frame.size.height)];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, searchField.frame.size.height)];
[scrollView addSubview:searchesView];
searchField.leftView = scrollView;
searchField.leftViewMode = UITextFieldViewModeAlways;
[searchField becomeFirstResponder];
kindsArray = [NSArray arrayWithObjects:#"Audio", #"Video", #"Other", #"Text", nil];
matchedKinds = [[NSMutableArray alloc] init];
buttonsArray = [[NSMutableArray alloc] init];
[searchField addTarget:self
action:#selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Text Field Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[newSearchController dismissPopoverAnimated:YES completion:^{
[self popoverControllerDidDismissPopover:newSearchController];
}];
return YES;
}
// not technically part of the TF delegate, but it fits better here
- (void)textFieldDidChange:(UITextField *)tf
{
if(tf.text.length > 0)
{
if(newSearchController == nil)
{
searchTable = [[UITableViewController alloc] init];
searchTable.tableView.delegate = self;
searchTable.tableView.dataSource = self;
searchTable.preferredContentSize = CGSizeMake(320, 136);
newSearchController = [[WYPopoverController alloc] initWithContentViewController:searchTable];
newSearchController.delegate = self;
newSearchController.popoverLayoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);
newSearchController.theme.arrowHeight = 5;
searchTable.tableView.tag = 1;
[newSearchController presentPopoverFromRect:searchField.bounds
inView:searchField
permittedArrowDirections:WYPopoverArrowDirectionUp
animated:YES
options:WYPopoverAnimationOptionFadeWithScale];
}
[matchedKinds removeAllObjects];
for(NSString *type in kindsArray)
{
NSRange foundRange = [[type lowercaseString] rangeOfString:[tf.text lowercaseString]];
if(foundRange.location != NSNotFound)
{
// Found a match!
[matchedKinds addObject:type];
}
}
[searchTable.tableView reloadData];
}
else
{
[newSearchController dismissPopoverAnimated:YES completion:^{
[self popoverControllerDidDismissPopover:newSearchController];
}];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView.tag == 1)
return 2;
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView.tag == 1)
{
if(section == 0)
return 1;
return [matchedKinds count];
}
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView.tag == 1)
{
if(section == 0)
return #"Filenames";
else
return #"Kinds";
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"reuser"];
// Configure the cell...
if(tableView.tag == 1)
{
if(indexPath.section == 0)
{
cell.textLabel.text = [NSString stringWithFormat:#"Name matches: %#", searchField.text];
}
else
{
cell.textLabel.text = [matchedKinds objectAtIndex:indexPath.row];
}
}
else
{
if(indexPath.row == 0)
{
switch (tableView.tag) {
case 2:
cell.textLabel.text = #"Filename";
break;
default:
cell.textLabel.text = #"Kind";
break;
}
}
else if(indexPath.row == 1)
cell.textLabel.text = #"Everything";
else
cell.textLabel.text = #"<Delete>";
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.tag == 1) // new search table tapped
{
SearchButton *searchedButton = [SearchButton buttonWithType:UIButtonTypeSystem];
if(indexPath.section == 0)
{
searchedButton.type = ButtonTypeName;
searchedButton.searchedString = searchField.text;
}
else
{
searchedButton.type = ButtonTypeKind;
searchedButton.searchedString = [matchedKinds objectAtIndex:indexPath.row];
}
searchedButton.defaulted = YES;
searchedButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
[searchedButton setTitleColor:[UIColor darkTextColor]
forState:UIControlStateNormal];
[searchedButton addTarget:self
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[searchedButton setBackgroundColor:[UIColor colorWithWhite:0.9f alpha:1.0f]];
searchedButton.layer.cornerRadius = 5.0f;
searchedButton.clipsToBounds = YES;
[buttonsArray addObject:searchedButton];
activeButton = searchedButton;
searchField.text = #"";
[self updateButtonsFromCreation:YES];
[newSearchController dismissPopoverAnimated:YES completion:^{
[self popoverControllerDidDismissPopover:newSearchController];
}];
}
else // text field of an existing search
{
switch (indexPath.row) {
case 0:
activeButton.defaulted = YES;
break;
case 1:
activeButton.defaulted = NO;
break;
default:
[buttonsArray removeObject:activeButton];
break;
}
[self updateButtonsFromCreation:NO];
[existingSearchController dismissPopoverAnimated:YES completion:^{
[self popoverControllerDidDismissPopover:existingSearchController];
}];
}
}
#pragma mark - Popover delegate
- (void)popoverControllerDidDismissPopover:(WYPopoverController *)controller
{
if(controller == newSearchController)
{
newSearchController = nil;
}
else
{
existingSearchController = nil;
}
}
#pragma mark - Other functions
- (void)updateButtonsFromCreation:(BOOL)creation
{
[[searchesView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
float widthTotal = 0;
for(SearchButton *button in buttonsArray)
{
NSString *buttonText;
if(button.defaulted)
{
if(button.type == ButtonTypeName)
buttonText = #"Name: ";
else
buttonText = #"Kind: ";
}
else
buttonText = #"Any: ";
buttonText = [buttonText stringByAppendingString:button.searchedString];
[button setTitle:buttonText forState:UIControlStateNormal];
CGSize buttonSize = [buttonText sizeWithAttributes:#{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];
button.frame = CGRectMake(widthTotal + 2, 2, buttonSize.width + 4, searchField.frame.size.height - 4);
widthTotal += button.frame.size.width + 2;
[searchesView addSubview:button];
}
searchesView.frame = CGRectMake(0, 0, widthTotal, searchesView.frame.size.height);
scrollView.frame = CGRectMake(0, 0, ((widthTotal > 200) ? 200 : widthTotal), scrollView.frame.size.height);
scrollView.contentSize = CGSizeMake(widthTotal, scrollView.frame.size.height);
if(creation)
{
scrollView.contentOffset = CGPointMake(activeButton.frame.origin.x, 0);
}
}
- (void)buttonTapped:(SearchButton *)sender
{
activeButton = sender;
existingTable = [[UITableViewController alloc] init];
existingTable.tableView.delegate = self;
existingTable.tableView.dataSource = self;
existingTable.preferredContentSize = CGSizeMake(160, 90);
existingSearchController = [[WYPopoverController alloc] initWithContentViewController:existingTable];
existingSearchController.delegate = self;
existingSearchController.popoverLayoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);
existingSearchController.theme.arrowHeight = 5;
existingTable.tableView.tag = sender.type;
[existingSearchController presentPopoverFromRect:sender.frame
inView:scrollView
permittedArrowDirections:WYPopoverArrowDirectionUp
animated:YES
options:WYPopoverAnimationOptionFadeWithScale];
}
#end
SearchButton.h
typedef enum {ButtonTypeName = 2,
ButtonTypeKind = 3} ButtonType;
#interface SearchButton : UIButton
#property (nonatomic) ButtonType type;
#property (nonatomic) BOOL defaulted;
#property (nonatomic, retain) NSString *searchedString;
#end
SearchButton.m
no changes to default generated

Pickerview value on uitabelview cell

In my application i have implement the functionality as follows,Tabelview cell contains two button on click it shows the picker view,When i select picker value its not reflecting on uitabelview corresponding cell.label, here the code.
Tableview cell
if(tableView.tag==25)
{
static NSString *simpleTableIdentifier = #"ordercel";
ordercel *cell = (ordercel *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ordercel" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.pimg.image=[arr objectAtIndex:indexPath.row];
cell.sizlb.text=#"45";
cell.qtylb.text=[qtyary objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = indexPath.row+2000;
[button addTarget:self action:#selector(BtnPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor=[UIColor clearColor];
button.frame = CGRectMake(130, 20 , 70, 35);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.tag = indexPath.row+4000;
[button1 addTarget:self action:#selector(sizPressed:) forControlEvents:UIControlEventTouchUpInside];
button1.backgroundColor=[UIColor clearColor];
button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button1.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button1.frame = CGRectMake(230, 20 , 70, 35);
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:button1];
return cell;
}
Button method
-(void)BtnPressed:(UIButton*)sender
{
[self createActionSheet];
pickerType = #"qtypicker";
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.tag=sender.tag-2000;
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
}
Picker view code
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([pickerType isEqualToString:#"qtypicker"])
{
counti = [array count];
}
if ([pickerType isEqualToString:#"sizepicker"])
{
counti = [array1 count];
}
return counti;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"qtypicker"])
{
string = [array objectAtIndex:row];
}
if ([pickerType isEqualToString:#"sizepicker"])
{
string = [array1 objectAtIndex:row];
}
return string;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
UITableViewCell *owningCell = (UITableViewCell*)[thePickerView superview];
NSIndexPath *pathToCell = [tabvu indexPathForCell:owningCell];
int index1=thePickerView.tag;//= thePickerView.tag-2000;
if ([pickerType isEqualToString:#"qtypicker"])
{
UILabel *obj=((UILabel *)[self.view viewWithTag:index1+1000]);
obj.text = [array objectAtIndex:row];
[qtyary addObject:obj.text];
[tabvu reloadData];
}
if ([pickerType isEqualToString:#"sizepicker"])
{
}
}
How to get the selected picker value on selected cell.What change should i made in my code?Please help me to sort it out
Your code is difficult to read. But this should be the way.
pickerView:(UIPickerView *)thePickerView didSelectRow
method update the array object [qtyary objectAtIndex:indexPath.row]. Then call [tableView reloadData]; This will update your cell.qtylb.text. For other label also you need a array which you will update. I hope this works.
try changing line in didSelectRow
UITableViewCell *owningCell = (UITableViewCell*)[thePickerView superview];
to
ordercel *owningCell = (ordercel*)[thePickerView superview];
If you have custom cell that you are using to put button on.Then you should take cell instance of your custom cell not UITableViewCell.
And try printing log in this condition
if ([pickerType isEqualToString:#"qtypicker"])
{
UILabel *obj=((UILabel *)[self.view viewWithTag:index1+1000]);
obj.text = [array objectAtIndex:row];
[qtyary addObject:obj.text];
[tabvu reloadData];
}
what object it is returning.Apply NSLog to obj.

how can i make uipickerview with multiple row selection and displaying liste options selected in uitextfield iphone?

i have to make a picker view with multiple row selection. i have tried this solution but i was failed for me.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setBounds:CGRectMake(0, 0, cell.frame.size.width - 20, 44)];
cell.tag = row ;
UITapGestureRecognizer * singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(toggleSelection:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
[cell addGestureRecognizer:singleTapGestureRecognizer];
}
if ([self.selectedItems indexOfObject:[NSNumber numberWithInt:row]] != NSNotFound) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text = [self.options objectAtIndex:row];
return cell;
}
-(void)toggleSelection:(UITapGestureRecognizer *)recognizer {
NSNumber *row = [NSNumber numberWithInt:recognizer.view.tag];
NSUInteger index = [self.selectedItems indexOfObject:row];
if (index != NSNotFound) {
[self.selectedItems removeObjectAtIndex:index];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryNone];
} else {
[self.selectedItems addObject:row];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
For multiple row selection in pickerView you can use https://github.com/alexleutgoeb/ALPickerView
for mutliple selection we have to create custom picker by using Tableview.once see this one

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;
}
}

Changing text size in Pickerview

How can we change the size of the text in a picker view?
Anyone please help.
You can change the font size of UIPickerView using code :
- (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.font = [UIFont systemFontOfSize:22];
return retval;
}
For completeness, yakub_moriss code lacks of one line, which was in the post signaled by indragie. Without a line as
retval.text = [pickerViewArray objectAtIndex:row];
the picker will be empty.
This is also a great way to change the alignment of the text in the component too, change the textAlignment of the retval label.
Also, the background for the label should be set to clear:
- (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)];
}
retval.opaque = NO;
retval.backgroundColor = [UIColor clearColor];
retval.font = [UIFont systemFontOfSize:28];
if (component == 0) {
retval.textAlignment = UITextAlignmentRight;
retval.text = [NSString stringWithFormat:#"%d ", row];
} else {
retval.textAlignment = UITextAlignmentLeft;
retval.text = [NSString stringWithFormat:#" .%d", row];
}
return retval;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 100;
}
return 88;
}
I'm using a category for this. It relies on this other category I have: http://compileyouidontevenknowyou.blogspot.com/2012/06/adding-properties-to-class-you-dont.html.
Anyway, this one looks like this:
#import <UIKit/UIKit.h>
#interface UIPickerView (CustomViewForRow)
#property (nonatomic, strong) UIFont *customViewForRowFont;
- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
#end
and the .m
#import "UIPickerView+CustomViewForRow.h"
#import "NSObject+AssociatedObjectSupport.h"
#implementation UIPickerView (CustomViewForRow)
static char keyForCustomViewForRowFont;
- (UIFont *)customViewForRowFont {
UIFont *retVal = [self associatedObject:&keyForCustomViewForRowFont];
if (!retVal)
retVal = [UIFont boldSystemFontOfSize:17];
return retVal;
}
- (void)setCustomViewForRowFont:(UIFont *)customViewForRowFont {
[self setAssociatedObject:customViewForRowFont forKey:&keyForCustomViewForRowFont];
}
- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label;
if (!view) {
view = [[UIView alloc] init];
CGRect frame = CGRectZero;
frame.size = [self rowSizeForComponent:component];
view.frame = frame;
view.backgroundColor = UIColor.clearColor;
label = [[UILabel alloc] init];
label.font = self.customViewForRowFont;
label.backgroundColor = [UIColor clearColor];
frame = view.bounds;
frame.origin.x += 7;
frame.size.width -= 14;
label.frame = frame;
[view addSubview:label];
}
label = [view.subviews objectAtIndex:0];
#try {
NSString *text = [self.delegate pickerView:self titleForRow:row forComponent:component];
if (!text)
text = #"???";
label.text = text;
} #catch (NSException *e) {
label.text = #"???";
}
return view;
}
#end
Try this
https://github.com/kafejo/CCPicker
it's very easy picker :)