今天到UITabBarController 结合 UIPickView, 这里一共有5个实现, 由浅到易。

其实在IB上面使用UITabBarController很简单, 就像平常拖控件一样拖到界面上面, 然后把Tab Bar Item拉到UITabBarController就可以增加底下的tab, 再分别指定底下tab就可以关联到对应的ViewController。

BIDAppDelegate.h

#import <UIKit/UIKit.h>

@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) IBOutlet UITabBarController *rootController; @end

BIDAppDelegate.m

#import "BIDAppDelegate.h"

@implementation BIDAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil]; // 因为与IB做了IBOutlet的关联, 所以以这种方式加载xib到对应的controller
self.window.rootViewController = self.rootController; // 与IB做了IBOutlet的关联
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} @end

=============================================================

BIDDatePickerViewController第一个Tab所对应的视图控制器:

BIDDatePickerViewController.h

#import <UIKit/UIKit.h>

@interface BIDDatePickerViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
- (IBAction)buttonPressed; @end

BIDDatePickerViewController.m

#import "BIDDatePickerViewController.h"

@implementation BIDDatePickerViewController

- (IBAction)buttonPressed
{
NSDate *selected = [self.datePicker date]; // 返回datapicker的时间
NSString *message = [[NSString alloc] initWithFormat:
@"The date and time you selected is: %@", selected]; // 弹出UIAlertView
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Date and Time Selected"
message:message
delegate:nil
cancelButtonTitle:@"Yes, I did."
otherButtonTitles:nil];
[alert show];
} - (void)viewDidLoad
{
[super viewDidLoad];
NSDate *now = [NSDate date]; // 获取当前的时间
[self.datePicker setDate:now animated:NO]; // 在视图加载完后, 让datapicker显示当前的时间
} @end

=============================================================

BIDSingleComponentPickerViewController.h   第二个Tab所有对应的视图控制器:

#import <UIKit/UIKit.h>

@interface BIDSingleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>  // datapicker的所需要委托还有数据源
@property (strong, nonatomic) IBOutlet UIPickerView *singlePicker; @property (strong, nonatomic) NSArray *characterNames;  // datapicker的数据
- (IBAction)buttonPressed;
@end

BIDSingleComponentPickerViewController.m

#import "BIDSingleComponentPickerViewController.h"

@implementation BIDSingleComponentPickerViewController

- (IBAction)buttonPressed
{
// 获取picker选择的哪一行, 0是表示第一列
NSInteger row = [self.singlePicker selectedRowInComponent:];
NSString *selected = self.characterNames[row]; // 根据行数得到Array的对应索引的内容
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];
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.characterNames = @[@"Luke", @"Leia", @"Han", @"Chewbacca",
@"Artoo", @"Threepio", @"Lando"]; // 为Array赋数值
} #pragma mark -
#pragma mark Picker Data Source Methods
// UIPickerViewDataSource必须实现的方法, 指定picker有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
} // 指定列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [self.characterNames count];
} #pragma mark Picker Delegate Methods
// UIPickerViewDelegate必须的方法, 指定picker每一行显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return self.characterNames[row];
} @end

================================================================

BIDDoubleComponentPickerViewController.h 第三个tab所对应的视图控制器:

#import <UIKit/UIKit.h>

#define kFillingComponent 0
#define kBreadComponent 1 @interface BIDDoubleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> @property (strong, nonatomic) IBOutlet UIPickerView *doublePicker;
@property (strong, nonatomic) NSArray *fillingTypes;
@property (strong, nonatomic) NSArray *breadTypes; -(IBAction)buttonPressed; @end

BIDDoubleComponentPickerViewController.m

#import "BIDDoubleComponentPickerViewController.h"

@implementation BIDDoubleComponentPickerViewController

-(IBAction)buttonPressed
{
// kFillingComponent与kBreadComponent是定义的宏, 用来标记是第几列的
NSInteger fillingRow = [self.doublePicker selectedRowInComponent:
kFillingComponent]; // 返回第kFillingComponent列被选中的行
NSInteger breadRow = [self.doublePicker selectedRowInComponent:
kBreadComponent]; // 返回第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];
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.fillingTypes = @[@"Ham", @"Turkey", @"Peanut Butter",
@"Tuna Salad", @"Chicken Salad", @"Roast Beef", @"Vegemite"];
self.breadTypes = @[@"White", @"Whole Wheat", @"Rye",
@"Sourdough", @"Seven Grain"];
} #pragma mark -
#pragma mark Picker Data Source Methods
// 表示这个picker有两列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
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

===============================================================

BIDDependentComponentPickerViewController.h 第四个tab所对应的视图控制器:

