//
// ViewController.m
// TestApplication
//
// Created by Macbook on 08/02/14.
// Copyright (c) 2014 Macbook. All rights reserved.
//
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize isSelectDate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -OpenActionshhetWithDatePicker Event
-(void)openactionsheetWithDatePicker
{
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose Date" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"",@"",@"",nil];
actionSheet.delegate = self;
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];
actionSheet.tag = 3;
[actionSheet showInView:self.view];
UIButton *btncancelpicker = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
[btncancelpicker setFrame:CGRectMake(5, 5, 70,30)];
[btncancelpicker setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
[btncancelpicker addTarget:self action:@selector(datePickerCancelClicked) forControlEvents:UIControlEventTouchUpInside];
[actionSheet addSubview:btncancelpicker];
UIButton *btndonepicker = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ||[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
[btndonepicker setFrame:CGRectMake(405, 5, 70,30)];
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ||[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
[btndonepicker setFrame:CGRectMake(240, 5, 70,30)];
[btndonepicker setBackgroundImage:[UIImage imageNamed:@"done.png"] forState:UIControlStateNormal];
[btndonepicker addTarget:self action:@selector(datePickerDoneClicked) forControlEvents:UIControlEventTouchUpInside];
[actionSheet addSubview:btndonepicker];
datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 45, 320, 190)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.date = [NSDate date];
[actionSheet addSubview:datePicker];
}
-(void)datePickerDoneClicked
{
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"dd/MM/yyyy"];
NSString *tmp=[outputFormatter stringFromDate:datePicker.date];
if (isSelectDate==TRUE) {
[btnfrom setTitle:tmp forState:UIControlStateNormal];
}
else{
[btnTo setTitle:tmp forState:UIControlStateNormal];
}
[outputFormatter release];
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)datePickerCancelClicked
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
#pragma mark -Button Click Event
-(IBAction)btnfromPress:(id)sender
{
isSelectDate=TRUE;
[self openactionsheetWithDatePicker];
}
-(IBAction)btnToPress:(id)sender
{
isSelectDate =FALSE;
[self openactionsheetWithDatePicker];
}
-(IBAction)btnRoomPress:(id)sender
{
[self openRoomPicker];
}
-(void)openRoomPicker
{
arrRooms = [[NSMutableArray alloc] init];
[arrRooms addObject:@"1 Room"];
[arrRooms addObject:@"2 Room"];
roomPickerview.hidden=FALSE;
roomPickerview.delegate=self;
roomPickerview.dataSource=self;
[roomPickerview selectRow:0 inComponent:0 animated:NO];
NSString *strRoom= [arrRooms objectAtIndex:[roomPickerview selectedRowInComponent:0]];
[btnRoom setTitle:strRoom forState:UIControlStateNormal];
}
#pragma mark -PickerView Method
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pv;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pv numberOfRowsInComponent:(NSInteger)component;
{
return [arrRooms count];
}
- (void)pickerView:(UIPickerView *)_pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch ([roomPickerview selectedRowInComponent:0])
{
case 0:
NSLog(@"select row is 1");
[btnRoom setTitle:[arrRooms objectAtIndex:row] forState:UIControlStateNormal];
break;
case 1:
NSLog(@"select row is 2");
[btnRoom setTitle:[arrRooms objectAtIndex:row] forState:UIControlStateNormal];
break;
default:
break;
}
}
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 300.0f, 60.0f)]; //x and width are mutually correlated
// label.textAlignment = NSTextAlignmentLeft;
//
// label.text = [arrRooms objectAtIndex:row];
//
// return label;
// }
//
- (NSString *)pickerView:(UIPickerView *)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (arrRooms!=nil)
{
return [arrRooms objectAtIndex:row];
}
return @"";
}
@end
//
// ViewController.h
// TestApplication
//
// Created by Macbook on 08/02/14.
// Copyright (c) 2014 Macbook. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIActionSheetDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{
IBOutlet UIButton *btnfrom;
IBOutlet UIButton *btnTo;
UIActionSheet *actionSheet;
UIDatePicker *datePicker;
BOOL isSelectDate;
IBOutlet UIButton *btnRoom;
IBOutlet UIPickerView *roomPickerview;
NSMutableArray *arrRooms;
}
@property(nonatomic,readwrite) BOOL isSelectDate;
-(IBAction)btnfromPress:(id)sender;
-(IBAction)btnToPress:(id)sender;
-(IBAction)btnRoomPress:(id)sender;
-(void)openactionsheetWithDatePicker;
-(void)openRoomPicker;
@end
來源
2014-02-08 04:42:50
xyz
已經有人問過這個問題。看到這裏:http://stackoverflow.com/questions/1849873/how-do-i-make-a-uipickerview-in-a-uiactionsheet –