UIPickerView won't show - objective-c

I have been trying for 2 days now to get a UIPickerView to show and I am at a loss. I have implemented all the methods to connect a data source (NSArray) and have no idea why I can't get it to show. The assignment is now due tonight and the professor is less than useless, I've given up asking her.
This is the .m file of the subview containing the UIPickerView. All I need is to figure out why the picker won't display! Help appreciated I'm exasperated....
#import "SpotPicker.h"
#implementation SpotPicker
#synthesize spotPicker;
NSArray *pickerLocations;
NSString *selectedLocation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
pickerLocations = [[NSArray alloc]initWithObjects:#"Grand Canyon", #"Mt Rushmore", #"Statue of Liberty", #"Empire State Building", #"Hollywood Sign", #"Lincoln Memorial", #"Space Needle", nil];
}
- (void)viewDidUnload
{
[self setSpotPicker:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissPicker:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [pickerLocations count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerLocations objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//NSLog(#"Selected Color: %#. Index of selected color: %i", [pickerLocations objectAtIndex:row], row);
selectedLocation = [pickerLocations objectAtIndex:row];
}
-(NSString *) getLocation {
return selectedLocation;
}
#end
Here's the header file to just to make sure I haven't declared something incorrectly or something stupid
#import <UIKit/UIKit.h>
#interface SpotPicker : UIViewController
- (IBAction)dismissPicker:(id)sender;
-(NSString *) getLocation;
#property (weak, nonatomic) IBOutlet UIPickerView *spotPicker;
#end

You need to declare that your SpotPicker implements the UIPickerViewDelegate and UIPickerViewDataSource protocols:
#interface SpotPicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
and set your UIPickerView's delegate to be your SpotPicker. You could either do this in IB, or programatically:
- (void)viewDidLoad
{
...
self.spotPicker.delegate = self;
self.spotPicker.dataSource = self;
}

Related

UIAlertView: alert won't display

Can't get this alert instance to display in this implementation file, and I just can't find the problem.
Everything else works fine.
Any suggestions as to what's wrong?
#import "BIDSingleComponentViewController.h"
#implementation BIDSingleComponentViewController
- (IBAction)buttonPressed
{
NSInteger row = [self.singlePicker selectedRowInComponent:0];
NSString *selected = self.characterNames[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];
}
- (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.
self.characterNames = #[#"Luke", #"Leia", #"Han", #"Chewbacca", #"Artoo", #"Threepio", #"Lando"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1; // Incompatible integer to pointer conversion
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.characterNames count];
}
#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return self.characterNames[row];
}
#end
Header file:
#import <UIKit/UIKit.h>
#interface BIDSingleComponentViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
#property (strong, nonatomic)IBOutlet UIPickerView *singlePicker;
#property (strong, nonatomic)NSArray *characterNames;
- (IBAction)buttonPressed;
#end
In your header file make sure you include the AlertViewDelegate:
#interface BIDSingleComponentViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource, UIAlertViewDelegate>
Also, be sure to connect all outlets to where they belong in IB

Implementation file can't see Constant declared in header file

I am getting error messages in the implementation file (commented out) for Constants declared in the header file. Why is the compiler doing this?
Thanks in advance.
Header file:
#import <UIKit/UIKit.h>
#define kFilling Component 0
#define kBread Component 1
#interface BIDDoubleComponentViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
#property (strong, nonatomic) IBOutlet UIPickerView *doublePicker;
#property (strong, nonatomic) NSArray *fillingTypes;
#property (strong, nonatomic) NSArray *breadTypes;
- (IBAction) buttonPressed;
#end
Implementation file:
#import "BIDDoubleComponentViewController.h"
#implementation BIDDoubleComponentViewController
- (IBAction)buttonPressed
{
NSInteger fillingRow = [self.doublePicker selectedRowInComponent:kFillingComponent]; // Use of undeclared identifieer 'kFillingComponent'
NSInteger breadRow = [self.doublePicker selectedRowInComponent:kBreadComponent]; // Use of undeclared identifieer 'kBreadComponent'
NSString *filling = self.fillingTypes[fillingRow];
NSString *bread = self.breadTypes[breadRow];
NSString *message = [[NSString alloc] initWithFormat:#"Your %# on %# will be right up.", filling, bread];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Thank you for your order"
message:message
delegate:nil
cancelButtonTitle:#"Great"
otherButtonTitles:nil];
[alert show];
}
- (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.
self.fillingTypes = #[#"Ham", #"Turkey", #"Peanut Butter", #"Tuna salad", #"Chicken salad", #"Roast Beef", #"Vegemite"];
self.breadTypes = #[#"White", #"Whole Wheat", #"Rye", #"Sour Dough", #"Seven-Grain"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == kBreadComponent) // Use of undeclared identifieer 'kFillingComponent'
{
return [self.breadTypes count];
} else {
return [self.fillingTypes count];
}
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == kBreadComponent) // Use of undeclared identifieer 'kBreadComponent'
{
return self.breadTypes[row];
} else {
return self.fillingTypes[row];
}
}
#end
Your defines are bad. You have
#define kBread Component 1
But you're using kBreadComponent. What the #define is doing is it's defining a token called kBread that evaluates during preprocessing to the tokens Component 1. You probably meant to use
#define kBreadComponent 1

