How to add Date picker in Action Sheet? - iphone

- (IBAction) showCatPicker {
if (self.catList !=nil) {
self.catList=nil;
[catList release];
}
self.catList = [[NSMutableArray alloc] init];
self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
if(self.catPicker == nil) {
self.catPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.catPicker.showsSelectionIndicator = YES;
self.catPicker.dataSource = self;
self.catPicker.opaque = YES;
self.catPicker.multipleTouchEnabled = YES;
self.catPicker.userInteractionEnabled = YES;
self.catPicker.delegate = self;
}
[self.actionSheet addSubview:self.catPicker];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Select"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[closeButton addTarget:self action:#selector(dismissGenderPicker:) forControlEvents:UIControlEventValueChanged];
UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(10, 7.0f, 50.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[cancelButton addTarget:self action:#selector(cancelActionSheet) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:cancelButton];
[self.actionSheet addSubview:closeButton];
[cancelButton release];
[closeButton release];
[self.actionSheet showInView:self.view];
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[catPicker reloadComponent:0];
}

You should definitely NOT use UIActionSheet to hold an UIDatePicker because this functionality is deprecated!
From the Documentation:
Subclassing Notes
UIActionSheet is not designed to be subclassed, nor should you add
views to its hierarchy. If you need to present a sheet with more
customization than provided by the UIActionSheet API, you can create
your own and present it modally with
presentViewController:animated:completion:.
and
Important: UIActionSheet is deprecated in iOS 8. (Note that
UIActionSheetDelegate is also deprecated.) To create and manage action
sheets in iOS 8 and later, instead use UIAlertController with a
preferredStyle of UIAlertControllerStyleActionSheet.
What you could very easily do is to create an UIView to hold the UIDatePicker and animate the view as appropriate. You can even add an UIToolbar to it if you need to.
Here's an example:
Create two properties:
#property (strong, nonatomic) UIDatePicker *theDatePicker;
#property (strong, nonatomic) UIView *pickerView;
Create your picker, embed it into the UIView and show:
-(void) createDatePickerAndShow {
UIToolbar *controlToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, pickerView.bounds.size.width, 44)];
[controlToolbar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"Set" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateSet)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelDateSet)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[theDatePicker setFrame:CGRectMake(0, controlToolbar.frame.size.height - 15, theDatePicker.frame.size.width, theDatePicker.frame.size.height)];
if (!pickerView) {
pickerView = [[UIView alloc] initWithFrame:theDatePicker.frame];
} else {
[pickerView setHidden:NO];
}
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
CGFloat pickerViewYposition = self.view.frame.size.height - pickerView.frame.size.height;
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addSubview:controlToolbar];
[pickerView addSubview:theDatePicker];
[theDatePicker setHidden:NO];
[self.view addSubview:pickerView];
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYposition,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}
And to dismiss the DatePicker:
-(void) cancelDateSet {
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}

