How to add a done button over the pickerview? - iphone

I want to display a picker view and a done button above the picker view .
I have a custom label which will become first responder . So when it becomes first responder I am displaying a picker view as the input view for my label.
Now I want to have a done button on the picker to dismiss the picker view. I played with the inputAccesoryView property of UIPickerView but did not succeed . inputAccessoryView is possible with UITextView , not sure about the UIPickerView . Here is the link for how it can be done with UITextView
UITextView with “Done” button and “Return” key?
Has any body tried this , if someone knows how to do this for picker , please answer.
Thank you all in advance !!

Add your picker view to an action sheet. First of all make sure youre view controller impliments UIPickerViewDelegate and UIActionSheetDelegate.
Then add this method, and call it when you want to show your picker.
- (void)showPicker {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Pick Value"
delegate:self
cancelButtonTitle:#"Done"
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,180,0,0)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
[menu addSubview:pickerView];
[menu showInView:self.view.superview];
//Change the height value in your CGRect to change the size of the actinsheet
[menu setBounds:CGRectMake(0,0,320, 615)];
[pickerView release];
[menu release];
}

I think you can try to put the picker inside a view with a 'Done' button and present that view

Related

iPhone application : UIDatePicker for two text fields on the same view

UIPickerView select and hide
I used this to pop up a date picker view when I "touch down" on a text field. That is, I need the text field to display whatever I choose on the date picker view as its contents.
- (IBAction)showPicker:(id)sender {
// create a UIPicker view as a custom keyboard view
UIDatePicker* pickerView = [[UIDatePicker alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
//pickerView.delegate = self;
//pickerView.dataSource = self;
//pickerView.showsSelectionIndicator = YES;
self.datePickerView = pickerView; //UIDatePicker
fromDate.inputView = pickerView;
// create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
// Prepare done button
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self
action:#selector(pickerDoneClicked:)] autorelease];
fromDate.text = (NSString *)datePickerView.date; // fromDate is the Text Field outlet, where I am trying to display the selection on the picker.
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
// Plug the keyboardDoneButtonView into the text field...
fromDate.inputAccessoryView = keyboardDoneButtonView;
[pickerView release];
[keyboardDoneButtonView release];
}
- (IBAction)pickerDoneClicked:(id)sender {
[fromDate resignFirstResponder];
}
Edit : I am able to do this for one text field on the screen. No matter what, only a keyboard pops up for the second text field.
Any ideas to do this for a second text field?
fromDate.inputView = pickerView;
This line of code makes the picker view the input view for the fromDate field. You need similar code for the other date field. You'll also need to set the input accessory on your other field.
Creating a special method to do this and touch down actions on all your text fields is unnecessary work. Simply create the picker view and toolbar in viewDidLoad and assign them to the input views and accessory views of whatever fields you want them on. Text fields alread have a touch down method which will bring up the keyboard.
Just an alternate solution for your problem.
1) Put a custom UIButton over the textfield on tap of which you want to show the UIDatePickerView
2) set the delegate of the textfield to nil on which you want to show the picker view.
fromDate.delegate = nil;
3) on the IBAction("touch down") of that custom button write the below code :-
- (IBAction) customButtonAction: (id) sender
{
[_previousTextField resignFirstResponder]; // The instance of last textfield tapped.
[self showDatePicker];
}
4) On tap of "Done" button of Picker view set selected value to the textfield
- (IBAction)pickerDoneClicked:(id)sender
{
fromDate.text = #"value from UIPickerView's selected row"
}

how is a drop-down list made on the iphone?