Text Box Picker

Im practicing using a picker to input values into a text field. Ive tried to adapt some code found on here to present a picker when text box is touched to fill out text field. The text boxes were built in code SOURCE and I would like to build the text boxes in IB. However this has stopped the presentation picker view, and presents only the keyboard now instead
Perhaps someone would point me in the right direction here. My code:
.h
#import <UIKit/UIKit.h>
#interface pick2 : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *locationsPicker;
UIToolbar *accessoryView;
UITextField *text1;
NSArray *locations;
}
#property (nonatomic, readonly) UIPickerView *locationsPicker;
#property (nonatomic, readonly) UIToolbar *accessoryView;
#property (strong, nonatomic) IBOutlet UITextField *text1;
- (void)onLocationSelection;
#end
.m
#import "pick2.h"
#interface pick2 ()
#end
#implementation pick2
#synthesize text1;
- (UIPickerView *)locationsPicker {
if ( locationsPicker == nil ) {
locationsPicker = [[UIPickerView alloc] init];
locationsPicker.delegate = self;
locationsPicker.dataSource = self;
locationsPicker.showsSelectionIndicator = YES;
}
return locationsPicker;
}
- (UIToolbar *)accessoryView {
if ( accessoryView == nil ) {
accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain
target:self
action:#selector(onLocationSelection)];
[accessoryView setItems:[NSArray arrayWithObject:doneButton]];
}
return accessoryView;
}
#pragma mark - Memory management
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)onLocationSelection {
NSInteger row = [self.locationsPicker selectedRowInComponent:0];
( [text1 isFirstResponder] ); {
text1.text = [locations objectAtIndex:row];
[text1 resignFirstResponder];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - picker view delegate/datasource
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [locations objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return [locations count];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
locations = [[NSArray alloc] initWithObjects:#"New York", #"Chicago", #"Memphis", #"California", #"Seattle",#"London",#"Paris", nil];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setText1:nil];
[super viewDidUnload];
}
#end
All you have to do is give the inputView of your UITextField as object of your pickerview.
text1.inputView = locationsPicker;
tapping on the UITextField will present the pickerview now.
Sorted this by using IB to build text boxes and
NSInteger selectedRow = [select selectedRowInComponent:0];
text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];
To retrive text value

UIPickerView undeclared?

I got a slight problem, I'm getting a error saying "'UIPickerView' undeclared on line 1 of the below code. I copied right out of the book that I have and I'm not sure whats wrong?
could I get some help? it would be greatly appreciated, thanks :)
this is the entire .m file code.
#import "InstatwitViewController.h"
#implementation InstatwitViewController
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping",
#"eating", #"working", #"thinking", #"crying", #"begging",
#"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:#"awesome",
#"sad", #"happy", #"ambivalent", #"nauseous", #"psyched",
#"confused", #"hopeful", #"anxious", nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)
pickerView {
return 2;
}
- (NSInteger)PickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent : (NSInteger)component {
if (component == 0) {
return [activities count];
}
else {
return [feelings count];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (component) {
case 0:
return [activities objectAtIndex:row];
case 1:
return [feelings objectAtIndex:row];
}
return nil;
}
}
- (void)dealloc {
[activities release];
[feelings release];
[super dealloc];
}
#end
Where did you copy it? It should be in the #implementation ... #end section of the .m file of the class you are implementing. It should not be in the #interface ... #end part, which is typically in a .h file, or elsewhere, i.e. outside the #implementation ... #end section.
Please show a little more of your code.
Edit
This:
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
lacks a closing } before - (NSString *)pickerView: .... In other words, change it thus:
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

2 UIPickerViews each having its own UILabel to display value from NSMutableArray

