views:

8611

answers:

5

Heading

My application requires following things to be added in action sheet.

=>A UIToolbar
=>A Button on UIToolbar
=>A UIPicker Control

i have given below an image to understand my requirements.

alt text

Will you plz explain, How this can be implemented?

+6  A: 

Yep ! I finally Find it.

implement following code on your button click event, to pop up action sheet as given in the image of question.

UIActionSheet *aac = [[UIActionSheet alloc] initWithTitle:@"How many?"
            delegate:self
         cancelButtonTitle:nil
          destructiveButtonTitle:nil
         otherButtonTitles:nil];

UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
if(IsDateSelected==YES)
{
 theDatePicker.datePickerMode = UIDatePickerModeDate;
 theDatePicker.maximumDate=[NSDate date];
}else {
 theDatePicker.datePickerMode = UIDatePickerModeTime;
}

self.dtpicker = theDatePicker;
[theDatePicker release];
[dtpicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];

pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];

[pickerDateToolbar setItems:barItems animated:YES];

[aac addSubview:pickerDateToolbar];
[aac addSubview:dtpicker];
[aac showInView:self.view];
[aac setBounds:CGRectMake(0,0,320, 464)];
sugar
hi sagar, thanks for the code, DatePickerDoneClick button event, how to declare itthanks
Apache
sagar, how to dismiss the pop up action sheet, and add selected value into the text fieldthanks
Apache
sugar
thanks for reply sagar, i add [aac dismissWithClickedButtonIndex];but i'm getting warning: 'UIActionSheet' may not respond to '-dismissWithClickedButtonIndex'then i create new method for selector 'DatePickerDoneClick'as below- (void) DatePickerDoneClick{ NSLog(@"Done clicked"); ratings.text = pickerView objectAtIndex:row]}what shall i do, so when i click the done button UIActionSheet(aac) dismiss and textfield(ratings.text) be filled with selected value from pickerthanks sagar
Apache
hi sagar, i able to update the text all, just can't dismiss the action sheet, even when click done button can trig, what shall i add into - (void) DatePickerDoneClick{ NSLog(@"Done clicked"); NSInteger row = [pickerView selectedRowInComponent:0]; rat.text = [pickerData objectAtIndex:row];}so action sheet dismissthanks
Apache
hi sagar, thanks, i'm able find to dismiss the action sheet,thanks
Apache
Hi sagar, good work, thanks a lot...
shinto Joseph
@shitno Joseph - why not an up vote?
sugar
@selector(DatePickerDoneClick) should be @selector(DatePickerDoneClick:)
sza
A: 

sagar i appreciate you , well because you have asked, you yourself have answered......!!!!!!!!!!!!!!

Mikhail Naimy
If he really has answered your question, click the green check mark to the left of his answer. You have not accepted a single answer yet, which will make people less likely to answer your questions in the future.
Brad Larson
+5  A: 

One more solution:

  • no toolbar but a segmented control (eyecandy)

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];

[actionSheet showInView:[UIApplication mainWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
marcio
+2  A: 

Marcio's excellent solution to this question was of great help to me in adding subviews of any kind to a UIActionSheet.

For reasons that are not (yet) entirely clear to me, the bounds of the UIActionSheet can only be set after it has been displayed; both sagar's and marcio's solutions successfully address this with a setBounds:CGRectMake(...) message being sent to the actionsheet after it is shown.

However, setting the UIActionSheet bounds after the sheet has been displayed creates a jumpy transition when the ActionSheet appeaars, where it "pops" into view, and then only scrolls in over the final 40 pixels or so.

When sizing a UIPickerView after adding subviews, I recommend wrapping the setBounds message sent to the actionSheet inside an animation block. This will make the entrance of the actionSheet appear smoother.

UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];


// add one or more subviews to the UIActionSheet
// this could be a UIPickerView, or UISegmentedControl buttons, or any other 
// UIView.  Here, let's just assume it's already set up and is called 
// (UIView *)mySubView
[actionSheet addSubview:myView];

// show the actionSheet
[actionSheet showInView:[UIApplication mainWindow]];


// Size the actionSheet with smooth animation
    [UIView beginAnimations:nil context:nil];
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
    [UIView commitAnimations]; 
reed
+1  A: 

For those guys who are tying to find the DatePickerDoneClick function... here is the simple code to dismiss the Action Sheet. Obviously aac should be an ivar (the one which goes in your implmentation .h file)

  • (void)DatePickerDoneClick:(id)sender{ [aac dismissWithClickedButtonIndex:0 animated:YES]; }
Accilies