Use of undeclared identifier - objective-c

Am trying to configure a two component picker, and am getting some errors which I have commented out:
Implementation file:
#import "BIDDoubleComponentPickerViewController.h"
#implementation BIDDoubleComponentPickerViewController
#synthesize doublePicker;
#synthesize fillingTypes;
#synthesize breadTypes;
- (IBAction)buttonPressed:(id)sender
{
NSInteger fillingRow = [doublePicker selectedRowInComponent:kFillingComponent]; // Use of undeclared identifier 'kFillingComponent'
NSInteger breadRow = [doublePicker selectedRowInComponent:kBreadComponent]; // Use of undeclared identifier 'kBreadComponent'
NSString *bread = [breadTypes objectAtIndex:breadRow];
NSString *filling = [fillingTypes objectAtIndex:fillingRow];
NSString *message = [[NSString alloc]initWithFormat:#"Your %# on %# bread 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];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *fillingArray = [[NSArray alloc] initWithObjects:#"Ham", #"Turkey", #"Peanut Butter", #"Tuna Salad", #"Chicken Salad", #"Roast Beef", #"Vegemite", nil];
self.fillingTypes = fillingArray;
NSArray *breadArray = [[NSArray alloc] initWithObjects:#"White", #"Whole Wheat", #"Rye", #"Sourdough Bread", #"Seven Grain", nil];
self.breadTypes = breadArray;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.doublePicker = nil;
self.breadTypes = nil;
self.fillingTypes = nil;
}
#pragma mark - View lifecycle
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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 identifier 'kBreadComponent'
return [self.breadTypes.count]; // Expected identifier
return [self.fillingTypes objectAtIndex:row]; // Use of undeclared identifier 'row'
}
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == kBreadComponent) { // Use of undeclared identifier 'kBreadComponent'
return [self.breadTypes objectAtIndex:row];
return [self.fillingTypes objectAtIndex:row];
}
}
#end
Interface file:
#import <UIKit/UIKit.h>
#interface BIDDoubleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
#property (strong, nonatomic) IBOutlet UIPickerView *doublePicker;
#property (strong, nonatomic) NSArray *fillingTypes;
#property (strong, nonatomic) NSArray *breadTypes;
- (IBAction)buttonPressed:(id)sender;
#end

kBreadComponent and kFillingComponent are not declared anywhere. If they're declared in a header (.h) file, you need to #import it.

Your numberOfRowsInComponent and titleForRow methods are similar, and they are wrong: they have two consequent unconditional return, and this means that the second one will never be executed.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.breadTypes.count;
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == kBreadComponent) {
return [self.fillingTypes objectAtIndex:row];
}
return nil; // Or something, empty string for example. Method requires to return a NSString, so you have to return at least nil;
}
Still you have to find kBreadComponent and kFillingComponent and #import the file with the definitions of them.

Related

How can I display a certain message/alert after the user chooses certain values from a multicomponent picker in xcode?