self.view.userInteractionEnabled = NO;
self.blurView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.blurView setBackgroundColor:[UIColor lightGrayColor]];
[self.blurView setAlpha:0.3];
[[[UIApplication sharedApplication] delegate].window addSubview:self.blurView];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
__block CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, 220);
self.viewForPicker = [[UIView alloc] initWithFrame:frame];
[self.viewForPicker setBackgroundColor:[UIColor whiteColor]];
[self.viewForPicker addSubview:pickerView];
[[[[UIApplication sharedApplication] delegate] window] addSubview:self.viewForPicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[button setTitle:#"Done" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(250, 15, 60, 30)];
[button.layer setCornerRadius:5.0];
[button.layer setBorderWidth:1.0];
[button.layer setBorderColor:[[IMAppStyle appColor] CGColor]];
[self.viewForPicker addSubview:button];
[self.viewForPicker.layer setCornerRadius:8.0];
float animationDuration = 0.25;
[UIView animateWithDuration:animationDuration animations:^{
frame.origin.y -= (180+40+35);
[self.viewForPicker setFrame:frame];
}];

Related

ToolBar at the top of UIPIckerView in xcode?

I need to add a toolbar with done button on the top of UIPickerView. I don't want to use actionSheet because I want the rest of the view to be active. I've gone for the following code:
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
[txtstate resignFirstResponder];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(10, 75, 180,20);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,75,180,10);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
textField.inputAccessoryView = toolbar;
[pickerView addSubview:toolbar];
[self.view addSubview:pickerView];
}
By using the above code my toolbar is added but it is hidden under UIPickerView and it is getting added in the middle. How can I make the toolbar come to the front (on the top of UIPickerView) ?
There is a simpler way
You can insert the picker as inputView of your text Field
and add the toolbar as inputAccessoryView to your textField
like:
textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;
You can getPicker using this
-(void)getValuePicker
{
ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[ViewForValuePicker addSubview:toolBar];
valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
valuePicker.delegate=self;
valuePicker.dataSource=self;
valuePicker.showsSelectionIndicator=YES;
[ViewForValuePicker addSubview:valuePicker];
[appDelegate.window addSubview:ViewForValuePicker];
}
And its Delegete Method
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [pickerValueAry count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
id str=[ary objectAtIndex:row];
return str;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selectedRowInPicker >> %d",row);
}
You can follow my answer for more Link
in viewDidLoad just add this code and when textFieldBegin just change frame like bellow...
First Create global UIView For back View of Date Picker in .h file like bellow..
UIView *viewPicker;
UIPickerView * pickerView;
and then use it like bellow..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
viewPicker.frame = CGRectMake(0, 112, 320, viewPicker.frame.size.height);
[UIView commitAnimations];
return YES;
}
add this bellow code...
viewPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 480, 320, 258)];
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0,320,44);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 320,216);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[viewPicker addSubview:pickerView];
[viewPicker addSubview:toolbar];
[self.view addSubview:viewPicker];
and in Done button clicked just set again frame like this..
-(IBAction)doneClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
viewPicker.frame = CGRectMake(0, 370, 320, 258);
[UIView commitAnimations];
}
i hope this help you...
You could try with this code:
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
[textField resignFirstResponder];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 180, 260)];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 180,216);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0,180,44);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
textField.inputAccessoryView = toolbar;
[view addSubview:toolbar];
[view addSubview:pickerView];
[self.view addSubview:view];
return YES;
}
Ok... How about just creating a vertical stack view on the storyboard editor, adding the toolbar above and the pickerview below? They don't have to be really associated to each other, they just need to be displayed and hidden at the same time. Kinda like this below:
It is really easy to do and you don't have to do coding magic.
Yeah I know this question is 7 years old but there are still people looking for solutions to this question.
you can set your UIPickerView like this way:-
-(void)showPicker{
menu = [[UIActionSheet alloc] initWithTitle:#"Select Time"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
pickerView=[[UIPickerView alloc] init];
pickerView.frame= CGRectMake(0.0, 44.0, 0.0, 0.0);
pickerView.delegate=self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
txtopsbox.inputView=pickerView;
dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
dateTool.barStyle=UIBarStyleBlackOpaque;
[dateTool sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick)];
[barItems addObject:flexSpace];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelClicked:)];
[barItems addObject:cancel];
[dateTool setItems:barItems animated:YES];
[menu addSubview:dateTool];
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 464)];
[pickerView release];
}
UPDATE
You can also do like Another Way like:-
- (IBAction)showPicker
{
if(pickerVisible == NO)
{
// create the picker and add it to the view
if(self.datePicker == nil) self.datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 460, 320, 216)] autorelease];
[self.datePicker setMaximumDate:[NSDate date]];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker setHidden:NO];
[self.view addSubview:datePicker];
// the UIToolbar is referenced 'using self.datePickerToolbar'
[UIView beginAnimations:#"showDatepicker" context:nil];
// animate for 0.3 secs.
[UIView setAnimationDuration:0.3];
CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
datepickerToolbarFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePickerToolbar.frame = datepickerToolbarFrame;
CGRect datepickerFrame = self.datePicker.frame;
datepickerFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePicker.frame = datepickerFrame;
[UIView commitAnimations];
pickerVisible = YES;
}
}
- (IBAction)done
{
if(pickerVisible == YES)
{
[UIView beginAnimations:#"hideDatepicker" context:nil];
[UIView setAnimationDuration:0.3];
CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
datepickerToolbarFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePickerToolbar.frame = datepickerToolbarFrame;
CGRect datepickerFrame = self.datePicker.frame;
datepickerFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePicker.frame = datepickerFrame;
[UIView commitAnimations];
// remove the picker after the animation is finished
[self.datePicker performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:0.3];
}
}

UIButton does not respond to touchupinside when in a uipopover

I added a button to my UIPopoverController but it appears to not respond to touches. I don't know if I am supposed to set some property on the UIPopoverController or what. Here is the code that renders the popover view and button.
- (void)topicImageButtonPressed
{
CGRect aFrame = CGRectMake(0.0, 0.0, 1000.0, 600.0);
UIViewController *aView = [[UIViewController alloc] init];
aView.view.frame = aFrame;
UIImageView *iView = [[UIImageView alloc] init];
[iView setContentMode:UIViewContentModeScaleAspectFit];
[iView setImage:self.topicImageView1.image];
aView.view = iView;
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextButton addTarget:self
action:#selector(quizButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[nextButton setTitle:#"Next" forState:UIControlStateNormal];
nextButton.frame = CGRectMake(700.0, 550.0, 160.0, 40.0);
[aView.view addSubview:nextButton];
//aView.view.backgroundColor = [UIColor colorWithPatternImage:self.topicImageView1.image];
imagePopoverController = [[UIPopoverController alloc]
initWithContentViewController:aView];
imagePopoverController.popoverContentSize = CGSizeMake(1000, 600);
imagePopoverController.passthroughViews = [NSArray arrayWithObject:nextButton];
[imagePopoverController presentPopoverFromRect:CGRectMake(212, 10, 1000, 600) inView:self.view
permittedArrowDirections:0 animated:YES];
}
Adding this made it work.
iView.userInteractionEnabled = YES;
UIViewController *popoverContent = [[UIViewController alloc]init];
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
datepiker= [[UIDatePicker alloc]init];
[datepiker setFrame:CGRectMake(0, 0, 320, 216)];
datepiker.datePickerMode=UIDatePickerModeDateAndTime;
datepiker.hidden=NO;
datepiker.minimumDate=[NSDate date];
[self.view addSubview:datepiker];
[datepiker release];
// [datepiker addTarget:self action:#selector(changedDate:) forControlEvents:UIControlEventValueChanged];
btn_add=[[UIButton alloc]initWithFrame:CGRectMake(115, 250, 100, 30)];
[btn_add setTitle:#"Add" forState:UIControlStateNormal];
[btn_add setFont:[UIFont fontWithName:#"Arial-BoldMT" size:20]];
[btn_add setBackgroundColor:[UIColor redColor]];
[btn_add addTarget:self action:#selector(AddDate:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:btn_add];
btn_cancel=[[UIButton alloc]initWithFrame:CGRectMake(115, 300, 100, 30)];
[btn_cancel setTitle:#"Cancel" forState:UIControlStateNormal];
[btn_cancel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:20]];
[btn_cancel setBackgroundColor:[UIColor redColor]];
[btn_cancel addTarget:self action:#selector(CancelDate:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:btn_cancel];
[popoverView addSubview:datepiker];
[popoverView addSubview:btn_add];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(320,350);
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(230,250, 320,220)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[popoverView release];
[popoverContent release];
-(void)AddDate:(id)sender
{
}
-(void)CancelDate:(id)sender
{
[popoverController dismissPopoverAnimated:YES];
}
try this .. it works for me successfully.

UIPIckerView issue

I have the code below in my app. I show a UIPickerView with options, and I have a TextView placed on the same UIViewController .
when the picker is shown with options, the rest of the view seems not in focus and only when I resign the uipicker, the entire view gets focus.
what is the problem here?
-(void)viewWillAppear:(BOOL)animated {
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[myActionSheet addSubview:pickerViewCountry];
[pickerViewCountry release];
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 27.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(hidePicker) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showInView:self.view];
[myActionSheet setBounds:CGRectMake(0, 0, 320, 250)];
UITextView *myTextView = [[UITextView alloc] init];
myTextView.frame = CGRectMake(100, 12, 210, 20);
myTextView.layer.borderWidth = 1.0f;
myTextView.layer.cornerRadius = 5;
myTextView.clipsToBounds = YES;
myTextView.layer.borderColor = [[UIColor grayColor] CGColor];
myTextView.text = #"some text";
[self.view addSubview:myTextView];
[myTextView sizeToFit];
}
You've placed your UIPickerView inside UIActionSheet. That's standart behavior for UIActionSheet. Try to use keyboardView instead like so:
UITextField* dummyTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, -20, 10, 10)];//the point is to put it outside visible view
dummyTextField.inputView = pickerViewCountry;
[dummyTextField becomeFirstResponder];//call to appear
[dummyTextField resignFirstResponder];//call do disappear
and for the buttons you can use folloving:
dummyTextField.inputAccessoryView = <some toolbar with buttons or any other view>
Happy coding :)
I will suggest you one solution with custom View.
datePickerContainer is UIView:
//datepicker
datePickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 310, 300)];
datePickerContainer.opaque = YES;
datePickerContainer.backgroundColor = [UIColor clearColor];
UIImageView *pickerNavBar = [[UIImageView alloc] init];
UIImage *barImage = [UIImage imageNamed:#"pickerbar.png"];
pickerNavBar.image = barImage;
pickerNavBar.frame = CGRectMake(0, 7, barImage.size.width, barImage.size.height);
[datePickerContainer addSubview:pickerNavBar];
[pickerNavBar release];
UIButton* blueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[blueButton setTitle:#"Done" forState: UIControlStateNormal];
blueButton.frame = CGRectMake(255, 15, 55, 30);
[blueButton addTarget:self action:#selector(hidePickerViewContainer) forControlEvents:UIControlEventTouchUpInside];
[datePickerContainer addSubview:blueButton];
[blueButton release];
//UIPickerView Initialization code
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[datePickerContainer addSubview:pickerViewCountry];
[pickerViewCountry release];
//set container and release
[self.view addSubview: datePickerContainer];
[datePickerContainer release];
To show datePickerContainer UIVie use this code:
-(void)showPickerViewContainer{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 150.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
To handle done button and hide pickerView use this:
-(void)hidePickerViewContainer{
//here use you custom code to handle done action
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 440.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
This is just a custom solution. Hope this help.
declare variable in header file
UIActionSheet *actionSheet;
NSString *pickerType;
Write Below code into your implementation file:
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
-(IBAction)BtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
sessoTxt.text = [sessoArray objectAtIndex:0];
[chPicker release];
}
declare delegate methods
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}

Toolbar item not showing

Not sure what I'm doing wrong. I don't have a nib so I'm making everything in loadView. The toolbar shows up but the segmentedControl does not.
- (void)loadView
{
// Toolbar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
[toolbar setTintColor:[UIColor lightGrayColor]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
NSArray *toolbarItems = [NSArray arrayWithObjects:item, nil];
[toolbar setItems:toolbarItems animated:NO];
[self.view addSubview:toolbar];
}
Write below code in place of your code; this will help you to add segment control to your toolbar:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
[toolbar setTintColor:[UIColor lightGrayColor]];
CGRect frame;
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.width = 200;
frame.size.height = 30;
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Hello",#"Hi", nil]];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.frame = frame;
[toolbar addSubview:segmentedControl];
[self.view addSubview:toolbar];
You've got to add it as a subview of the view as you did with the toolbar. I.e:
[self.view addSubview:segmentedControl];
It should work.
Cheers

Loading MKMapView asynchronously?

When I first bring up an MKMapView my app seems to pause while it loads. I am assuming this is because it doesn't load asynchronously.
Is there any way I can get around this. While it loads you can't switch tabs or do anything.
Thanks
EDIT: here is my viewDidLoad method:
CGRect bounds = self.view.bounds;
mapView = [[MKMapView alloc] initWithFrame:bounds];
mapView.mapType=MKMapTypeStandard;
mapView.showsUserLocation = YES;
mapView.delegate = self;
[self.view insertSubview:mapView atIndex:0];
NSArray *mapTypes = [NSArray arrayWithObjects:#"Standard",#"Satellite",#"Hybrid",nil];
mapType = [[UISegmentedControl alloc] initWithItems:mapTypes];
[mapType setSegmentedControlStyle:UISegmentedControlStyleBar];
[mapType setCenter:CGPointMake(210, 345)];
[mapType setSelectedSegmentIndex:0];
[mapType setTintColor:[UIColor darkGrayColor]];
[mapType addTarget:self action:#selector(changeType:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mapType];
// refresh button...
UISegmentedControl *refreshButton = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Refresh",nil]] autorelease];
[refreshButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[refreshButton setCenter:CGPointMake(self.view.frame.size.width-((refreshButton.frame.size.width/2)+5),(refreshButton.frame.size.height/2)+5)];
[refreshButton setMomentary:YES];
[refreshButton setTintColor:[UIColor darkGrayColor]];
[refreshButton addTarget:self action:#selector(updateMarkers) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:refreshButton];
UIBarButtonItem *leftBarButton = [[[UIBarButtonItem alloc] initWithTitle:#"List" style:UIBarButtonItemStylePlain target:self action:#selector(goToList)] autorelease];
[self.navigationItem setRightBarButtonItem:leftBarButton animated:YES];
// refresh label
refreshView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width-74, 40)];
[refreshView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]];
[refreshView.layer setCornerRadius:5];
[self.view addSubview:refreshView];
UILabel *refreshLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, refreshView.frame.size.width-10, refreshView.frame.size.height-10)] autorelease];
[refreshLabel setTextColor:[UIColor whiteColor]];
[refreshLabel setBackgroundColor:[UIColor clearColor]];
[refreshLabel setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[refreshLabel setNumberOfLines:2];
[refreshLabel setLineBreakMode:UILineBreakModeWordWrap];
[refreshLabel setText:#"Move the map and press 'refresh' to load new results."];
[refreshView addSubview:refreshLabel];
[NSTimer scheduledTimerWithTimeInterval:10 target:refreshView selector:#selector(removeFromSuperview) userInfo:nil repeats:NO];