iOS开发-分页栏和选取器的使用
一、分页栏
创建一个新的项目,Subclass of的值选中UIViewController,然后在storyboard中删除根视图,在右下方拖出一个Tab Bar Controller

新增分页,只需从右下方拖出一个普通的View Controller,按住鼠标右键从分页栏控制器拖动到新视图控制器上释放,在弹出面板中的Relationship Segue标题下选中view controllers,使用这些分页要给他们创建Cocoa Touch Class 并关联。
设置分页栏底部的标题与图标,如图:


二、选取器
选取器分为Date Picker和Picker View,前者是日期选取器,后者为一般选取器,可以任意定制用途。

(一)下面实现一个功能:点击按钮,弹出一个警告视图显示当前在日期选取器中选定的日期和时间
1)往Date分页上拖出一个Date Picker和一个按钮,并设置约束


2)创建日期选取器的输出接口,选中Date Picker,按住右键拖动到实现文件,创建一个名为datePicker的输出接口,如图:

3)加载此视图时,选取器都会重置为当前的日期和时间的实现代码,在viewDidLoad方法中编写,如图:

4)实现功能的操作方法,选中按钮,按住右键拖动到实现文件下方,创建一个名为buttonPressed的操作方法,并编写代码,如图:

5)实现效果,显示的是格林威治标准时间

(二)自定义的选取器视图的实现,下面记录几个要点,与上面相同的步骤就不说了
1)将控制器实现为数据源和委托,关联dateSource和delegate,如图:

2)在.h文件中添加协议

3)创建一个数组,用于向选取器提供数据,并将其赋给characterNames属性,代码如下:

4)按钮的方法和数据源、委托的方法实现,结合上下图

5)实现效果

(三) 实现多滚轮选取器
1)添加委托与数据源协议,与上面相同。
<UIPickerViewDelegate,UIPickerViewDataSource>
2)定义两个选取器滚轮相应的索引值常量,以及相应的两个数组

3)其他方法的实现跟一个选取器差不多,这里只是多了一个判断使用哪个滚轮,代码如下:
@implementation DoubleComponentPickerViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 初始化滚轮的内容
    self.fillingTypes = @[@"Ham",
                       @"Turkey",
                       @"Peanut Butter",
                       @"Tuna Salad",
                       @"Chicken Salad",
                       @"Roast Beef",
                       @"Vegemite"];
    self.breadTypes = @[@"White",
                        @"Whole Wheat",
                        @"Rye",@"Sourdough",
                        @"Seven Grain"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)buttonPressed:(id)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];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Thank you for your order" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"Great!" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark Picker Data Soure Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    // 滚轮数目2个
    return ;
}
- (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];
    }
}
@end
4)运行效果

(四)实现有依赖关系的选取器,即其中的内容变化会引起另一个中的内容变化
由于步骤大同小异,这里只po代码了。。。
//
// DependentComponentPickerViewController.m
// Pickers
//
// Created by Jierism on 16/7/19.
// Copyright © 2016年 Jierism. All rights reserved.
// #import "DependentComponentPickerViewController.h" #define kStateComponent 0
#define kZipComponent 1 @interface DependentComponentPickerViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *dependentPicker;
@property (strong,nonatomic) NSDictionary *stateZips;
@property (strong,nonatomic) NSArray *states;
@property (strong,nonatomic) NSArray *zips; @end @implementation DependentComponentPickerViewController - (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[];
self.zips = self.stateZips[selectedState];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)buttonPressed:(id)sender {
NSInteger stateRow = [self.dependentPicker selectedRowInComponent:kStateComponent];
NSInteger zipRow = [self.dependentPicker selectedRowInComponent:kZipComponent]; NSString *state = self.states[stateRow];
NSString *zip = self.states[zipRow]; NSString *title = [[NSString alloc] initWithFormat:@"You selected zip code %@.",zip];
NSString *message = [[NSString alloc] initWithFormat:@"%@ is in %@",zip,state]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
} #pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
} - (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: inComponent:kZipComponent animated:YES];
}
} // 调整选取器占用的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
CGFloat pickerWidth = pickerView.bounds.size.width;
if (component == kZipComponent) {
return pickerWidth/;
}else{
return *pickerWidth/;
}
} @end
注意这里加入了一个.plist文件,涉及到包的使用。什么是包?
包(bundle )是一种特定类型的文件夹,其中的内容遵循特定的结构。应用程序和框架都是包,这个调用返回的包对象表示我们的应用程序。
(五)用选取器实现的滚轮小游戏
相同的内容也不说了,详见代码
1)接口的声明,这里还添加了游戏声音

2)往选取器里面加载图片