I'm trying to display a message when the user chooses 2 values from a double component picker.
In case the user’s choice was "High Fever" and "Strong Cough", use an Alert View to display a message indicating: 'you should do the PCR Test'.
In case the user’s choice was a selection of "High Fever" and "Medium Cough" or "Medium Fever" and "Strong Cough", display a message indicating: 'it is just a normal flu'.
In case of all other possible selections, display a message: 'Relax... Nothing to worry about!'
Here is the code below:
#import "FirstViewController.h"
#define kFeverComponent 0
#define kCoughComponent 1
#interface FirstViewController ()
#property (strong, nonatomic) NSArray *feverTypes;
#property (strong, nonatomic) NSArray *coughTypes;
#property (weak, nonatomic) IBOutlet UIPickerView *dependentPicker;
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.feverTypes = #[#"High Fever", #"Medium Fever", #"No Fever"];
self.coughTypes = #[#"Strong Cough", #"Medium Cough", #"No Cough"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)symptomsButtonPressed:(id)sender {
NSInteger feverRow = [self.dependentPicker selectedRowInComponent: kFeverComponent];
NSInteger coughRow = [self.dependentPicker selectedRowInComponent: kCoughComponent];
NSString *fever = self.feverTypes[feverRow];
NSString *cough = self.coughTypes[coughRow];
NSString *message = [[NSString alloc] initWithFormat:
#"You chose %# and %#.", fever, cough];
UIAlertController *alert =
[UIAlertController
alertControllerWithTitle:#"Thank you for choosing" message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:#"Okay"
style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:
(NSInteger)component {
if (component == kCoughComponent) {
return [self.coughTypes count];
} else {
return [self.coughTypes count];
}
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
if (component == kCoughComponent) {
return self.coughTypes[row];
} else {
return self.feverTypes[row];
}
}
#end
You need to implement pickerview:didSelectRow:inComponent delegate method. So make your class adhere to the UIPickerViewDelegate protocol by adding it to the interface definition e.g.
#interface FirstViewController () < UIPickerViewDelegate >
and then implement e.g.
- ( void ) pickerView:( UIPickerView * ) pickerView
didSelectRow:( NSInteger ) row
inComponent:( NSInteger ) component
{
// logic here e.g.
NSInteger fever = [pickerView selectedRowInComponent:kCoughComponent];
NSInteger cough = [pickerView selectedRowInComponent:kFeverComponent];
NSString * msg;
if ( fever >= threshold && cough >= threshold )
{
msg = #"Seek help";
}
else
{
msg = #"All fine";
}
// Show alert with msg
}

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

Adding a UI Picker to a UITextView

I'm trying to add a UIPicker to a UITextView when it is selected to choose an age. I found a previous thread and trued to implement the code but I can't get it to work properly. When I build the program the picker shows up, but there are no values in it. Any ideas as to what I'm doing wrong? Also, I'm getting two yellow errors here saying "Assigning to 'id' from incompatible type 'ViewController *const __strong" here:
pickerView.delegate = self;
pickerView.dataSource = self;
Thank you.
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *ageTextField;
#end
#implementation ViewController
#synthesize ageTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPickerView *pickerView =[[UIPickerView alloc]init];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator=YES;
ageTextField.inputView=pickerView;
}
- (void)pickerView:(UIPickerView *)PickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
NSMutableArray *ageArray = [[NSMutableArray alloc]init];
[ageArray addObject:#"1"];
[ageArray addObject:#"2"];
[ageArray addObject:#"3"];
[ageArray addObject:#"4"];
ageTextField.text=[ageArray objectAtIndex:row];
}
#interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) UIPickerView *pickerView;
#property (strong, nonatomic) NSArray *pickerElements;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
self.ageTextField.inputView = self.pickerView;
self.pickerElements = #[#"text1", #"text2", #"text3", #"text4"];
[self pickerView:self.pickerView
didSelectRow:0
inComponent:0];
}
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.pickerElements count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.pickerElements objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.ageTextField.text = [self.pickerElements objectAtIndex:row];
}
#end

UIPicker accessing items from a plist

I have a plist that is being loaded into a UIPickerView. I can access the array info and have it loaded into the picker in one of the components. What I'm trying to do is to access Item 0 or Item 1 and have them displayed on a UILabel based on a condition.
I can't figure out how to access Item 0 or Item 1 (the string values). Any tips on how I'd go about doing this? thanks for the help.
here's an image to clarify what I'm talking about:
pickerViewcontroller.h
#import <UIKit/UIKit.h>
#define kStateComponent 0
#define kZipComponent 1
#interface PickerViewController : UIViewController
<UIPickerViewDataSource,UIPickerViewDelegate>{
IBOutlet UIPickerView *dpicker;
NSDictionary *stateZip;
NSArray *states;
NSArray *zips;
}
#property (nonatomic,retain) UIPickerView *dpicker;
#property (nonatomic,retain) NSDictionary *stateZip;
#property (nonatomic, retain) NSArray *states;
#property (nonatomic, retain) NSArray *zips;
#end
pickerViewcontroller.m
#import "PickerViewController.h"
#implementation PickerViewController
#synthesize dpicker;
#synthesize stateZip;
#synthesize states;
#synthesize zips;
-(void) viewDidLoad{
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath =[bundle pathForResource:#"plistfilename" ofType:#"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.stateZip=dictionary;
[dictionary release];
NSArray *component = [self.stateZip allKeys];
NSArray *sorted =[component sortedArrayUsingSelector:#selector(compare:)];
self.states=sorted;
NSString *selectedState = [self.states objectAtIndex:0];
NSArray *array = [stateZip objectForKey:selectedState];
self.zips = array;
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[dpicker release];
[stateZip release];
[states release];
[zips release];
[super dealloc];
}
#pragma mark-
#pragma mark picker Data Source Methods
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerview
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == kStateComponent)
return [self.states count];
return [self.zips count];
}
#pragma mark picker delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if(component == kStateComponent)
return[self.states objectAtIndex:row];
return [self.zips objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == kStateComponent)
{
NSString *selectedState = [self.states objectAtIndex:row];
NSArray *array=[stateZip objectForKey:selectedState];
self.zips=array;
[dpicker selectRow:0 inComponent:kZipComponent animated:YES];
[dpicker reloadComponent:kZipComponent];
}
}
#endhere