ios之选取器与标签栏

选取器并不是从interface builder 中挑选一个选取器并放在内容视图上,然后进行配置就行。但是日期选取器是个例外。此外还需为选取器提供选取器委托和选取器数据源(是控制器的一部分),但日期选取器也是个例外。


1、将Tab Bar Controller对象库拖到MainWindow.xib上。

2、按住control并将 ...AppDelegate图标拖到TabBarController图表上,然后选择UITabBarController的实例。

3、将Tab  Bar  Item(标签项添加到)标签栏上。

4、在nib主窗口中,标签栏下的试图控制器,然后调出属性检查器(Tools->Attributes Inspector)。每个标签的视图控制器与欠恰当的nib就是在这里关联的(NIB Name)。

5、此时按下command+4 调出视图控制器的身份检查器。修改Class项。

6、在nib的主窗口中,单击第一个标签栏项,按command+1返回到检查属性。


一、实现日期选取器

.h文件

#import <UIKit/UIKit.h>


@interface DatePickerViewController : UIViewController {
IBOutlet UIDatePicker *datePicker;
}
@property (nonatomic, retain) UIDatePicker *datePicker;
-(IBAction)buttonPressed:(id)sender;
@end


#import "DatePickerViewController.h"




@implementation DatePickerViewController
@synthesize datePicker;


-(IBAction)buttonPressed:(id)sender
{
NSLog(@"Button Pressed");
NSDate *selected = [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:@"Yes, I did." otherButtonTitles:nil];
[alert show];
[alert release];
[message release];

}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}

- (void)viewDidLoad {
NSDate *now = [NSDate date];
[datePicker setDate:now animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}

- (void)dealloc {
[datePicker release];
[super dealloc];
}

@end


一、实现单个组件选取器。

.h文件

#import <UIKit/UIKit.h>




@interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *singlePicker;
NSArray *pickerData;
}
@property (nonatomic, retain) UIPickerView *singlePicker;
@property (nonatomic, retain) NSArray *pickerData;
- (IBAction)buttonPressed:(id)sender;
@end


.m文件

#import "SingleComponentPickerViewController.h"

@implementation SingleComponentPickerViewController
@synthesize singlePicker;
@synthesize pickerData;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (IBAction)buttonPressed:(id)sender
{
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex: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];
[alert release];
[title release];
}
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia", @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
self.pickerData = array;
[array release];
 
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}

- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView //决定组建的个数
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
@end


构建单个组件选取器视图。

1、打开标签栏中第二个标签的内容视图。单击View图标,并按option+command+3调出属性检查器,以便在Simulated Interface Elements部分将Buttom Bar设置为Tab Bar。

2、接下来从库中找到Picker View,将其添加到nib的view中。

3、按住control键并将File‘s Owner拖到选取器视图,然后选择singlePicker输出口。

4、单击选取器,按下command+2打开连接器,分别将DataSource和Delegate拖到File's Owner。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值