I want for a given label Example: Typemarital (possible answers: married, single, PACS, divorced, etc ...) to choose from a predefined list and know how to freeze the answers to a user on a field and not a view (without using the builder interface ) just through the code .
Thanks
It can be made using a button and and a picker view.
1) On click of button, you have to show the picker view and from picker you have to select the value you want. Then from inputAccessoryView of picker view in which you can add a toolbar with done button.
2) On done button click you can get selected value of the picker and hide the picker.
3) The selected value then you can show it into a UILabel.
Hope this helps you.
EDIT:
Here is a very useful tutorial for dropdownmenu in iPhone/iPad:
http://kshitizghimire.com.np/dropdown-in-iphoneipad/
Its quite simple actually.
On click of label create a view and inside it create a table view where you can display list you require, and by simply clicking on cell i.e.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
place text of selected cell to your label and remove your view.
Hope it helps.....
you have to use UITableView.
1) Create a button with title.
2) when clicked on button tableView will be shown.
3) when one of the row of tableview would selected the tableview goes hide and the button title would be row selected in tableview..
You can use UIActionSheet. Add UIPickerView in UIActionSheet.
On tap of a button Try this:
category = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
category.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
UIPickerView *categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,50, 320, 120)];
categoryPicker.delegate = self;
categoryPicker.dataSource = self;
categoryPicker.showsSelectionIndicator = YES;
[categoryPicker selectRow:1 inComponent:0 animated:NO];
[category addSubview:categoryPicker];
UISegmentedControl *segmentedControlDone = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
segmentedControlDone.momentary = YES;
segmentedControlDone.frame = CGRectMake(260, 7, 50, 30);
segmentedControlDone.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControlDone.tintColor = [UIColor blackColor];
[segmentedControlDone addTarget:self action:#selector(actionPickerDone) forControlEvents:UIControlEventValueChanged];
[category addSubview:segmentedControlDone];
// implement data source method of UIPicker also

last Button of actionsheet does not get clicked

I have used an actionsheet in my project and when it appears it show all buttons but last (4th) button does not responds to my click(only it's half part responds)..
I know the reason it is because i have used a TabBarController and the present class is inside that tabbar controller....
only that part of the actionsheet is responding which is above the tabs....and my last button is half above and half is on top of tabbar
please help
i suggest using this:
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
I had the same problem that you have and using this method to show it worked for me. The TabBar wants to stay key Window what makes your bottom button appear above, but is actually under the tabbar.
Hope this does the trick for you..
Edit
If you use landscape mode and the method above doesn't work. You can use the following fix:
#Vinh Tran: [sheet showFromTabBar:self.parentViewController.tabBarController.tabBar]
What method do you use to show your actionsheet. Try showFromTabBar: method
The real problem comes in, when your interface is rotated to landscape and the parent view controller has a transformation on it. Believe me, that's a realistic scenario, doh. Then the action sheet is clipped and you can't use the parentViewController because it is transformed. The solution to avoid all these issues is to create a new window, add a rotatable view controller as rootViewController and use its view to display the sheet.
CGRect applicationRect = [[UIScreen mainScreen] bounds];
UIWindow* actionSheetWindow = [[UIWindow alloc] initWithFrame:applicationRect];
RotationViewController* rootViewController = [[RotationViewController alloc] initWithNibName:nil bundle:nil];
actionSheetWindow.rootViewController = rootViewController;
[rootViewController release];
actionSheetWindow.hidden = NO;
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil];
[actionSheet setCancelButtonWithTitle:#"Cancel" handler:^{
actionSheetWindow.hidden = YES;
[actionSheetWindow release];
}];
[actionSheet showInView:rootViewController.view];
The code above uses BlocksKit, but you can do it also by using the actionSheet delegate and instance properties.
RotationViewController is just a UIViewController subclass that implements
- (void) viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

Strange Problem with UIDatePicker in ActionSheet - Below that Selection indicator I can't select anything, It's become like disabled

