《精通iOS开发》第07章学习笔记

实现日期选取器

日期数据初始化:

1
2
3
4
5
6
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSDate *now = [NSDate date];
[self.datePicker setDate:now animated:YES];
}

获取日期选取器的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (IBAction)buttonPressed:(UIButton *)sender {
NSDate *selected = [self.datePicker date];
NSString *message = [[NSString alloc]
initWithFormat:@"The date and time you selected is: %@", selected];

UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Date and Time Selected"
message:message
delegate:nil
cancelButtonTitle:@"That's so true!"
otherButtonTitles:nil];
[alert show];
}

实现单滚轮选取器

声明遵循的协议:

1
2
3
4
@interface BIDSingleComponentPickerViewController
: UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

@end

实现数据源和委托:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
return [self.characterNames count];
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return self.characterNames[row];
}

获取选取器的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (IBAction)buttonPressed:(UIButton *)sender {
NSInteger row = [self.singlePicker selectedRowInComponent:0];
NSString *selected = self.characterNames[row];
NSString *title =
[[NSString alloc] initWithFormat:@"You selected %@!", selected];
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:title
message:@"Thank you for choosing."
delegate:nil
cancelButtonTitle:@"You're Welcome"
otherButtonTitles:nil];
[alert show];
}

实现多滚轮选取器

实现数据源和委托:

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
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
if (component == kBreadComponent) {
return [self.breadTypes count];
} else {
return [self.fillingTypes count];
}
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
if (component == kBreadComponent) {
return self.breadTypes[row];
} else {
return self.fillingTypes[row];
}
}

获取选取器的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (IBAction)buttonPressed:(UIButton *)sender {
NSInteger fillingRow = [self.doublePicker selectedRowInComponent:kFillingComponent];
NSInteger breadRow = [self.doublePicker selectedRowInComponent:kBreadComponent];

NSString *filling = self.fillingTypes[fillingRow];
NSString *bread = self.breadTypes[breadRow];

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];
}

实现依赖型多滚轮选取器

获取包数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL =
[bundle URLForResource:@"statedictionary" withExtension:@"plist"];

self.stateZips = [NSDictionary dictionaryWithContentsOfURL:plistURL];

NSArray *allStates = [self.stateZips allKeys];
NSArray *sortedStates =
[allStates sortedArrayUsingSelector:@selector(compare:)];

self.states = sortedStates;

NSString *selectedState = self.states[0];
self.zips = self.stateZips[selectedState];
}

实现数据源和委托:

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
#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];
} else {
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[row];
} else {
return self.zips[row];
}
}

- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (component == kStateComponent) {
NSString *selectedState = self.states[row];
self.zips = self.stateZips[selectedState];
[self.dependentPicker reloadComponent:kZipComponent];
[self.dependentPicker selectRow:0 inComponent:kZipComponent animated:YES];
}
}

获取选取器的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (IBAction)buttonPressed:(UIButton *)sender {
NSInteger stateRow =
[self.dependentPicker selectedRowInComponent:kStateComponent];
NSInteger zipRow =
[self.dependentPicker selectedRowInComponent:kZipComponent];

NSString *state = self.states[stateRow];
NSString *zip = self.zips[zipRow];
NSString *title =
[[NSString alloc] initWithFormat:@"You selected zip code %@", zip];
NSString *message =
[[NSString alloc] initWithFormat:@"%@ is in %@", zip, state];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}

设置每个滚轮组件的宽度:

1
2
3
4
5
6
7
8
- (CGFloat)pickerView:(UIPickerView *)pickerView
widthForComponent:(NSInteger)component {
if (component == kZipComponent) {
return 90;
} else {
return 200;
}
}

实现自定义视图选取器

实现数据源和委托:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 5;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
return [self.images count];
}

#pragma mark Picker Delegate Methods
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UIImage *image = self.images[row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
return imageView;
}

获取选取器的值:

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
- (IBAction)spin:(UIButton *)sender {
BOOL win = NO;
int numInRow = 1;
int lastVal = -1;

for (int i = 0; i < 5; i++) {
int newValue = random() % [self.images count];

if (newValue == lastVal) {
numInRow++;
} else {
numInRow = 1;
}

lastVal = newValue;

[self.picker selectRow:newValue inComponent:i animated:YES];
[self.picker reloadComponent:i];

if (numInRow >= 3) {
win = YES;
}
}

if (crunchSoundID == 0) {
NSString *path =
[[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"];
NSURL *soundURL = [NSURL fileURLWithPath:path];

AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL,
&crunchSoundID);
}

AudioServicesPlaySystemSound(crunchSoundID);

if (win) {
[self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
} else {
[self performSelector:@selector(showButton) withObject:nil afterDelay:.5];
}

self.button.hidden = YES;
self.winLabel.text = @"";
}

设置每个滚轮组件内容的高度:

1
2
3
4
- (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component {
return 70;
}

播放音乐文件:

1
2
3
4
5
6
7
8
9
10
11
12
- (void)playWinSound {
if (winSoundID == 0) {
NSURL *soundURL =
[[NSBundle mainBundle] URLForResource:@"win" withExtension:@"wav"];

AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &winSoundID);
}

AudioServicesPlaySystemSound(winSoundID);
self.winLabel.text = @"WINNING!";
[self performSelector:@selector(showButton) withObject:nil afterDelay:1.5];
}
坚持原创技术分享,您的支持将鼓励我继续创作!