I'm having 2 UIPickerViews and two UILabels in my view and the UIPickerViews are populated with numbers from an NSMutableArray.
The pickers need to send there chosen value to there assigned label. Example:
_pickerView1 (selected "18")
_pickerOutputLabel1 (shows "18")
_pickerView2 (selected "7")
_pickerOutputLabel2 (shows "7")
I can't get this working, _pickerView2 also sends its value to _pickerOutputLabel1 instead of _pickerOutputLabel2.
I've tried a couple of things but i can't figure out how to get it to work.
This is the code (i removed my attempts to fix the issue so it atleast compiles :)
//header file
#import <UIKit/UIKit.h>
#interface UIPickerViewAndLabelsViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
NSMutableArray *nrArray;
IBOutlet UIPickerView *_pickerView1;
IBOutlet UIPickerView *_pickerView2;
UILabel *_pickerOutputLabel1;
UILabel *_pickerOutputLabel2;
}
#property (nonatomic, retain) IBOutlet UIPickerView *pickerView1;
#property (nonatomic, retain) IBOutlet UIPickerView *pickerView2;
#property (nonatomic, retain) IBOutlet UILabel *pickerOutputLabel1;
#property (nonatomic, retain) IBOutlet UILabel *pickerOutputLabel2;
#end
//implementation file
#import "UIPickerViewAndLabelsViewController.h"
#implementation UIPickerViewAndLabelsViewController
#synthesize pickerView1 = _pickerView1;
#synthesize pickerView2 = _pickerView2;
#synthesize pickerOutputLabel1 = _pickerOutputLabel1;
#synthesize pickerOutputLabel2 = _pickerOutputLabel2;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
// Implement loadView to create a view hierarchy programmatically, without using a nib.
/*
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
_pickerOutputLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(400, 120, 50, 50)];
[self.view addSubview:_pickerOutputLabel1];
_pickerOutputLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(400, 320, 50, 50)];
[self.view addSubview:_pickerOutputLabel2];
nrArray = [[NSMutableArray alloc] init];
for (int i=0;i<20+1;i++) {
[nrArray addObject:[NSString stringWithFormat:#"%d", i]];
}
_pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(500, 120, 100, 162)];
_pickerView1.delegate = self;
_pickerView1.dataSource = self;
_pickerView1.showsSelectionIndicator = YES;
_pickerView1.transform = CGAffineTransformMakeScale(0.8, 0.8);
[self.view addSubview:_pickerView1];
[_pickerView1 release];
[_pickerView1 selectRow:0 inComponent:0 animated:NO];
_pickerOutputLabel1.text = [nrArray objectAtIndex:[_pickerView1 selectedRowInComponent:0]];
_pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(500, 320, 100, 162)];
_pickerView2.delegate = self;
_pickerView2.dataSource = self;
_pickerView2.showsSelectionIndicator = YES;
_pickerView2.transform = CGAffineTransformMakeScale(0.8, 0.8);
[self.view addSubview:_pickerView2];
[_pickerView2 release];
[_pickerView2 selectRow:0 inComponent:0 animated:NO];
_pickerOutputLabel2.text = [nrArray objectAtIndex:[_pickerView2 selectedRowInComponent:0]];
[super viewDidLoad];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)_pickerView1;
{
return 1;
}
- (void)pickerView:(UIPickerView *)_pickerView1 didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
_pickerOutputLabel1.text= [nrArray objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)_pickerView1 numberOfRowsInComponent:(NSInteger)component;
{
return [nrArray count];
}
- (NSString *)pickerView:(UIPickerView *)_pickerView1 titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [nrArray objectAtIndex:row];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return 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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I'm trying for 3 days and i'm stuck.
Thanks in advance.
In the UIPickerView delegate methods, you've named the pickerView parameter "_pickerView1". Naming that parameter the same as the instance variable does not mean the delegate method will be called only for that picker. It just becomes the local name for whatever picker calls the delegate method.
Since you've set the delegate for both the pickers to be self, both the pickers call the same methods.
To tell which picker is making the call, a couple of ways are:
Set a different tag value for each one when creating them and check the tag in the delegate method (eg. _pickerView1.tag = 1; and in the delegate method: if (pickerView.tag == 1)... )
Or, compare directly against the instance variable. For example:
- (void)pickerView:(UIPickerView *)pickerView //<-- std name as in doc
didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == _pickerView1)
// Above:
// "pickerView" is the picker in which a row was selected
// "_pickerView1" is the actual instance variable
_pickerOutputLabel1.text = [nrArray objectAtIndex:row];
else
_pickerOutputLabel2.text = [nrArray objectAtIndex:row];
}
Also, you have IBOutlet in front of the control declarations but then you create them programmatically. If you are using Interface Builder to create the controls, don't re-create them in code. If you're not using IB, remove the IBOutlet.
you can also use this:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if( [pickerView isEqual: picker ]){
firststr = [firstArray objectAtIndex:row];
}
if( [pickerView isEqual: pickerAnother ]){
secondstr = [secondArray objectAtIndex:row];
}
}