summaryrefslogtreecommitdiff
path: root/platform/ios/Classes/MuChoiceFieldController.m
blob: 3a3aee9411a6d8f9067de6e07bef37e6261367fb (plain)
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
#import "MuChoiceFieldController.h"

@interface MuChoiceFieldController ()
- (IBAction)onCancel:(id)sender;
- (IBAction)onOkay:(id)sender;
@property (retain, nonatomic) IBOutlet UIPickerView *picker;
@end

@implementation MuChoiceFieldController
{
	void (^okayBlock)(NSArray *);
	NSArray *choices;
	NSInteger selected;
}

- (instancetype)initWithChoices:(NSArray *)_choices okayAction:(void (^)(NSArray *))block
{
	self = [super initWithNibName:@"MuChoiceFieldController" bundle:nil];
	if (self)
	{
		okayBlock = Block_copy(block);
		choices = [_choices retain];
		selected = 0;
	}
	return self;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	_picker.dataSource = self;
	_picker.delegate = self;
	// Do any additional setup after loading the view from its nib.
}

- (void)dealloc
{
	[okayBlock release];
	[choices release];
	[_picker release];
	[super dealloc];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return choices.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return choices[row];
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	selected = row;
}

- (IBAction)onOkay:(id)sender
{
	if (selected >= 0 && selected < choices.count)
		okayBlock(@[choices[selected]]);
	[self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)onCancel:(id)sender
{
	[self dismissViewControllerAnimated:YES completion:nil];
}

@end