how is a drop-down list made on the iphone? - 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

Related

Calling an ActionSheet from a TabBar

I have a tab bar with 4 buttons (Home, Visits, Share, More). I want the "Share" button in my tab bar to call an actionsheet which has the appropriate buttons to select the particular channels I want the user to be able to distribute links to the app (Facebook, Twitter, Email, etc.).
I'm able to do this using Storyboard but I have to create a unique (blank) view controller that is linked to the "Share" button in the tab bar. I put the following code within the viewDidAppear method to display the actionsheet when the "Share" button is selected:
int selectedTab = self.tabBarController.selectedIndex;
if (selectedTab == 2)
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Share Your Visit with Friends!" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Facebook", #"Twitter", #"Email eduLaunchpad", nil];
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}
When I close the actionsheet, I then return by default to the home view:
[self.tabBarController setSelectedIndex:0];
While this works, it isn't optimal from a user perspective. I would prefer the actionsheet to appear over the particular view that was displaying when the "Share" button was selected. For example, if the user was on the "Visits" view and selected "Share" from the tab bar, I would like the actionsheet to appear over the top of the visits view with that view being visible behind the actionsheet.
Am I able to achieve this using Storyboard? Or do I need a custom tab bar in order to implement this functionality? If a custom tab bar is required, I would appreciate some guidance/insight into how to implement that including the custom tab bar as well as the actionsheet from the tab bar.
Thanks in advance!
I needed this, too, googled around, and eventually built the following category on UITabBarController.
The trick to getting the action sheet to appear on the current tab is that you never want to actually visit that action-sheet-presenting tab. Cover the tab bar in that position with a UIButton which initiates the action sheet.
Paint your tab bar as you normally would in storyboard, leaving an empty bar item at the position where the special behavior is to occur. Then add this category...
// UITabBarController+Button.h
#interface UITabBarController (Button) <UIActionSheetDelegate>
- (void)replaceItemAtIndex:(NSUInteger)index withButtonImage:(UIImage*)buttonImage;
#end
// UITabBarController+Button.m
#import "UITabBarController+Button.h"
#define kBUTTON_TAG 4096
#implementation UITabBarController (Button)
- (void)replaceItemAtIndex:(NSUInteger)index withButtonImage:(UIImage*)buttonImage {
UIButton *button = (UIButton *)[self.view viewWithTag:kBUTTON_TAG];
if (!button) {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = kBUTTON_TAG;
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UITabBar *bar = self.tabBar;
CGFloat width = bar.frame.size.width / bar.items.count;
button.frame = CGRectMake(index*width, bar.frame.origin.y, width, bar.frame.size.height);
[self.view addSubview:button];
}
}
#pragma mark - Handle button tap
- (void)buttonTapped:(id)sender {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Action Sheet:"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Action A", #"Action B", #"Action C", nil];
[sheet showFromTabBar:self.tabBar];
}
Call it with an index < tabBar.items.count and an image for the button. If it's your app's root vc, you can call it as follows:
#import "the category i suggest above"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// whatever else you do in here, then
NSLog(#"%#", self.window.rootViewController); // make sure this is a UITabBarController
// if it is, then ...
UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
[tbc replaceItemAtIndex:2 withButtonImage:[UIImage imageNamed:#"some image name"]];
return YES;
}
Instead of opening actionsheet from tabbar
[actionSheet showFromTabBar:self.tabBarController.tabBar];
use this code.
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];

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 to add a done button over the pickerview?

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

How to add a UISwitch to a cell in a grouped table view?

So I'm creating a settings page in my app and up to now I've set it up with a grouped table view with both group titles and settings/cell tiles.
At this point I'd like to add UISwitches (Already have their properties set up) to each cell. How can I do that?
The easiest way is to add a UISwitch as the accessory view for the UITableViewCell.
UISwitch* aswitch = [[UISwitch alloc] initWithFrame:CGRectZero];
aswitch.on = YES; // or NO
cell.accessoryView = aswitch;
[aswitch release];

How to display text in subview by button click?

Hi I am new to iPhone.
What I need is, have to display some text as help for my application. For that I create a button while clicking that button text must be displayed.
How can I do this?
Please post some code. Thank you.
Inside your button click event,
for example.
-(IBAction) showTextView:(id)sender{
yourSubViewController * subView = [[yourSubViewController alloc] initWithNibName:#"yourSubViewController" bundle:nil];
[self.navigationController pushViewController:subView animated:YES];
}
and inside yourSubViewController viewDidLoad method,
UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
textView.text = #"display your info text";
[self.view addSubview:textView];
before you must declare UITextViewDelegate in yourSubViewController.h
on button click event load new UIviewController.
add uitextview in it and load your help text in that textview