When click a button,I want to popup a view form bottom,like this!
Any help is welcome,thanks.
Use the presentModalViewController:animated: method. See the docs for details on its use.
If you want to reproduce what is seen in the screenshot you linked you can also put a picker in a UIActionSheet like so:
UIActionSheet datePickerSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(#"Select Date",#"Selecte Date")
delegate:self
cancelButtonTitle:NSLocalizedString(#"Done",#"Done")
destructiveButtonTitle:NSLocalizedString(#"Cancel",#"Cancel")
otherButtonTitles:nil];
// Add the picker
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,185,0,0)];
[datePicker addTarget:self action:#selector(dateDidChange) forControlEvents:UIControlEventValueChanged];
[datePickerSheet addSubview:datePicker];
[datePickerSheet showInView:self.view];
[datePickerSheet setBounds:CGRectMake(0,0,320, 700)];
[datePicker release];
[datePickerSheet release];
Related
Im trying to follow this post Display datepicker on tapping on textfield and when I get to the point where it says "In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach it to the view" i get lost. Im using storyboards and he's using xib's. Is that the problem? xib's aren't an option for me. So is there a way to drag a date picker out, but not attach to the view?
The difference with storyboard is that you would use a XIB file (also called NIB) for each view.
But you should be able to follow any instructions.
In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach
it to the view.
I believe he means not to connect to the ViewController like you would do with a button (IBAction for example)
Since you are showing when you tap the textfield then you have to do it programmatically.
You don't need to add it to your view really.
You can create a viewcontroller that has a date picker at the bottom and a close button at the top.
When tapping the textfield you can just present that viewcontroller modally.
Set delegate methods to the viewcontroller for:
closeButtonClicked: , and dateSelected: ,
and implemet these delegate merhod in the main controller.
In addition, you can "block" the screen by adding the datePicker's viewcontroller background color with certain alpha for a nice affect.
I found that to be the easiest way to do that. No need for a nib
May be it will helpful to you..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
dateActionSheet = [[UIActionSheet alloc]initWithTitle:#"Select Your Date of Birth" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil , nil];
dateActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged ];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[dateActionSheet addSubview:datePicker];
[dateActionSheet addSubview:self.datePickerView];
UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,320,40)];
tools.barStyle=UIBarStyleBlackOpaque;
[dateActionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *flexSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [[NSArray alloc]initWithObjects:flexSpace,flexSpace,doneButton,nil];
[tools setItems:array];
//picker title
UILabel *lblPickerTitle=[[UILabel alloc]initWithFrame:CGRectMake(60,8, 200, 25)];
lblPickerTitle.text=#"Select Date of Birth";
lblPickerTitle.backgroundColor=[UIColor clearColor];
lblPickerTitle.textColor=[UIColor whiteColor];
lblPickerTitle.textAlignment = NSTextAlignmentCenter;
lblPickerTitle.font=[UIFont boldSystemFontOfSize:15];
[tools addSubview:lblPickerTitle];
//cell.detailTextLabel.text = [self datePickerValueChangedd:datePicker];
[dateActionSheet showFromRect:CGRectMake(0,480, 320,215) inView:self.view animated:YES];
[dateActionSheet setBounds:CGRectMake(0,0, 320, 411)];
}
-(void)btnActinDoneClicked {
[dateActionSheet dismissWithClickedButtonIndex:1 animated:YES];
[dateActionSheet removeFromSuperview];
}
-(void)datePickerValueChangedd:(UIDatePicker*) datePicker
{
Youytextfield.text = (NSString *)datePicker.date;
}
Hey so I have a problem where I am inserting an actionsheet into a view that is inside of a scrollview in a different view controller. The actionsheet works just fine, the problem is that if i go down at all in the scrollview the actionsheet gets clipped off. I've tried several solutions with no luck. I think the problem is that the actionsheet is being inserted into the view that is placed inside the scrollview. Anyone have any idea how to launch the action sheet from the view controller that the scrollview is in instead? Here is how I am trying to do it right now:
When a button is touched it calls this method:
- (IBAction)callDP:(id)sender {
UIButton *selectedButton = (UIButton *)sender;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
datePickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
datePickerView.tag = 10;
[datePickerView addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:datePickerView];
[actionSheet setTitle:#"Start Date"];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
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];
datePickerView.datePickerMode = UIDatePickerModeDate;
//THIS IS THE PART THAT I THINK I AM HAVING THE PROBLEM WITH
// [actionSheet showFromToolbar:self.toolbarItems];
//[actionSheet showFromTabBar:self.parentViewController.tabBarController.tabBar];
[actionSheet showInView:self.parentViewController.view];
}
Here is where I insert the view into the scrollview. I have it set up so that I am using a view from a different uiviewcontroller class to control everything. The reason I do that is so that i can have the scrollable part, but be able to visually create everything that I need without having to do it programmatically....I apologize if that is kind of confusing. I can clarify if needs be...but here it is. The viewcontroller class that contains the view I want to put into the scroll view is called registration page. Its inside of registrationPage that it calls the actionSheet. Let me know what you think...
registrationPage = [[RegistrationPageToInsertViewController alloc]init];
viewToInsert = registrationPage.view;
[scrollView addSubview:viewToInsert];
[scrollView setContentSize:viewToInsert.frame.size];
// [scrollView sendSubviewToBack:imgView];
//scrollView.contentSize=CGSizeMake(320,416);
[self.view bringSubviewToFront:scrollView];
Here are a couple of screenshots to help you see what I'm talking about:
instead of this line
[actionSheet showInView:self.parentViewController.view];
try like this.
[actionSheet showInView:self.view];
Just do as i say.You have a nib or viewController. Whatever. Just add a scrollView as subView to what you have.Then you just add all your textfields, labels and all will be subview of scrollView.this is the correct way to do that.if you present action sheet,you have to show that in self.View only.
I am making an iphone app on one of the view I am using a date picker. I have an another date which I am fetching from the database. Now I want that the date picker enable from the selected date so that user couldn't get unable to select the same date again. Is there any way to enable the picker from selected date. If anyone know please help me.
-(void)findextenddate
{
NSLog(#"count:%#",adddatetime);
addatimeclass = adddatetime;
endnsdate = [addatimeclass objectAtIndex:[addatimeclass count]-1];
endnsdate1 = (NSDate*) endnsdate;
NSLog(#"formatdate:%#",endnsdate1);
}
-(IBAction)clicktoextend:(id)sender
{
actionsheet = [[UIActionSheet alloc]init];
actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select Date" delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select", nil];
[actionsheet showInView:[self.view superview]];
[actionsheet setFrame:CGRectMake(0, 117, 320, 383)];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
pickerView.minimumDate = endnsdate1 ;
pickerView.datePickerMode = UIDatePickerModeDate;
//Add picker to action sheet
[actionsheet addSubview:pickerView];
subviews = [actionsheet subviews];
[[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
[pickerView release];
[actionsheet release];
}
Thanks alot.
Yes, UIDatePicker has minimumDate property which serves exactly that purpose
USE
datePicker.minimumDate=YOUR date object
this function disable all dates before your date
If you meant that you need to set the date of the picker to the one you are fetching from the database, you can do so via the - (void)setDate:(NSDate *)date animated:(BOOL)animated method.
If you need to enable the picker from that date ONWARDS (no past dates), set the minimumDate property as is suggested by the earlier answers.
I've created a UIPickerView, which displays on button click. The question is how I fill in the rowItems
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Location"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Done", #"")]];
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:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
and How do I get value when "done" is clicked.
you've set the delegate of the alert, and the picker already. Now you have to implement them. You don't fill in data for the picker, the picker asks your delegate for the data.
You need the mandatory functions defined in these three protocols.
UIPickerViewDelegate
UIPickerViewDataSource
UIActionSheetDelegate
You have to implement -(IBAction)dismissActionSheet:(UIButton *)sender too.
and How do I get value when "done" is clicked.
I would store the value to a instance variable every time the picker changes, and once the done button is clicked I would assign it to my data model.
Edit: If you want to learn something I would get rid of the copy&paste solution and try to implement it on my own. But YMMV.
I want to make the font size smaller for the text in the buttons of my action sheet as my text is longer than the width.
How can I make the font size smaller?
I'm guessing as you can add things like picker views to action sheets that I may need to add my own buttons to the action sheet, if so how ?
I need an example.
EDIT: I've made a little progress, but the actual button isn't shown, but you can see it working with the change in the button text when you click on it.
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"OK",nil];
CGRect frame = CGRectMake(100.0, 80.0, 100.0, 40.0);
UIButton *pickerView = [[UIButton alloc] initWithFrame:frame];
[pickerView setTitle:#"FRED" forState:UIControlStateNormal];
[pickerView setTitle:#"Go Back" forState:UIControlStateNormal];
[pickerView setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
pickerView.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pickerView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//[removeButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchDown];
[pickerView setBackgroundColor:[UIColor blueColor]];
[menu addSubview:pickerView];
[menu showInView:self.view];
[pickerView release];
[menu release];
You may be able to do this directly, but note that actionsheet buttons are not standard UIButtons, but are their own special private control subclass. For this reason, when I need to customize an action sheet, I add my own custom subview to the UIActionSheet and then resize the sheet to match. The downside is that to match the look and feel I need a custom button image background. I adapted (but didn't check in a compiler) this example from Fitting a UIDatePicker into a UIActionSheet. Instead of the picker, just add your xib created UIView.
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[myUIActionSheet addSubview:pickerView];
[myUIActionSheet showInView:self.view];
[myUIActionSheet setBounds:CGRectMake(0,0,320, myUIActionSheet.bounds.size.height + pickerView.bounds.size.height)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = - pickerView.bounds.size.height; //you may need to change this offset
pickerView.bounds = pickerRect;
[pickerView release];
[myUIActionSheet release];
EDIT: To include a subview from a xib, add a UIView to the top level of the xib and wire it to a UIView property in your controller just as if it was a normal interface component. Within your controller, it will now be available.
You can also wire button actions normally instead of using the action sheet callback. You will need to close the action sheet after the press with something like [myUIActionSheet.dismissWithClickedButtonIndex:0 animated:YES]; I think that dismissing it as if it is modal will also work, IIRC.