MainViewController.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
    UIPickerView *locationsPicker;
    UIToolbar    *accessoryView;
    UITextField  *homeCityField;
    UITextField  *destinationField;
    
    NSArray      *locations;
}

@property (nonatomic, readonly) UIPickerView *locationsPicker;
@property (nonatomic, readonly) UIToolbar *accessoryView;

- (void)onLocationSelection;

@end

MainViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#import "MainViewController.h"

@implementation MainViewController

- (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

- (id) init {
    if ( (self = [super init]) ) {
        locations = [[NSArray alloc] initWithObjects:@"New York", @"Chicago", @"Memphis", @"California", @"Seattle",@"London",@"Paris", nil];
    }
    
    return self;
}

- (void)dealloc
{
    [homeCityField release];
    [destinationField release];
    [locations release];
    [locationsPicker release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)loadView
{
    UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    
    homeCityField = [[UITextField alloc] initWithFrame:CGRectMake(100, 50, 120, 30)];
    destinationField = [[UITextField alloc] initWithFrame:CGRectMake(100,120, 120, 30)];
    
    homeCityField.borderStyle = UITextBorderStyleRoundedRect;
    destinationField.borderStyle = UITextBorderStyleRoundedRect;
    
    homeCityField.inputView = self.locationsPicker;
    destinationField.inputView = self.locationsPicker;
    homeCityField.inputAccessoryView = self.accessoryView;
    destinationField.inputAccessoryView = self.accessoryView;
    
    [rootView addSubview:homeCityField];
    [rootView addSubview:destinationField];
    
    self.view = rootView;
    [rootView release];
}

- (void)onLocationSelection {
    NSInteger row = [self.locationsPicker selectedRowInComponent:0];
    
    if ( [destinationField isFirstResponder] ) {
        destinationField.text = [locations objectAtIndex:row];
        [destinationField resignFirstResponder];
    } else {
        homeCityField.text = [locations objectAtIndex:row];
        [homeCityField 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];
}

@end