UIPicker accessing items from a plist - objective-c

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

Related

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

UISearchBarDelegate on UITableView with Sections Issue

I'm having issues with implementing the UISearchBarDelegate on my UITableView with sections issue where every time I try to search for something inside the UITableView, either it doesn't display the desired result or it crashes the app with an error saying:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Here's inside my header file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>
{
UITableView *mainTableView;
NSMutableArray *contentsList;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
NSMutableArray *ivSectionKeys;
NSMutableDictionary *ivSectionContents;
}
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) NSMutableArray *contentsList;
#property (nonatomic, retain) NSMutableArray *searchResults;
#property (nonatomic, copy) NSString *savedSearchTerm;
#property (nonatomic, retain) NSMutableArray *sectionKeys;
#property (nonatomic, retain) NSMutableDictionary *sectionContents;
- (void)handleSearchForTerm:(NSString *)searchTerm;
#end
while this is inside my implementation file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mainTableView;
#synthesize contentsList;
#synthesize searchResults;
#synthesize savedSearchTerm;
#synthesize sectionKeys = ivSectionKeys;
#synthesize sectionContents = ivSectionContents;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *colorKey = #"Colors";
NSString *clothingKey = #"Clothing";
NSString *miscKey = #"Misc";
[contents setObject:[NSArray arrayWithObjects:#"Red", #"Blue", nil] forKey:colorKey];
[contents setObject:[NSArray arrayWithObjects:#"Pants", #"Shirt", #"Socks", nil] forKey:clothingKey];
[contents setObject:[NSArray arrayWithObjects:#"Wankle Rotary Engine", nil] forKey:miscKey];
[keys addObject:clothingKey];
[keys addObject:miscKey];
[keys addObject:colorKey];
self.sectionKeys = keys;
self.sectionContents = contents;
// Restore search term
if (self.savedSearchTerm)
{
self.searchDisplayController.searchBar.text = self.savedSearchTerm;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Save the state of the search UI so that it can be restored if the view is re-created.
self.savedSearchTerm = self.searchDisplayController.searchBar.text;
self.searchResults = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.mainTableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)handleSearchForTerm:(NSString *)searchTerm
{
self.savedSearchTerm = searchTerm;
if (self.searchResults == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
self.searchResults = array;
array = nil;
}
[self.searchResults removeAllObjects];
if ([self.savedSearchTerm length] != 0)
{
for (NSString *currentString in self.sectionContents)
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[self.searchResults addObject:currentString];
}
}
}
}
#pragma mark -
#pragma mark UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger sections = [self.sectionKeys count];
return sections;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
NSString *key = [self.sectionKeys objectAtIndex:section];
return key;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
NSString *key = [self.sectionKeys objectAtIndex:section];
NSArray *contents = [self.sectionContents objectForKey:key];
NSInteger rows;
if (tableView == [[self searchDisplayController] searchResultsTableView])
rows = [self.searchResults count];
else
rows = [contents count];
NSLog(#"rows is: %d", rows);
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [self.sectionKeys objectAtIndex:indexPath.section];
NSArray *contents = [self.sectionContents objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:indexPath.row];
if (tableView == [self.searchDisplayController searchResultsTableView])
contentForThisRow = [self.searchResults objectAtIndex:indexPath.row];
else
contentForThisRow = [contents objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = contentForThisRow;
return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self handleSearchForTerm:searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
self.savedSearchTerm = nil;
[self.mainTableView reloadData];
}
#end
I think my issue is inside my for-loop but I'm not really sure.
You need to update all of your table view data source and delegate methods to handle both tables (you only do this in some):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger sections = [self.sectionKeys count];
return sections;
}
should be:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.tableView) {
NSInteger sections = [self.sectionKeys count];
return sections;
} else { // assume it's the search table
// replace this with the actual result
return numberOfSectionsForSearchTable;
}
}

Add NSTableView data to NSPopUpButton items

I have a NSTableView where items can be added and deleted. Once items have been added to the table, I would like those items to also show as items for an NSPopUpButton. I tried the addItemsWithTitles: method but it gives me an error.
#import "TableController.h"
#import "Favorites.h"
#interface TableController ()
#property NSMutableArray *array;
#property (weak) IBOutlet NSTableView *tableView;
#property (weak) IBOutlet NSPopUpButton *popButton;
#end
#implementation TableController
- (id)init {
self = [super init];
if (self) {
_array = [[NSMutableArray alloc] init];
}
return self;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [_array count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Favorites *fav = [_array objectAtIndex:row];
NSString *ident = [tableColumn identifier];
return [fav valueForKey:ident];
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Favorites *fav = [_array objectAtIndex:row];
NSString *ident = [tableColumn identifier];
[fav setValue:object forKey:ident];
}
- (IBAction)add:(id)sender {
[_array addObject:[[Favorites alloc] init]];
[_tableView reloadData];
[_popButton addItemsWithTitles:_array];
}
-(IBAction)delete:(id)sender {
NSInteger row = [_tableView selectedRow];
[_tableView abortEditing];
if (row != -1) {
[_array removeObjectAtIndex:row];
}
[_tableView reloadData];
}
#end
So I tried logging the objectAtIndex:0 for the array and didn't get a string but received some numbers instead:
Array string is <Favorites: 0x10013e820>
And also for reference my Favorites class is
#import "Favorites.h"
#interface Favorites ()
#property (copy) NSString *location;
#end
#implementation Favorites
- (id)init {
self = [super init];
if (self) {
_location = #"City, State or ZIP";
}
return self;
}
#end
Your array has instances of your class, Favorites, and in your addItemsWithTiles: method you are adding those instances, not some string property of those instances, which is what I presume you want. If you use [_array valueForKey:#"whateverKeyHasYourStrings"] as the argument to addItemsWithTitles: instead of just _array, I think it will work ok.

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.

UIMenuController: how to tell which menuItem is pressed?

I have a UILongPressGestureRecognizer on a UITableViewCell that displays a UIMenuController with some categories the user can pick from. These categories are stored in a NSMutableArray and can be customized by the user. I want to use one method to handle all category-presses in the UIMenuController. How can I pass the index of the selected UIMenuItem? Thanks in advance.
#pragma mark -
#pragma mark Custom Quick Menu Item
#interface QuickMenuItem : UIMenuItem
{
}
#property (nonatomic, retain) NSIndexPath *indexPath;
#property (nonatomic, retain) NSMutableString *category;
#end
#implementation QuickMenuItem
#synthesize indexPath, category;
- (void)dealloc
{
[indexPath release];
[category release];
[super dealloc];
}
#end
#pragma mark -
#pragma mark Handle UILongPressGesture
- (void)handleLongItemPress:(UILongPressGestureRecognizer *)longPressRecognizer
{
if (longPressRecognizer.state == UIGestureRecognizerStateBegan)
{
NSIndexPath *pressedIndexPath = [queueTableView indexPathForRowAtPoint:[longPressRecognizer locationInView:queueTableView]];
if (pressedIndexPath && (pressedIndexPath.row != NSNotFound) && (pressedIndexPath.section != NSNotFound))
{
[self becomeFirstResponder];
UIMenuController *menuController = [UIMenuController sharedMenuController];
NSMutableArray *categoryMenuItems = [NSMutableArray array];
NSEnumerator *e = [self.stats.categories objectEnumerator]; // array with categories
NSMutableString *cat;
while (cat = [e nextObject])
{
QuickMenuItem *categoryMenuItem = [[QuickMenuItem alloc] initWithTitle:cat action:#selector(quickMenuCategoryPressed:)];
categoryMenuItem.indexPath = pressedIndexPath;
categoryMenuItem.category = cat;
[categoryMenuItems addObject:categoryMenuItem];
[categoryMenuItem release];
}
menuController.menuItems = [NSArray arrayWithArray:categoryMenuItems];
[menuController setTargetRect:[queueTableView rectForRowAtIndexPath:pressedIndexPath] inView:queueTableView];
[menuController setMenuVisible:YES animated:YES];
}
}
}
- (void)quickMenuCategoryPressed:(UIMenuController *)menuController
{
QuickMenuItem *menuItem = [[[UIMenuController sharedMenuController] menuItems] objectAtIndex: ??]; // How to tell which category is selected?
if (menuItem.indexPath)
{
[self resignFirstResponder];
// Perform action
}
}
You'll probably need to create some dynamic selectors, as described at Dynamic UIMenuItems with #selector and dynamic methods