Skip to content

Commit

Permalink
example on using pickerView: M18Coffee
Browse files Browse the repository at this point in the history
  • Loading branch information
vikingosegundo committed Jun 17, 2011
1 parent 0db97e6 commit d8ef53c
Show file tree
Hide file tree
Showing 24 changed files with 2,474 additions and 485 deletions.
3 changes: 2 additions & 1 deletion ExtendingCells/Classes/FirstViewController.m
Expand Up @@ -9,7 +9,7 @@
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
selectedIndexPath = nil;
selectedIndexPath = [[NSIndexPath indexPathForRow:-1 inSection:-1] retain];
articles = [[NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"one", @"two", @"three",
@"four", @"five", @"six",
@"seven", @"eight", @"nine",
Expand Down Expand Up @@ -61,6 +61,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}

id key = [[articles allKeys] objectAtIndex:indexPath.section];
cell.textLabel.text = [[articles objectForKey:key] objectAtIndex:indexPath.row];
return cell;
Expand Down
2 changes: 2 additions & 0 deletions M18Coffee/.gitattributes
@@ -0,0 +1,2 @@
*.pbxproj -crlf -diff -merge
*.xib/ -crlf -diff -merge
13 changes: 13 additions & 0 deletions M18Coffee/.gitignore
@@ -0,0 +1,13 @@
.DS_Store
*.swp
*~.nib

build/
*.psd

*.pbxuser
*.perspective
*.perspectivev3

*.mode1v3
*.mode2v3
19 changes: 19 additions & 0 deletions M18Coffee/Classes/M18CoffeeAppDelegate.h
@@ -0,0 +1,19 @@
//
// M18CoffeeAppDelegate.h
// M18Coffee
//
// Created by Manuel on 19.01.10.
// Copyright apparatschik 2010. All rights reserved.
//

@interface M18CoffeeAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;
UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

47 changes: 47 additions & 0 deletions M18Coffee/Classes/M18CoffeeAppDelegate.m
@@ -0,0 +1,47 @@
//
// M18CoffeeAppDelegate.m
// M18Coffee
//
// Created by Manuel on 19.01.10.
// Copyright apparatschik 2010. All rights reserved.
//

#import "M18CoffeeAppDelegate.h"
#import "RootViewController.h"


@implementation M18CoffeeAppDelegate

@synthesize window;
@synthesize navigationController;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch

[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}


@end

25 changes: 25 additions & 0 deletions M18Coffee/Classes/M18PickerViewController.h
@@ -0,0 +1,25 @@
//
// M18PickerViewController.h
// M18Coffee
//
// Created by vikingosegundo on 18.01.10.
// Copyright 2010 apparatschik. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface M18PickerViewController : UIViewController {
IBOutlet UIPickerView *pickerView;
NSArray *coffeeKinds;
NSArray *coffeeOptions;
NSArray *latteOptions;
}

-(IBAction) addCoffee;

@property(nonatomic,retain) NSArray *coffeeKinds;
@property(nonatomic,retain) NSArray *coffeeOptions;
@property(nonatomic, retain)NSArray *latteOptions;

@end
167 changes: 167 additions & 0 deletions M18Coffee/Classes/M18PickerViewController.m
@@ -0,0 +1,167 @@
//
// M18PickerViewController.m
// M18Coffee
//
// Created by vikingosegundo on 18.01.10.
// Copyright 2010 apparatschik. All rights reserved.
//

#import "M18PickerViewController.h"
#import "RootViewController.h"


@interface M18PickerViewController ()
NSInteger numberOfComponents;
@end


@implementation M18PickerViewController



@synthesize coffeeKinds;
@synthesize coffeeOptions;
@synthesize latteOptions;

- (void)viewDidLoad {
[super viewDidLoad];
[[self.view viewWithTag:1] setHidden:NO];
[self.view becomeFirstResponder];
coffeeKinds = [[NSArray alloc] initWithObjects:@"",@"Coffee",@"Cappuchino",@"Latte",@"Espresso",@"Milkcoffee",nil];

coffeeOptions = [[NSArray alloc] initWithObjects:@"",@"soymilk",@"doubled",@"cremé",@"simple",@"macchiato",nil];
latteOptions = [[NSArray alloc] initWithObjects:@"",@"Caramel", @"Hazelnut",@"Almond", nil];
UIButton *button = (UIButton*)[self.view viewWithTag:2];
[button setEnabled:NO];
numberOfComponents = 3;
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}



