I'm trying to subclass UIPickerView but is not loading in the main view.
Here is my code:
//Picker.h:
#interface Picker : UIPickerView
- (id)initWithFrame:(CGRect)frame;
//Picker.m:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return #"bla";
}
This the code on my viewController:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
self.myPicker = [[Picker alloc] initWithFrame:self.view.frame];
[self.view addSubview:_myPicker];
}
Any of you knows why the picker is not loading on my view?
I'll really appreciate your help.
First, add <UIPickerViewDelegate, UIPickerViewDataSource> in header of Picker.h.
Then, assign delegate to your own Picker object.
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.delegate = self; // <-- add this line
}
return self;
}
Related
enter image description here $ The data of assigned array is not showing in the pickerView. What mistake I am doing as I haven't being able to identify my mistake.
Its .m file code:
// PickerViewController.m
// dropDownButtonTry
//
#import "PickerViewController.h"
#interface PickerViewController ()
{
NSArray *genderArray;
NSArray *cityArray;
NSArray *currentArray;
UITextField *currentTextField;
}
#end
#implementation PickerViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
cityArray = [[NSArray alloc]initWithObjects:#"Delhi",#"Mumbai",#"Chennai", nil];
genderArray = [[NSArray alloc]initWithObjects:#"Male",#"Female",#"Transgender", nil];
self.pickerView.hidden = YES;
self.btnDoneOutlet.hidden = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
//Needed to prevent keyboard from opening
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
currentTextField = textField;
if (textField == self.textFieldGenderOutlet) {
currentArray = genderArray;
}
if (textField == self.textFieldCityOutlet) {
currentArray = cityArray;
}
// do here everything you want
NSLog(#"Pressed on TextField!");
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;
[self.view endEditing:YES]; // Hide keyboard
NSLog(#"****current array**** %#",currentArray);
return NO;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [currentArray count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[currentTextField setText:[currentArray objectAtIndex:row]];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [currentArray objectAtIndex:row];
}
- (IBAction)textFieldGenderAction:(id)sender {
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;
}
- (IBAction)btnDone:(id)sender {
self.pickerView.hidden = YES;
self.btnDoneOutlet.hidden = YES;
}
- (IBAction)textFieldCityAction:(id)sender {
}
#end
You need to reload picker view :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
currentTextField = textField;
if (textField == self.textFieldGenderOutlet) {
currentArray = genderArray;
}
if (textField == self.textFieldCityOutlet) {
currentArray = cityArray;
}
// do here everything you want
NSLog(#"Pressed on TextField!");
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;
[self.thePicker reloadAllComponents];
[self.view endEditing:YES]; // Hide keyboard
NSLog(#"****current array**** %#",currentArray);
return NO;
}
currentArray is assigned value in textFieldShouldBeginEditing but it is not initialized anywhere in your code.
Just initialize currentArray in viewDidLoad like below:
currentArray = [NSArray new];
I am trying to use a PickerView with ARC turned off, it has become a nightnmare, will be extremely grateful if someone could please have a quick look kindly suggest how to solve...thnx in advance
the issue is that the rest of my app is non-arc and so i want to add the pickerview as a class that can operate as a viewcontroller in the project...
I had multiple pickers working beautifully 4 days ago with arc but when i turned arc off it became a nightmare...to identify the precise issue i have removed all the frills and am just working with a bare structure now....there are no errors but the data from the array is not displaying...i am listing the header and omplementation files below...the reason i had linked the folder was to make the interface file available....so that the connections can be checked too...but we can look at that after the basic code is first checked....thanks so much for your kind attention and time...
.h
#import <UIKit/UIKit.h>
#interface HomeViewController : UIViewController UIPickerViewDataSource,UIPickerViewDelegate>
{NSArray *categoryTypes;
}
#property (retain, nonatomic) IBOutlet UIPickerView *categoryTypePicker;
#property (retain, nonatomic) IBOutlet UIButton *categoryTypeBtn;
#end
.m
#import "HomeViewController.h"
#interface HomeViewController ()
#define kPICKER1COLUMN 1
#define kCATEGORYTYPEPICKERTAG 21
#define kCATEGORYTYPEBTN 31
#end
#implementation HomeViewController
#synthesize categoryTypeBtn;
#synthesize categoryTypePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
categoryTypes = [[NSArray alloc] initWithObjects:#"Appetizers",#"Breakfast",#"Dessert",#"Drinks",
#"Main Dish/Entree", #"Salad", #"Side Dish", #"Soup", #"Snack",
#"Baby Food", #"Pet Food", nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
categoryTypePicker.tag = kCATEGORYTYPEPICKERTAG;
categoryTypePicker.showsSelectionIndicator = TRUE;
categoryTypePicker.dataSource = self;
categoryTypePicker.delegate = self;
self.categoryTypePicker.dataSource = self;
self.categoryTypePicker.delegate = self;
[self.categoryTypePicker reloadAllComponents];
categoryTypePicker.hidden = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if (pickerView.tag == kCATEGORYTYPEPICKERTAG)
return kPICKER1COLUMN;
else { return 0; }
}
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView.tag == kCATEGORYTYPEPICKERTAG)
return [categoryTypes count];
else { return 0; }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView.tag == kCATEGORYTYPEPICKERTAG)
return [categoryTypes objectAtIndex:row];
else { return 0; }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == kCATEGORYTYPEPICKERTAG)
{
NSString *categoryType = [categoryTypes objectAtIndex:[categoryTypePicker selectedRowInComponent:0]];
[categoryTypeBtn setTitle:categoryType forState:UIControlStateNormal];
NSLog(#"%#", categoryType);
}
pickerView.hidden=YES;
}
-(IBAction) showCategoryTypePicker{
{
[self.view addSubview:categoryTypePicker];
categoryTypePicker.hidden = NO;
}
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[categoryTypes release];
[categoryTypePicker release];
[super dealloc];
}
#end
i will await your reply...
linktocode
[dont have access to comments so am responding here] thanks rdelmar i commented out the init-bundle and the subview of the button but now the build is giving the error bad access...i have a rough idea of what is going on but don't know how to address it...the issue is basically that the array is not retained for the objectAtIndex call...need some way of retaining it...thnx
I'm trying to bring up a picker view instead of a keyboard to put in a textfield, but my picker view doesn't show.
#import "SettingsViewController.h"
#import "FindClasses.h"
#interface SettingsViewController ()
#property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
#property (weak, nonatomic) IBOutlet UITextField *classTextField;
#property (strong, nonatomic) UIPickerView *picker;
#property (nonatomic, strong) FindClasses *finder;
#property (nonatomic, strong) NSArray *itemsArray;
#end
#implementation SettingsViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *itemsArray = [self.finder findClassesInTimetable];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)presentPicker {
CGRect frame = CGRectMake(0, 40, 0, 0);
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:frame];
picker.delegate = self;
picker.dataSource = self;
// TODO: animate this on screen
[self.view addSubview:picker];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self presentPicker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.itemsArray count]; //where items is your array of items
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row inComponent:(NSInteger)component {
return [self.itemsArray objectAtIndex:row];
}
Can someone tell me what I'm doing wrong? I took the example from another Stackoverflow post:
This is how I use UIPickerViews, with sample code:
UIPickerView* genderPicker = [[UIPickerView alloc] init];
genderPicker.delegate = self;
genderPicker.dataSource = self;
After initializing your UITextField (registrationGenderField in my case):
registrationGenderField.inputView = genderPicker;
And then the delegates and dataSources:
- (NSString *) pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
switch (row) {
case 0:
return #"Gender";
break;
case 1:
return #"Male";
break;
case 2:
return #"Female";
break;
default:
return #"Gender";
break;
}
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (row) {
case 0:
registrationGenderField.text = #"";
break;
case 1:
registrationGenderField.text = #"Male";
break;
case 2:
registrationGenderField.text = #"Female";
break;
default:
registrationGenderField.text = #"";
break;
}
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}
Hope it helps you
- (void)presentPicker {
UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
// TODO: animate this on screen
classTextField .delegate = self;
classTextField.inputView = picker;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[textField becomeFirstResponder];
return YES;
}
you just have to alloc init your pickerview;
and set its delegate to self and also add uipickerviewdelegate and datasource in .h file
than in textfieldbegin editing add [textField setInputView:picker];
also implement textfieldshouldreturn
I have been working on a multi-component picker and I wanted to set the defaults for the various components. The code looks like the following.
#import "MathTablesSelectVC.h"
#interface MathTablesSelectVC ()
#property (weak, nonatomic) IBOutlet UIPickerView *problemDifficultyTypeTable;
#property NSArray *problemDifficulties;
#property NSArray *problemTypes;
#property NSArray *problemTables;
#end
#implementation MathTablesSelectVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"0");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(#"1");
[super viewDidLoad];
_problemDifficulties = #[#"Practice", #"Slow", #"Normal", #"Fast"];
_problemTypes = #[#"Addition", #"Subtraction", #"Multiplication", #"Division", #"Remainders"];
_problemTables = #[#" 0's", #" 1's", #" 2's", #" 3's", #" 4's", #" 5's", #" 6's", #" 7's", #" 8's", #" 9's", #"10's", #"11's", #"12's", #"All"];
}
-(void)viewWillAppear:(BOOL)animated {
NSLog(#"2");
[super viewWillAppear:animated];
[_problemDifficultyTypeTable selectRow:1 inComponent:0 animated:NO];
[_problemDifficultyTypeTable selectRow:0 inComponent:1 animated:NO];
[_problemDifficultyTypeTable selectRow:1 inComponent:2 animated:NO];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
NSLog(#"3");
return 3;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
NSLog(#"4");
if(component == 0) {
return 150.0;
} else if(component == 1) {
return 300.0;
} else {
return 75.0;
}
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
NSLog(#"5");
return 60.0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSLog(#"6");
if(component == 0) {
return [_problemDifficulties count];
} else if(component == 1) {
return [_problemTypes count];
} else {
return [_problemTables count];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSLog(#"7");
if(component == 0) {
return [_problemDifficulties objectAtIndex:row];
} else if(component == 1) {
return [_problemTypes objectAtIndex:row];
} else {
return [_problemTables objectAtIndex:row];
}
}
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(#"8");
}
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(#"9");
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"10");
}
#end
Now this all worked nicely for a while and then it stopped working :(
I then spent the next several hours working out what I'd done wrong.
The answer I stuffed things up when I did some refactoring. I managed to kill the connection between the storyboard and the IBOutlet. Now the nice small, very small, little circle was still there next to the #property it just no longer had a dot in it. As soon as I re-established the connection things started behaving again.
You'll also note a whole bunch of NSLog's and this seems to be the order things are first called in. We get several repeats of some of the numbers as we cycle through the components.
The other thing I broke although I picked that up pretty much straight away were the delegation connections.
I am trying to set the UIPicker View with the date for the last 20 Mondays. The array is filled with 20 Week Objects. e.g NSDate *weekDate=3/24/2014 NSString *weekDateString=Week of 3/24/2014.
When I set the rows initially it shows the first 5-6 rows correctly, when I try to scroll the picker is trying to set the title for the new rows it is displaying, but setTitle is logging (null) for the weekDateString. The list is still showing a count of 20, when I log the count in titleForRow.
If I set the NSArray just NSString there are no issues.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_weeks = [Week getWeeks];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=#"Weeks";
[self.navigationController.navigationBar.layer insertSublayer:[LayoutColors getNavBarGradient:self.navigationController.navigationBar.bounds] atIndex:1];
self.navigationController.navigationBar.titleTextAttributes
= #{NSForegroundColorAttributeName : [UIColor whiteColor]};
int startYValue = self.navigationController.navigationBar.frame.size.height + HeaderHeight;
int width = self.view.frame.size.width;
_picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, startYValue, width/2, DATE_PICKER_HEIGHT)];
_picker.delegate = self;
_picker.dataSource = self;
[self.view addSubview:self.picker];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return _weeks.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
Week *week = [_weeks objectAtIndex:row];
return week.weekDateString;
}
There is nothing wrong with your code, so one is compelled to infer that there is probably something wrong with your model object, i.e. your _weeks array is not in fact correctly populated the way you think it is - the weekDateString is apparently missing for some of the Week objects. You should probably go back and look at how you are generating this array.