Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7640 views
1
#import "MuChoiceFieldController.h"
2
3
@interface MuChoiceFieldController ()
4
- (IBAction)onCancel:(id)sender;
5
- (IBAction)onOkay:(id)sender;
6
@property (retain, nonatomic) IBOutlet UIPickerView *picker;
7
@end
8
9
@implementation MuChoiceFieldController
10
{
11
void (^okayBlock)(NSArray *);
12
NSArray *choices;
13
NSInteger selected;
14
}
15
16
- (id)initWithChoices:(NSArray *)_choices okayAction:(void (^)(NSArray *))block
17
{
18
self = [super initWithNibName:@"MuChoiceFieldController" bundle:nil];
19
if (self)
20
{
21
okayBlock = Block_copy(block);
22
choices = [_choices retain];
23
selected = 0;
24
}
25
return self;
26
}
27
28
- (void)viewDidLoad
29
{
30
[super viewDidLoad];
31
_picker.dataSource = self;
32
_picker.delegate = self;
33
// Do any additional setup after loading the view from its nib.
34
}
35
36
- (void)dealloc
37
{
38
[okayBlock release];
39
[choices release];
40
[_picker release];
41
[super dealloc];
42
}
43
44
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
45
{
46
return 1;
47
}
48
49
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
50
{
51
return [choices count];
52
}
53
54
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
55
{
56
return [choices objectAtIndex:row];
57
}
58
59
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
60
{
61
selected = row;
62
}
63
64
- (IBAction)onOkay:(id)sender
65
{
66
if (selected >= 0 && selected < [choices count])
67
okayBlock([NSArray arrayWithObject:[choices objectAtIndex:selected]]);
68
[self dismissViewControllerAnimated:YES completion:nil];
69
}
70
71
- (IBAction)onCancel:(id)sender
72
{
73
[self dismissViewControllerAnimated:YES completion:nil];
74
}
75
76
@end
77
78