I am implementing datepicker with Action Sheet with the following code :
-(void)popupActionSheetWithDatePicker
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Done",nil];
UIDatePicker *picker = [[UIDatePicker alloc] init];
self.datePicker = picker;
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[sheet addSubview:self.datePicker];
[sheet showInView:self.view];
[sheet setBounds:CGRectMake(0,0, 320,500)];
CGRect pickerRect = self.datePicker.bounds;
pickerRect.origin.y = -80;
self.datePicker.bounds = pickerRect;
[sheet bringSubviewToFront:self.datePicker];
[picker release];
[sheet release]; }
Now, The Picker pops up & working perfactly Except below the Selection Indicator.
I mean On & Above the Selection indicator I am able to choose date, month & year, but Below that Selection indicator I can't select anything, It's become like disabled.
Can't find the reason or solution.
If Possible Please send any working Datepicker with UIActionsheet code.
Thanks in advance.
I have had a very similar issue with parts of a view not working when used with a UIActionSheet. My problem was that the bounds of the UIActionSheet were larger than the view I had added it to.
My solutions was to change where i inserted the sheet
[sheet showInView:self.view];
You may need to add it higher up for example
[sheet showInView:self.view.superview];
Hope this at least sets you on the right track
I had this problem also , i supose you have an UIToolbar behind the picker. Just do
[self.navigationController setToolbarHidden:YES];
while in picker , then
[self.navigationController setToolbarHidden:NO];
hope this help

Editable TextView with Second NavBar - Text appears, but too late

Editable TextView with Second NavBar - Text appears, but too late.
The app has a single Navigation Controller.
I have an iPhone App that has basically three levels.
Level 1 - Table with category Names
Level 2 - Table with list of items for selected category
Level 3 - Tabbed View with several views, including UITextView for details of item
One to these Tabbed Views with a TextView is editable.
When the user taps in the editable TextView the KeyBoard
appears. User can type in the TextView. Characters appear
as they are typed.
At the top of this Level 3 TextView there is a NavBar (present for all 3 levels with
changes) with a BackButton and a "home->Level1" button on the right.
All works just fine until in the editable TextView I add a second NavigationBar
below the existing NavBar. This second NavBar has two buttons
as well. They are Save/Cancel.
When I click these Save and Cancel buttons the correct action
methods are reached. All is perfect with one exception, The text
which is typed does not appear in the TextView until either
the Save or the Cancel button is touched. The relevant Button setup and
action methods in my TabViewController.m are below. I need to persist this
data.
I thought that getting a Notification from the TextView and the action handleTextChange would do the trick, but no luck. I am stuck.
.........
- (void)loadView {
self.myTextView = [[UITextView alloc] init];
self.myTextView.delegate = self;
self.view = self.myTextView;
//UITextViewTextDidChangeNotification
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(handleTextChange:)
name:UITextViewTextDidChangeNotification
object:nil];
NSLog(#"Registered DG_HandleChangeTextNotification with notification center.");
}
- (void)handleTextChange:(NSNotification * )note
{
[self.myTextView setNeedsDisplay] ;
NSLog(#"...Handled Text Change.");
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// provide my own Done/Save button to dismiss the keyboard
saveNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
saveNavigationBar.barStyle = UIBarStyleBlackOpaque;
UINavigationItem *doneItem = [[UINavigationItem alloc] init];
doneItem.title = #"My Notes";
UIBarButtonItem *doneItemButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:#selector(saveAction:)];
UIBarButtonItem *cancelItemButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self
action:#selector(cancelAction:)];
[doneItem setRightBarButtonItem:doneItemButton animated:NO];
[doneItem setLeftBarButtonItem:cancelItemButton animated:NO];
[saveNavigationBar pushNavigationItem:doneItem animated:NO];
[self.view addSubview:saveNavigationBar];
[doneItem release];
[cancelItemButton release];
[doneItemButton release];
}
- (void)saveAction:(id)sender
{
// finish typing text/dismiss the keyboard by removing it as the first responder
self.text = self.myTextView.text;
[self.saveNavigationBar removeFromSuperview];
[self.myTextView resignFirstResponder];
}
- (void)cancelAction:(id)sender
{
[self.saveNavigationBar removeFromSuperview];
[self.myTextView resignFirstResponder];
}
The Second NavBar was hiding the area of the UITextEdit
such that I had to type about four lines before I saw the text. I believe
I need to lower the height of the UITextEdit by 44 pixels.