- (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 {
[self.coffeeKinds release], self.coffeeKinds = nil;
[self.coffeeOptions release], self.coffeeOptions=nil;
[super dealloc];
}
- (IBAction) addCoffee{
UIButton *button =(UIButton*)[self.view viewWithTag:2];

UITableView *tv =(UITableView*)[[self.view window] viewWithTag:100];

UIPickerView* picker = (UIPickerView*)[self.view viewWithTag:1];


NSString *row1 = [NSString stringWithFormat: @"%i", [picker selectedRowInComponent:0]+1];
NSString *row2 = [NSString stringWithFormat: @"%@", [self.coffeeKinds objectAtIndex:[picker selectedRowInComponent:1]]];
NSString *row3 = [NSString stringWithFormat: @"%@", [self.coffeeOptions objectAtIndex:[picker selectedRowInComponent:2]] ];

[(RootViewController *)tv.dataSource addRow:[NSString stringWithFormat:@"%@ %@ %@",row1,row2,row3]];
[picker selectRow:0 inComponent:0 animated:YES ];
[picker selectRow:0 inComponent:1 animated:YES ];
[picker selectRow:0 inComponent:2 animated:YES ];
[button setEnabled:NO];
[button setTitle:@"" forState:UIControlStateNormal];
[button setHighlighted:NO];
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pv{
return numberOfComponents;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSInteger i;
if (component == 0) {
i= 7;
} else if (component == 1) {
i= [self.coffeeKinds count];

} else if (component == 2) {
i= [self.coffeeOptions count];
} else {
i = [self.latteOptions count];
}

return i;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

NSString *string;
if (component == 0) {
string = [NSString stringWithFormat:@"%i",row+1];
} else if (component == 1) {
string = [NSString stringWithFormat:@"%@", [self.coffeeKinds objectAtIndex:row]];
if ([string isEqualToString: @"" ]) {
[[self.view viewWithTag:3] setHidden:YES];
}
}else if (component == 2) {
string = [NSString stringWithFormat:@"%@", [self.coffeeOptions objectAtIndex:row]];
}else if (component == 3) {
string = [NSString stringWithFormat:@"%@", [self.latteOptions objectAtIndex:row]];
}
return string;
}
- (CGFloat)pickerView:(UIPickerView *)pv widthForComponent:(NSInteger)component {
CGFloat f;
if (component == 0) {
f = 30;

} else{
f = [[pv viewWithTag:1] frame].size.width-80;
f = f/(numberOfComponents -1);
}
return f;
}
- (void)pickerView:(UIPickerView *)pv didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
UIButton *button =(UIButton*)[self.view viewWithTag:2];

if ([pv selectedRowInComponent:1] == 0) {
[button setEnabled:NO];
[button setHighlighted:NO];
} else {
[button setEnabled:YES];
[button setHighlighted:YES];
}

UIPickerView* picker = (UIPickerView*)[self.view viewWithTag:1];
NSString *row1 = [NSString stringWithFormat: @"%i", [picker selectedRowInComponent:0]+1];
NSString *row2 = [NSString stringWithFormat: @"%@", [self.coffeeKinds objectAtIndex:[picker selectedRowInComponent:1]]];
NSString *row3 = [NSString stringWithFormat: @"%@", [self.coffeeOptions objectAtIndex:[picker selectedRowInComponent:2]] ];
[button setTitle:[NSString stringWithFormat:@"%@ %@ %@",row1,row2,row3] forState:UIControlStateNormal];
if (component == 1 && row == 3) {
numberOfComponents = 4;
[pv reloadAllComponents];
} else if ([pv selectedRowInComponent:1] !=3) {
numberOfComponents = 3;
[pv reloadAllComponents];
}

if (numberOfComponents == 4) {
NSString *row4 = [self.latteOptions objectAtIndex:[picker selectedRowInComponent:3]];
[button setTitle:[button.titleLabel.text stringByAppendingFormat:@" %@", row4] forState:UIControlStateNormal];
}


}


@end
27 changes: 27 additions & 0 deletions M18Coffee/Classes/RootViewController.h
@@ -0,0 +1,27 @@
//
// RootViewController.h
// M18Coffee
//
// Created by Manuel on 19.01.10.
// Copyright apparatschik 2010. All rights reserved.
//
#import "M18PickerViewController.h"

@interface RootViewController : UITableViewController {
M18PickerViewController* pickerViewController;
NSMutableArray *data;
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}

-(IBAction) togglePicker;
-(void) addRow:(NSString*) row;
@property(nonatomic,retain) M18PickerViewController *pickerViewController;
@property(nonatomic,retain) NSMutableArray *data;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

0 comments on commit d8ef53c

Please sign in to comment.