UITextField inputview to pop up UIPickerView displaying question mark in pickerview - iphone

Here is my .h file
When I run this code and select a text field, a pickerview pop's with question marks in it
but when I select question mark I get correct values in text field
#import <UIKit/UIKit.h>
#import "ScrollableViewController.h"
#import "MIBackgroundTapDelegate.h"
//#interface Activapc1 : UIViewController {
#interface Activapc1 : ScrollableViewController <MIBackgroundTapDelegate, UIPickerViewDelegate, UIPickerViewDataSource>{
UITextField *amplitude1;
UITextField *rate1;
UITextField *pulse_width1;
UITextField *impedance1;
IBOutlet UITextField *configuration;
NSArray *mode;
}
#property (nonatomic, retain) IBOutlet UITextField *amplitude1;
#property (nonatomic, retain) IBOutlet UITextField *rate1;
#property (nonatomic, retain) IBOutlet UITextField *pulse_width1;
#property (nonatomic, retain) IBOutlet UITextField *impedance1;
-(IBAction)next;
-(IBAction)skip;
-(IBAction)home;
-(IBAction)select;
-(IBAction)textFieldDoneEditing:(id) sender;
#end
Here is .m file
I am not able to understand why I am getting question mark please help me
#import "Activapc1.h"
#import "dbsViewController.h"
#import "Activapc2.h"
#import "APCoption.h"
#implementation Activapc1
#synthesize amplitude1;
#synthesize rate1;
#synthesize pulse_width1;
#synthesize impedance1;
const int MyModePicker = 3002;
-(void)viewDidLoad {
self.svScrollViewM.contentSize = CGSizeMake(320, 416);
[self registerForEditingEvents:amplitude1];
[self registerForEditingEvents:rate1];
[self registerForEditingEvents:pulse_width1];
[self registerForEditingEvents:impedance1];
UIPickerView *modePicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
modePicker.tag = MyModePicker;
modePicker.delegate = self;
modePicker.dataSource = self;
[modePicker setShowsSelectionIndicator:YES];
configuration.inputView = modePicker;
[modePicker release];
[super viewDidLoad];
//Prepare data for pickers
mode = [NSArray arrayWithObjects:#"Voltage",#"Current",nil];
[mode retain];
//mode = [[NSMutableArray alloc] init];
//[mode addObject]
}
//********picker view code**************
#pragma mark -
#pragma mark UIPickerViewDelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titileForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if(pickerView.tag == MyModePicker)
{
return [mode objectAtIndex:row];
}
return #"Unknown title";
}
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView.tag == MyModePicker)
{
configuration.text = (NSString *)[mode objectAtIndex:row];
}
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(pickerView.tag == MyModePicker)
{
return mode.count;
}
return 1;
}
#pragma mark -
//**********picker view code end************
-(IBAction) backgroundTap:(id) sender{
[self.amplitude1 resignFirstResponder];
[self.rate1 resignFirstResponder];
[self.pulse_width1 resignFirstResponder];
[self.impedance1 resignFirstResponder];
}
-(IBAction) textFieldDoneEditing:(id)sender{
[sender resignFirstResponder];
}
-(IBAction)home {
dbsViewController *dbs = [[dbsViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:dbs animated:YES];
}
-(IBAction)skip {
Activapc2 *activapc2 = [[Activapc2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:activapc2 animated:YES];
}
-(IBAction)next {
Activapc2 *activapc2 = [[Activapc2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:activapc2 animated:YES];
}
-(IBAction)select {
APCoption *apcoption = [[APCoption alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:apcoption animated:YES];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mode release];
[super dealloc];
}
#end

The pickerview needs a title not a titile. Also the delegate method that returns the string does not have a reusingView: parameter. Since you don't properly implement the delegate method the picker simply displays question marks.
-(NSString *)pickerView:(UIPickerView *)pickerView titileForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
I believe it should be:
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

Related

custom pickerview does not get scrolled

I have created a custom picker view with xib which loads a data from the array and displays it properly. Now my problem is, this picker view does not get scrolled. Actually this picker view is loaded on to another view. So when i click into the picker view, the click appears to be on the base view on which this has been loaded.
Below is the code. Can any one please tell how do i correct this problem. Thanks
#import <UIKit/UIKit.h>
#class PickerView;
#protocol PickerViewDelegate <NSObject>
-(void) didSelectValueAtIndex;
#end
#interface PickerView : UIView <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
#property (nonatomic,weak) id<PickerViewDelegate> PickerViewDelegate;
-(id) showPickerView : (UIButton *) senderButton dataArray: (NSArray *) dataArray viewframe: (CGRect) viewFrame;
-(void) hidePickerView : (UIButton *) senderButton;
#end
#import "PickerView.h"
#interface PickerView ()
#property (nonatomic,retain) NSArray * pickerData;
#property (nonatomic,retain) UIButton *button;
#end
#implementation PickerView
#synthesize pickerView;
#synthesize PickerViewDelegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(id) showPickerView : (UIButton *) senderButton dataArray: (NSArray *) dataArray viewframe:(CGRect)viewFrame{
self.button = senderButton;
self.pickerView = (UIPickerView *)[super init];
if (self) {
//inorder to load xib
NSArray *theView = [[NSBundle mainBundle] loadNibNamed:#"PickerView" owner:self options:nil];
UIView *pickerviewLoadedFromXib = [theView objectAtIndex:0];
CGRect btn = senderButton.frame;
self.pickerData = dataArray;
pickerviewLoadedFromXib.frame = CGRectMake(btn.origin.x , btn.origin.y+ viewFrame.size.height, pickerviewLoadedFromXib.frame.size.width, pickerviewLoadedFromXib.frame.size.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
pickerviewLoadedFromXib.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 200);
[UIView commitAnimations];
[senderButton.superview addSubview:self];
[self addSubview:pickerviewLoadedFromXib];
}
return self;
}
-(void) hidePickerView : (UIButton *) senderButton{
NSLog(#"hide picker view");
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.pickerData.count;
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.pickerData objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selectd value %#", [self.pickerData objectAtIndex:row]);
}
#end
pickerviewLoadedFromXib.delegate = self;
pickerviewLoadedFromXib.dataSource = self;
Pop these two in after you get the pickerviewloadedfromxib variable...

Data from array is not showing up in the pickerView

I am trying to do Single-Component Picker example Beginning iOS 6 Development Book, almost every thing works fine but Data from array is not showing up in the pickerView
Please Help
Here is my .h file
<UIPickerViewDataSource , UIPickerViewDelegate>
{
IBOutlet UIPickerView *singlePicker;
NSArray *chaNames;
}
#property (strong, nonatomic) IBOutlet UIPickerView *singlePicker;
#property (strong, nonatomic) NSArray *chaNames;
- (IBAction)buttonPressed:(id)sender;
#end
And here is .m file
#import "SSPSingleComponentViewController.h"
#interface SSPSingleComponentViewController ()
#end
#implementation SSPSingleComponentViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.chaNames = #[#"Luke",#"Leia",#"Han",#"Chewbacca",#"Artoo", #"Threepio",#"lando"];
}
- (IBAction)buttonPressed:(id)sender {
NSInteger row = [self.singlePicker selectedRowInComponent:0];
NSString *selected = self.chaNames[row];
NSString *title = [[NSString alloc]initWithFormat:#"You slected %#", selected];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"OutPut is : " message:title delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [self.chaNames count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return[self.chaNames objectAtIndex:row];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Please Help
Thanks in advance :)
You need to alloc your NSArray object as follows:
self.chaNames = [NSArray alloc] initWithObjects:#"Luke",#"Leia",#"Han",#"Chewbacca",#"Artoo", #"Threepio",#"lando"];
Edit:
you also need to set singlePicker.datasource = self; and singlePicker.delegate = self; within viewDidLoad or hook it up correctly with IB (both the delegate and datasource)
-(void)viewDidLoad {
self.chaNames = [NSArray alloc] initWithObjects:#"Luke",#"Leia",#"Han",#"Chewbacca",#"Artoo", #"Threepio",#"lando"];
}

Implement UIPickerView horizontally?

The default scroll setting for a UIPickerView is set to vertical. Is it possible to implement a UIPickerView horizontally?
If so, could you please show me a sample or direct me where to helpful documentation?
you can use CPPickerView .. A custom, configurable, horizontal version of UIPickerView (based on the spinning-wheel or slot-machine metaphor), with an included table cell implementation. Originally intended for condensing the space/rows needed for a multi-option setting.
IN .h File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *itemArray;
IBOutlet UILabel *myLabel;
}
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) UILabel *myLabel;
#end
IN .XIB File
drag and drop UIPickerView And One UILable
Also connect BOTH the “delegate” and “Referencing Outlet” to the FileOwner.
IN .M File
#import "ViewController.h"
#implementation ViewController
#synthesize pickerView, myLabel;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pickerView.delegate = self;
self.pickerView.showsSelectionIndicator =YES;
self.pickerView.backgroundColor = [UIColor blackColor];
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
rotate = CGAffineTransformScale(rotate, 0.1, 0.8);
[self.pickerView setTransform:rotate];
self.pickerView.center = CGPointMake(160,75);
UILabel *theview[20];
CGAffineTransform rotateItem = CGAffineTransformMakeRotation(-M_PI_2);
rotateItem = CGAffineTransformScale(rotateItem, 1, 10);
for (int i=0;i<20;i++) {
theview[i] = [[UILabel alloc] init];
theview[i].text = [NSString stringWithFormat:#"%d",i];
theview[i].textColor = [UIColor blackColor];
theview[i].frame = CGRectMake(0,0, 100, 100);
theview[i].backgroundColor = [UIColor clearColor];
theview[i].textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter is deprecated.
theview[i].shadowColor = [UIColor whiteColor];
theview[i].shadowOffset = CGSizeMake(-1,-1);
theview[i].adjustsFontSizeToFitWidth = YES;
UIFont *myFont = [UIFont fontWithName:#"Georgia" size:15];
[theview[i] setFont:myFont];
theview[i].transform = rotateItem;
}
itemArray = [[NSMutableArray alloc] init];
for (int j=0;j<20;j++) {
[itemArray addObject:theview[j]];
}
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [itemArray count];
}
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
return [itemArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
myLabel.text = [NSString stringWithFormat:#"SELECTED: %d", row+1];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end

UIPickerview AppDelegate problem with subviews

Issue with subviews but it may be related to the AppDelegate with respect to placing a UIPickerView in myView as apposed to self.view?
Thanks
[self.view addSubview:pickerView]; <-- works ok
//[myView addSubview:pickerView];<-- fails, can't get dial movement or response
AppDelegate.h
#import <UIKit/UIKit.h>
#class PickerViewController;
#interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PickerViewController *pvController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
AppDelegate.m
#import "PickerViewAppDelegate.h"
#import "PickerViewController.h"
#implementation PickerViewAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
pvController = [[PickerViewController alloc] initWithNibName:#"PickerView" bundle:[NSBundle mainBundle]];
[window addSubview:pvController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[pvController release];
[window release];
[super dealloc];
}
#end
Controller.h
#import <UIKit/UIKit.h>
#interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arrayColors;
}
#end
Controller.m
#import "PickerViewController.h"
#implementation PickerViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[myView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myView];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
arrayColors = [[NSMutableArray alloc] init];
for (int i=0; i<60; i++) {
[arrayColors addObject:[NSString stringWithFormat:#"%d", i]];
}
//[self.view addSubview:pickerView]; // <-- works
[myView addSubview:pickerView]; // <-- this fails.. this is what I need for multiple views
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[arrayColors release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayColors count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Color: %#. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
#end
If you comment
[myView addSubview:pickerView];
you will understand, that myView frame is too small. You will be able to interact with the picker only touching that parent view frame. If you want to make the pickers behave normally, you must set the parent view frame to contain the picker frame - make it equal or larger.
Please remember iOs Human Interface Guidelines,
The overall size of a picker, including its background, is fixed at the same size as the keyboard on iPhone.
Make sure that myView has userInteractionEnabled set to YES?

Picker view displaying and populated correctly but not interactive

I have created a Picker view in objective C and populated it with an NSArray and that all works fine, it compiles and shows on screen but the picker doesn't do anything. I'd like to turn as the user drags their finger across like you'd expect but it doesn't react in anyway.
'lil Help?
The code I used for the picker comes from "Beginning iPhone Development" but I have more views (a map view and a view I use for drawing) on screen, for clarity I can just dump all the code in here so I'll limit it to what I believe is relevant.
// SingleComponentPickerViewController.h
#import <UIKit/UIKit.h>
#interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *singlePicker;
NSArray *pickerData;
}
#property (nonatomic, retain) UIPickerView *singlePicker;
#property (nonatomic, retain) NSArray *pickerData;
- (IBAction)buttonPressed:(id)sender;
#end
// SingleComponentPickerViewController.m
#import "SingleComponentPickerViewController.h"
#implementation SingleComponentPickerViewController
#synthesize singlePicker;
#synthesize pickerData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (IBAction)buttonPressed:(id)sender
{
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:#"You selected %#!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:#"Thank you for choosing." delegate:nil cancelButtonTitle:#"You're Welcome" otherButtonTitles:nil];
[alert show];
[alert release];
[title release];
}
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"Luke", #"Leia", #"Han", #"Chewbacca", #"Artoo", #"Threepio", #"Lando", nil];
self.pickerData = array;
[array release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
#end
That's the code but I have a feeling the problem is more to do with how the views are placed/interacting.
Set the "useractionEnabled" property set to true (in either IB or code)? This should be the default.
But as mentioned above in the comments ... some code might be helpful :)