3)实现点击按钮,选取器开始滚动,并在滚动过程中隐藏按钮
- (void)showButton {
    self.button.hidden = NO;
}
- (IBAction)spin:(id)sender {
    BOOL win = NO;
    int numInRow = ;
    int lastVal = -;
    for (int i = ; i <  ; i++) {
        int newValue = arc4random_uniform((uint)[self.images count]);
        if (newValue == lastVal) {
            numInRow++;
        }else{
            numInRow = ;
        }
        lastVal = newValue;
        [self.picker selectRow:newValue inComponent:i animated:YES];
        [self.picker reloadComponent:i];
        if (numInRow >= ) {
            win = YES;
        }
    }
    if (_crunchSoundID == ) {
        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:0.5];
    }else{
        [self performSelector:@selector(showButton) withObject:nil afterDelay:0.5];
    }
    // 隐藏按钮
    self.button.hidden = YES;
    self.winLabel.text = @" ";
}
4)播放声音的方法,点击按钮和获胜时会被调用,播放相应的音乐

5)数据源协议和委托的方法

6)运行效果


iOS开发-分页栏和选取器的使用的更多相关文章
- IOS开发—UIDatePicker 日期/时间选取器(滚轮)
		
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
 - IOS开发之简单音频播放器
		
今天第一次接触IOS开发的UI部分,之前学OC的时候一直在模拟的使用Target-Action回调模式,今天算是真正的用了一次.为了熟悉一下基本控件的使用方法,和UI部分的回调,下面开发了一个特别简易 ...
 - ios开发:一个音乐播放器的设计与实现
		
github地址:https://github.com/wzpziyi1/MusicPlauer 这个Demo,关于歌曲播放的主要功能都实现了的.下一曲.上一曲,暂停,根据歌曲的播放进度动态滚动歌词, ...
 - ios开发学习-  简易音乐播放器2 (基于iPhone4s屏幕尺寸)-- 歌词解析--plist文件应用--imageNamed图片加载耗内存
		
声明:(部分图片来自网络,如果侵犯了您的权益请联系我,会尽快删除!) 又是音乐播放器,不过这次和上次不一样了,准确说这次更像播放器了,初学者不建议看这个,可以先看前面一个音乐播放器(1),当然 我没加 ...
 - iOS开发-简单的图片查看器
		
现在你只要拿着手机,不管你Android还是iOS,新闻类的App不可避免都有一个功能就是图片查看,做个专题,查看一下内容,App Store中也有专门针对图片浏览的App,鉴于目前所知有限,无法做到 ...
 - iOS开发——导航栏的一些小设置
		
1.导航栏的隐藏与显示:navigationBarHidden - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; ...
 - 《精通iOS开发》书籍目录
		
1.欢迎来到iOS和Swift世界 2.创建一个新项目 3.实现基本交互 4.更丰富的用户界面 5.自动旋转和自动调整大小 6.多视图应用 7.分页栏与选取器 8.表视图简介 9.导航控制器和表视图 ...
 - 我的iOS开发系列博文
		
之前目录性的总结了发表过的关于OC方面的文章,今天在目录性的总结一下有关iOS开发的文章.走过路过不要错过哦,今天的博文也全都是干货.写技术博客与大家交流一下思想也是不错的. 下面是我的技术博客中有关 ...
 - 重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口
		
原文:重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口 [源码下载] 重新想象 Windows 8 Store Apps (27) - ...
 
随机推荐
- linux page cache和buffer cache
			
主要区别是,buffer cache缓存元信息,page cache缓存文件数据 buffer 与 cache 是作为磁盘文件缓存(磁盘高速缓存disk cache)来使用,主要目的提高文件系统系性能 ...
 - 深入理解ob_flush和flush的区别
			
ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑… 其实, 他们俩的操作对象不同, 有些情况下, flush根本不做什么事情.. ob_* ...
 - ckeditor的配置及使用
			
一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="text/javascript" src=&q ...
 - Windows Embedded Compact 7新特性
			
Windows® Embedded Compact 7是Windows Embedded CE的下一代产品,而Windows Embedded CE这款操作系统面向占用资源少的新颖设备.Windows ...
 - hdu 3549 Flow Problem(增广路算法)
			
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 模板题,白书上的代码... #include <iostream> #include & ...
 - poj 2299 Ultra-QuickSort  (归并排序 求逆序数)
			
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
 - poj 2109 Power of Cryptography (double  精度)
			
题目:http://poj.org/problem?id=2109 题意:求一个整数k,使得k满足kn=p. 思路:exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.log是自然对数,就 ...
 - bzoj3774
			
这算是最小割中比较难的吧 看到选取显然最小割 看到上下左右四个点我感觉肯定和染色相关 注意每个点的收益获得条件是[或],因此我们考虑拆点i', i,分别表示通过四周控制和控制本身的代价 连边s--&g ...
 - IDE模式下安装Windows 7强行改回ACHI后不断重启的解决方法
			
问题描述:用U盘启动进PE装的Win7,由于PE认不出硬盘,只好进BIOS设置硬盘模式为IDE才安装上.结果安装完系统后,在BIOS中强行修改硬盘模式为ACHI模式后,Win7开机不断重启,进不了系统 ...
 - Java [Leetcode 41]First Missing Positive
			
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...