#import <UIKit/UIKit.h>

#define kStateComponent 0
#define kZipComponent 1 @interface BIDDependentComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> @property (strong, nonatomic) IBOutlet UIPickerView *dependentPicker;
@property (strong, nonatomic) NSDictionary *stateZips;
@property (strong, nonatomic) NSArray *states;
@property (strong, nonatomic) NSArray *zips; - (IBAction) buttonPressed; @end

BIDDependentComponentPickerViewController.m

#import "BIDDependentComponentPickerViewController.h"

@implementation BIDDependentComponentPickerViewController

- (IBAction)buttonPressed
{
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];
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSBundle *bundle = [NSBundle mainBundle]; // 获取NSBundle
NSURL *plistURL = [bundle URLForResource:@"statedictionary"
withExtension:@"plist"]; // 获取对应名字和后缀名的URL self.stateZips = [NSDictionary
dictionaryWithContentsOfURL:plistURL]; // 根据URL获取对应的NSDictionary NSArray *allStates = [self.stateZips allKeys]; // 得到NSDictionary的所有key
NSArray *sortedStates = [allStates sortedArrayUsingSelector:
@selector(compare:)]; // 为Array排序
self.states = sortedStates; // 赋值 NSString *selectedState = self.states[]; // 获取Array的第一个字符
self.zips = self.stateZips[selectedState]; // 因为是Dictionary, 所以根据Array的第一个字符获取对应的Array
} #pragma mark -
#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];
}
} // UIPickerViewDelegate的方法, 方法是点击后触发
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
// kStateComponent列
if (component == kStateComponent) {
NSString *selectedState = self.states[row]; // 根据行数对应Array的元素
self.zips = self.stateZips[selectedState]; // Dictionary根据选中的元素也就是key获取对应的Array
[self.dependentPicker reloadComponent:kZipComponent]; // picker重新加载指定列
[self.dependentPicker selectRow:
inComponent:kZipComponent
animated:YES]; // 选中指定列的某一行
}
} // 为不同的列设置不同的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView
widthForComponent:(NSInteger)component
{
if (component == kZipComponent) {
return ;
} else {
return ;
}
} @end

==================================================================

BIDCustomPickerViewController.h   第五个tab所对应的视图控制器:

#import <UIKit/UIKit.h>

@interface BIDCustomPickerViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> @property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) IBOutlet UILabel *winLabel;
@property (strong, nonatomic) NSArray *images;
@property (strong, nonatomic) IBOutlet UIButton *button; - (IBAction)spin; @end

BIDCustomPickerViewController.m

#import "BIDCustomPickerViewController.h"
#import <AudioToolbox/AudioToolbox.h> @implementation BIDCustomPickerViewController {
SystemSoundID winSoundID;
SystemSoundID crunchSoundID;
} -(void)showButton
{
self.button.hidden = NO;
} -(void)playWinSound
{
if (winSoundID == ) {
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"win" withExtension:@"wav"]; // 获取指定音频文件得URL
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &winSoundID); // 创建系统声音对象
}
AudioServicesPlaySystemSound(winSoundID); // 播放系统声音
self.winLabel.text = @"WINNING!";
[self performSelector:@selector(showButton)
withObject:nil
afterDelay:1.5]; // 执行方法
} - (IBAction)spin
{
BOOL win = NO;
int numInRow = ;
int lastVal = -;
for (int i = ; i < ; i++) {
int newValue = random() % [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:.];
} else {
[self performSelector:@selector(showButton)
withObject:nil
afterDelay:.];
}
self.button.hidden = YES;
self.winLabel.text = @""; if (win) {
self.winLabel.text = @"WIN!";
} else {
self.winLabel.text = @"";
}
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.images = @[[UIImage imageNamed:@"seven"],
[UIImage imageNamed:@"bar"], [UIImage imageNamed:@"crown"],
[UIImage imageNamed:@"cherry"], [UIImage imageNamed:@"lemon"],
[UIImage imageNamed:@"apple"]]; srandom(time(NULL)); // random()每次生成的随机数就是非固定的了
} #pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
} - (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [self.images count];
} #pragma mark Picker Delegate Methods
// 自定义的UIPickerView内容,给每列每行设置一个UIView
- (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;
} @end

