Implementation file can't see Constant declared in header file - objective-c

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

Related

use of undeclared identifier 'prepareForSegue'

I'm having two issues with the below code. First, the userCategorySave Parse cloud function is supposed to run when the submit button is tapped, however there are no signs of it running in my logs.
The other problem is an error I'm getting that states use of undeclared identifier 'prepareForSegue'. I was under the impression that prepareForSegue was a standard part of an iOS app, and didn't need to be declared. Why is this class in particular giving me this error?
#import "CriteriaViewController.h"
#interface CriteriaViewController ()
#property (weak, nonatomic) IBOutlet UISegmentedControl *itemConditionSegment;
#property (weak, nonatomic) IBOutlet UISegmentedControl *itemLocationSegment;
#property (weak, nonatomic) IBOutlet UIButton *submitButton;
#end
#implementation CriteriaViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)itemConditionSegment:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
self.itemCondition = #"new";
}
else{
self.itemCondition = #"any";
}
}
- (IBAction)itemLocationSegment:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
self.itemLocation = #"US";
}
else if (selectedSegment == 1) {
self.itemLocation = #"WorldWide";
}
}
//add all the info to users respective new category object
- (IBAction)submitButton:(id)sender
{
if (self.minPrice.text.length > 0 && self.maxPrice.text.length > 0)
{
[PFCloud callFunctionInBackground:#"userCategorySave"
withParameters:#{#"categoryId": self.chosenCategory,
#"minPrice": self.minPrice.text,
#"maxPrice": self.maxPrice.text,
#"itemCondition": self.itemCondition,
#"itemLocation": self.itemLocation,}
block:^(NSString *result, NSError *error) {
if (!error) {
NSLog(#"Criteria successfuly saved.");
[self performSegueWithIdentifier:#"ShowMatchCenterSegue" sender:self];
}
}];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
#end
You did not close the } correctly.so add one brase in the above method prepare for seque.

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

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 won't show

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

Use of undeclared identifier

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.