I have an app with tabs and navigation controllers.
Everything works great except a UIActionSheet. I even have a UIAlertView taht shows fine in the same controller, but the action sheet doesn't show. The screen goes dark, like it's showing, but no view.
Here's the relevant code:
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Erase the file?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Clear List"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
//[actionSheet showInView:self.view];
//MyAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
//[actionSheet showInView:delegate.tabBarController.view];
[actionSheet release];
The commented out code was the different ways of showing it that I've tried.
Any thoughts as to why this isn't working?
[actionSheet showInView:self.view.window];
Don't show it from the current keyWindow - show it from the actual tab bar with something like
[actionSheet showFromTabBar:delegate.tabBarController.tabBar];
Try, this works perfectly:
UITextField *tempTextField;
UIActionSheet *myActionSheet;
- (IBAction) myDatePickerAction:(id)sender {
tempTextField = (UITextField *)sender;
[(UITextField *)sender resignFirstResponder];
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[myActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIDatePicker *theDatePicker = [[UIDatePicker alloc] init];
theDatePicker.datePickerMode = UIDatePickerModeDate;
CGRect datePickRect = theDatePicker.frame;
datePickRect.size.height = 120;
theDatePicker.frame = datePickRect;
[theDatePicker addTarget:self action:#selector(setDateValue:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:theDatePicker];
[theDatePicker release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
CGRect closeButtonRect = closeButton.frame;
closeButtonRect.size.width = 50;
closeButtonRect.size.height = 30;
closeButton.frame = closeButtonRect;
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showFromTabBar:appDelegate.tabBarController.tabBar];
CGRect actionSheetRect = myActionSheet.bounds;
actionSheetRect.size.height = 300;
myActionSheet.bounds = actionSheetRect;
}
- (IBAction)setDateValue:(id)sender {
NSDateFormatter *myDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSLog(#"Formatted date1:%#",[myDateFormatter stringFromDate:[(UIDatePicker *)sender date]]);
tempTextField.text = [myDateFormatter stringFromDate:[(UIDatePicker *)sender date]];
tempTextField = nil;
}
- (IBAction)dismissActionSheet:(id)sender {
[myActionSheet dismissWithClickedButtonIndex:0 animated:YES];
myActionSheet = nil;
//[myActionSheet release];
//Do not release here. Just set to nil and release it in dealloc.
}
Related
I have a textfield. once I tap on that I want a UIPickerView to pop out and select an item from the UIPickerView and after once I am done with the selection I want to confirm the selection by tapping on an UIActionSheet which has confirm button on it.So my requirement is to have a UIActionSheet and beneath that a picker view. I am just a beginner in iOS development and I am still not familiar with the technical terminologies. So please bear with my informal way of asking questions.
Here is my block of code:
creating UIPickerView and UIActionSheet
UIActionSheet *confirmRoomSelectionAS =[[UIActionSheet alloc]initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Done",nil];
UIPickerView *roomTypePicker=[[UIPickerView alloc]init];
[confirmRoomSelectionAS addSubview:roomTypePicker];
[confirmRoomSelectionAS showInView:self.view];
[confirmRoomSelectionAS setBounds:CGRectMake(0, 0, 320, 600)];
[roomTypePicker setFrame:CGRectMake(0, 170, 320, 216)];
[roomTypePicker setDataSource:self];
[roomTypePicker setDelegate: self];
roomTypePicker.showsSelectionIndicator = YES;
Try like this,
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet showInView:[self.view window]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 390)];
[actionSheet addSubview:[self createToolbar:#"Test"]];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 162);
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
- (UIView *)createToolbar:(NSString *)titleString {
UIToolbar *inputAccessoryView = [[UIToolbar alloc] init];
inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[inputAccessoryView sizeToFit];
CGRect frame = inputAccessoryView.frame;
frame.size.height = 44.0f;
inputAccessoryView.frame = frame;
UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancelBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel:)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 , 11.0f, 100, 21.0f)];
[titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:14]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:titleString];
[titleLabel setTextAlignment:UITextAlignmentCenter];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
NSMutableArray *array = [NSMutableArray arrayWithObjects:cancelBtn,flexibleSpaceLeft,title,flexibleSpaceLeft, doneBtn, nil];
[inputAccessoryView setItems:array];
[doneBtn release];
[title release];
[titleLabel release];
[flexibleSpaceLeft release];
[cancelBtn release];
return [inputAccessoryView autorelease];
}
- (void)done:(id)sender {
}
- (void)cancel:(id)sender {
}
It will be like,
3 things I thought you should modify:
1) add your cancel button manually, to later better check the cancel button pressing event
UIActionSheet * confirmRoomSelectionAS = [[UIActionSheet alloc]
initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[confirmRoomSelectionAS addButtonWithTitle:#"Done"];
confirmRoomSelectionAS.cancelButtonIndex = [confirmRoomSelectionAS addButtonWithTitle: #"Cancel"];
[confirmRoomSelectionAS showInView:self.view];
2) implement your actionsheet delegate method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) {
// cancel button pressed;
return;
}
}
3) You should declare *UIPickerView picker in your .h to later check of its selected value in the previous delegate method (also including the Done check) by adding sthing like this:
NSInteger row;
row = [picker selectedRowInComponent:0];
I want to
show a picker view (sliding up)
deactivate the background while it's visible
show (UIActionSheet) buttons at the bottom (not at the top)
It seems to me an easy solution at first since you can find code for adding a picker view to an action sheet everywhere in the web but all solutions position the buttons at top and I need to put the buttons at the bottom of the action sheet (alignment?).
Is this possible? I guess it is if I look at: Example
Regards
Jeven
EDITED (my solution based on coded dads solution)
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 0, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
UISegmentedControl *backButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Back", nil]] ;
[backButton addTarget:self action:#selector(someFunction:) forControlEvents:UIControlEventValueChanged];
backButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
backButton.segmentedControlStyle = UISegmentedControlStyleBar;
[backButton addTarget:self action:#selector(btnActionCancelClicked:) forControlEvents:UIControlEventAllEvents];
backButton.frame = CGRectMake(20.0, 220.0, 280.0, 40.0);
UISegmentedControl *acceptButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Accept", nil]] ;
[acceptButton addTarget:self action:#selector(anotherFunction:) forControlEvents:UIControlEventValueChanged];
acceptButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
acceptButton.segmentedControlStyle = UISegmentedControlStyleBar;
acceptButton.frame = CGRectMake(20.0, 280.0, 280.0, 40.0);
[actionSheet addSubview:pickerView];
[actionSheet addSubview:backButton];
[actionSheet addSubview:acceptButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setFrame:CGRectMake(0,150,320, 400)];
... then just apply iPhone 5/iphone 4 dependent view size constraints
EDITED 2:
Just another hint! Originally I wanted to use the standard Actionsheet, but didn't want to place the pickerview at the bottom. I didn't find a way how to move the buttons. Obviously it s really simple: Add UIView as subView on UIActionSheet Good to know ;)
You have 2 possibilites:
Option1: the big tweak.
ActionSheet shows the buttons with the order defined during init. So place some dummy buttons at the top and Cancel will be the only visible, all other buttons will be hidden by the pickerview. Code (warning-tweak at otherButtonTitles):
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"1", #"2", #"3", #"4", nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;
Option2: the selfmade
menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;
UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(80,230, 170, 73);
[startbtn setBackgroundImage:[UIImage imageNamed:#"yourbutton.png"] forState:UIControlStateNormal];
[startbtn addTarget:self action:#selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self.view];
[menu setFrame:CGRectMake(0,150,320, 350)];
Check that for option2 the button should be on the actionsheet unless the clicks will not be dispatched to the selector method.
I am trying to display an activity indicator in full screen so the user cannot press any button in the screen till the activity indicator is turned off as the alert view process. I have called the [activityView startAnimating] but I can push the buttons in the back. Is there way to prevent that?
Thanks from now.
You can use MBProgressHUD for such loading indictators. It's a great MIT-licensed class that extends well if you want to customize it further.
you can try this
UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:#"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(150, 100);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
and add this line where you want remove your alert
[alert dismissWithClickedButtonIndex:0 animated:YES];
I'll suggest a best way will be displaying an alertView with activity indicator on the screen.
You can use the following code for this:
declare property for UIAlertView like:
#property (nonatomic, strong) UIAlertView *sendAlert;
self.sendAlert = [[UIAlertView alloc] initWithTitle:#"Loading" message:#"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
act setFrame:CGRectMake(115, 60, 50, 50)];
[act startAnimating];
[sendAlert addSubview:act];
act = nil;
[sendAlert show];
When you want to remove alert you can use:
[sendAlert dismissWithClickedButtonIndex:0 animated:YES];
sendAlert = nil;
Another alternative, you can add the activity indicator to your view itself and set the userInteraction of backbutton to false. When you finish the task set to True. But It won't be a nice way.
hey for your this requirement use the bellow code which you can access in your every view use..
add this bellow code and object in AppDelegate.h file like bellow..
UIView *activityView;
UIView *loadingView;
UILabel *lblLoad;
and paste this bellow code in AppDelegate.m file
#pragma mark - Loading View
-(void) showLoadingView {
//NSLog(#"show loading view called");
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 60.0, 320.0, 420.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor darkGrayColor];
loadingView.alpha = 0.5;
UIView *subloadview=[[UIView alloc] initWithFrame:CGRectMake(84.0, 190.0,150.0 ,50.0)];
subloadview.backgroundColor=[UIColor blackColor];
subloadview.opaque=NO;
subloadview.alpha=0.8;
subloadview.layer.masksToBounds = YES;
subloadview.layer.cornerRadius = 6.0;
lblLoad=[[UILabel alloc]initWithFrame:CGRectMake(50.0, 7.0,80.0, 33.0)];
lblLoad.text=#"LoadingView";
lblLoad.backgroundColor=[UIColor clearColor];
lblLoad.textColor=[UIColor whiteColor];
[subloadview addSubview:lblLoad];
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10.0, 11.0, 25.0, 25.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[subloadview addSubview:spinningWheel];
[loadingView addSubview:subloadview];
[spinningWheel release];
}
[self.window addSubview:loadingView];
//[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
}
-(void) hideLoadingView {
if (loadingView) {
[loadingView removeFromSuperview];
[loadingView release];
loadingView = nil;
}
}
and call this method when you want in any class , just like bellow..
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate showLoadingView];
Add a UIView above your current view when activity indicator starts :
UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
[self.navigationController.view addSubview:overlayView];
I m newbie to objective-C
I want to display the UIDatePicker in a popup window after the button click ..
I have a button and when I click the button my popup should appear with DatePicker and later after chosing the date the popup should close and set the selected date in a textbox.
How can I do this ?
To create a datepicker I wrote this code ..
UIDatePicker *datePicker=[[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.view addSubview:datePicker];
But I do not know how to display it in a popup window on a button click ..?
Declaration in your .h file
UIActionSheet *aac;
UIDatePicker *theDatePicker;
Implementation in .m file
// Add the code after your comment
-(void)DatePickerDoneClick:(id)sender {
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"MM/dd/yyyy";
NSArray *temp=[[NSString stringWithFormat:#"%#",[df stringFromDate:theDatePicker.date]] componentsSeparatedByString:#""];
[dateString1 release];
dateString1=nil;
dateString1 = [[NSString alloc]initWithString:[temp objectAtIndex:0]];
UITextField* BirthDayTxtLBl.text = [NSString stringWithFormat:#" %#",dateString1];
NSString *theTime = [NSString stringWithFormat:#"%#",BirthDayTxtLBl.text];
NSLog(#"%#",theTime);
[aac dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)DatePickercancelClick:(id)sender{
[aac dismissWithClickedButtonIndex:0 animated:YES];
}
-(IBAction)AddTheTimePicker:(id)sendar {
aac = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?#"\n\n":nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
theDatePicker.datePickerMode=UIDatePickerModeDateAndTime;
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick:)];
//doneBtn.tag = tagID;
[barItems addObject:doneBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UILabel *toolBarItemlabel;
if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
toolBarItemlabel= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
else
toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = [NSString stringWithFormat:#"Select Start Time"];
UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
[toolBarItemlabel release];
[barItems addObject:buttonLabel];
[buttonLabel release];
[barItems addObject:flexSpace];
UIBarButtonItem *SelectBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(DatePickercancelClick:)];
[barItems addObject:SelectBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[aac addSubview:pickerDateToolbar];
[aac addSubview:theDatePicker];
CGRect myImageRect = CGRectMake(0.0f, 300.0f, 320.0f, 175.0f);;
[aac showFromRect:myImageRect inView:self.view animated:YES ];
[UIView beginAnimations:nil context:nil];
if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
[aac setBounds:CGRectMake(0,0,320, 464)];
else
[aac setBounds:CGRectMake(0,0,480, 400)];
[UIView commitAnimations];
}
// Add this if you wish to add support Orientation support for picker
- (BOOL) isViewPortrait {
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
return (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Try this, this can help you
-(IBAction)DatePickerAction:(id)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
[pickerView release];
[menu release];
}
you can set the .inputView property of the text field to a datepicker..
so myTextField.inputView = datePicker;
this will replace the keyboard input view for the datePicker.
Gets a little more indepth, you will need to add a target for when the value of the date picker changes (so it updates the text field)
let me know if this is how you want to go about it.
If you still want the popup window im not sure if you can subclass UIAlertView for this..
or you can add the datePicker to a UIView instance and add that onto the screen with some fancy animation, etc....
Plenty options and im happy to help you with them...
Write in your Button Click method
self.DatePicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
self.DatePicker.datePickerMode = UIDatePickerModeDate;
[self.DatePicker addTarget:self
action:#selector(SetDatePickerTime:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.DatePicker];
- (void)SetDatePickerTime:(id)sender
{
[self.DatePicker removeFromSuperview];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"dd:MM:yyyy"];
NSLog(#"%#",[outputFormatter stringFromDate:self.DatePicker.date]);
}
Best method will be to create animation like it..ive tried it using extension to uiview..
try the following
in the .m file where you want to show popup just above the normal implementation
#interface UIView (AlertAnimation)
- (void)doPopInAnimation;
#end
const NSTimeInterval kAnimationDuration = 0.3f;
#implementation UIView (AlertAnimation)
- (void) doPopInAnimation {
CALayer *viewLayer = self.layer;
CAKeyframeAnimation *popInAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
popInAnimation.duration = 0.3f;
popInAnimation.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:1.2], [NSNumber numberWithFloat:0.9], [NSNumber numberWithFloat:1], nil];
popInAnimation.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:0.8], [NSNumber numberWithFloat:1.0], nil];
[viewLayer addAnimation:popInAnimation forKey:#"transform.scale"];
}
#end
further put this magical line on the event you want popup
[yourPickerView doPopInAnimation];
Refer this code
startDatePicker = [[UIDatePicker alloc] init];
startDatePicker.datePickerMode = UIDatePickerModeDate;
[startDatePicker addTarget:self action:#selector(startLabelChange:)forControlEvents:UIControlEventValueChanged];
CGSize pickerSize = [startDatePicker sizeThatFits:CGSizeZero];
startDatePicker.frame = CGRectMake(-40, -20, pickerSize.width, pickerSize.height);
startDatePicker.transform = CGAffineTransformMakeScale(0.750f, 0.750f);
UIViewController *pickerController = [[UIViewController alloc] init];
[pickerController.view addSubview:startDatePicker];
[startDatePicker release];
[pickerController setContentSizeForViewInPopover:CGSizeMake(240, 180)];
UIPopoverController *pickerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController];
[pickerPopover presentPopoverFromRect:startDatelabel.frame
inView:rightSideView
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
pickerPopover.delegate = self;
self.popover = pickerPopover;
[pickerController release];
[pickerPopover release];
I have a UIPickerView. I want a done button on top of the pickerview and i want to dismiss the picker view on click of the done button. Can u please help me on this?
add action sheet to your view and then add tool bar with done button
at top of action sheet and below add your your picker to action sheet
in done button click write below method to dismiss action sheet
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
[cancelBtn release];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[barItems release];
[pickerToolbar release];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 216);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
[picker release];
-(void)done_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
You can use the toolbar for the same purpose. Take a look at this link. Here, the same solution is taken into consideration. Hope that may help you. Thanks.
every body took the action sheet and then added the bar button ,but simply added uibutton from the view and take method remove from the super view. show the bellow example , i am tried this code successful. i think this is very useful code with out using action sheet
-(void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(#"the marray data is %#",marray); // [self showpicker1]; if (marray.count == 0) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#"NO DATA FOUND" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil]; [alert show]; }
[self showpicker1];
}
-(void)showpicker1 { //view1=[[UIView alloc] initWithFrame:CGRectMake(55, 59, 171, 80)]; //[self.view addSubview:view1]; pv = [[UIPickerView alloc] initWithFrame:CGRectMake(55,79,171,0)]; pv.delegate = self; pv.dataSource = self; pv.showsSelectionIndicator = YES; [self.view addSubview:pv]; done=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [done setFrame:CGRectMake(55, 59, 171, 20)]; [done setTitle:#"DONE" forState:UIControlStateNormal]; [done addTarget:self action:#selector(dismiss) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:done]; } -(void)dismiss { [pv removeFromSuperview]; [done removeFromSuperview]; }