ios成长之每日一遍(day 7)的更多相关文章

  1. ios成长之每日一遍(day 8)

    这几天都有一些任务要跟, 把ios的学习拉后, 看看要抓紧咯, 看看轮到的学习的是UITableView. BIDAppDelegate.h #import <UIKit/UIKit.h> ...

  2. ios成长之每日一遍(day 5)

    iOS 屏幕方向那点事儿http://zhenby.com/blog/2013/08/20/talk-ios-orientation/ 针对当前的屏幕方向进行对应的代码布局 BIDViewContro ...

  3. ios成长之每日一遍(day 3)

    今天要介绍一下更多的控键使用, 当然也会对上一篇说的控件做一个巩固, 所以这一篇涉及到的控键会包括 UIImage.UITextField.UIButton.UILabel.UISwitch.以及 U ...

  4. ios成长之每日一遍(day 1)

    Hello world开始. 这里不讨论如何创建项目导入项目.由于趁上班时间打酱油所以也不谈细节, 只谈具体项目的实现与关键流程的解析, 只供本人实际程况使用.不喜请移驾. 首先来谈谈 AppDele ...

  5. ios成长之每日一遍(day 6)

    toolBar上的View Switcher BIDAppDelegate.h #import <UIKit/UIKit.h> @class BIDSwitchViewController ...

  6. ios成长之每日一遍(day 4)

    今天, 主要讲四种常见的问题, 废话不多说了, 直接开始. 自动布局:这个我发现有一篇文章写得非常好, 直接表明出地http://www.cocoachina.com/applenews/devnew ...

  7. ios成长之每日一遍(day 2)

    接着下来简单说说Label(相当于android的textview)和button的使用, 由于都是与上篇的AppDelegate一致, 所以这一篇就说说ViewController与xib的使用呗. ...

  8. iOS:从头捋一遍VC的生命周期

    一.介绍 UIViewController是iOS开发中的核心控件,没有它那基本上任何功能都无法实现,虽然系统已经做了所有控件的生命维护,但是,了解它的生命周期是如何管理还是非常有必要的.网上有很多教 ...

  9. IOS成长之路-用NSXMLParser实现XML解析

    再次对xml进行解析,又有了些理解,如果有不对的地方,请给小弟指出,谢谢! <?xml version="1.0" encoding="UTF-8"?&g ...

随机推荐

  1. 关于idtcpserver的使用

    原文:http://blog.csdn.net/hnxxcxg/article/details/2798019 用idTCPServer,客户端接上来时,如何取得客户端的IP? IP:=AThread ...

  2. kafka一直rebalance故障,重复消费

    今天我司线上kafka消息代理出现错误日志,异常rebalance,而且平均间隔2到3分钟就会rebalance一次,分析日志发现比较严重.错误日志如下 08-09 11:01:11 131 pool ...

  3. Windows下CRF++进行中文人名识别的初次尝试

    语料来自1998年1月份人民日报语料 1 语料处理 1.1 原始语料数据格式 语料中,句子已经被分词好,并且在人名后以“/”标注了“nr”表示是人名,其他非人名的分词没有进行标注 1.2 CRF++要 ...

  4. javascript模拟flash头像裁切上传

    是的,jq已经有类似的插件了,或者干脆用flash算了,为什么我还要自己写?因为造(wo)轮(bu)子(hui)也(flash)是一个学习的过程,轮子不会造,将来怎么造飞机?先来一张最终效果图: 一. ...

  5. PMK数据生成工具airolib-ng

    PMK数据生成工具airolib-ng   PMK(Pairwise Master Key)是根据ESSID和无线密钥生成的哈希值,用于WPA/WPA2身份认证.在WPA/WPA2暴力破解中,需要大量 ...

  6. android touch事件分发流程

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 三个方法:分发触摸事件dispatchTouchEvent.在触摸事件的时候onTouc ...

  7. Bzoj2694/Bzoj4659:莫比乌斯反演

    Bzoj2694/Bzoj4659:莫比乌斯反演 先上题面:首先看到这数据范围显然是反演了,然而第三个限制条件十分不可做.于是我们暂且无视他,大不了补集转化算完再减是吧. 于是我们有:这里我们定义:于 ...

  8. ZOJ.3551.Bloodsucker(期望DP)

    题目链接 \(Description\) 有1个吸血鬼和n-1个人,每天有且只会有两个人/吸血鬼相遇,如果是人与吸血鬼相遇,那个人会有p的概率变成吸血鬼:否则什么也不发生.求n个都变成吸血鬼的期望天数 ...

  9. C++ 队列(queue)堆栈(stack)实现基础

    Queue 在C++中只要#include<queue>即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序) 1. push 2. pop 3. size 4. ...

  10. BZOJ3355 : [Usaco2004 Jan]有序奶牛

    对于一条边x->y,若去掉之后x不能到达y,那么它是必需的. 首先拓扑排序求出拓扑序,然后按照终点拓扑序为第一关键字,起点拓扑序为第二关键字从小到大加边. 对于每个点,维护一个bitset,表示 ...