UITextfield mimic Dropdown (UITableView?) - uitableview

I want to add the feature that when i tap on a specific textfield, then there is a Dropdown / TableView in front of the gui and i can select a value and then the selected value is then inserted into the textfield?
How to achieve this in the easiest way?

Depends, if this is on the iPad, you can use UIPopoverController and present a UITableViewController inside it.
If not, you can present a UIActionSheet and add UIPickerView or a UITableView inside it...in this example, I am creating a UIActionSheet and placing a UIPickerView inside it:
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? #"\n\n\n\n\n\n\n\n\n" : #"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:#"%#%#", title, [entry objectForKey:#"Name"]]
delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Ok", nil];
[actionSheet setDelegate:self];
[actionSheet setTag:row];
pv = [[UIPickerView alloc] init];
[pv setShowsSelectionIndicator:YES];
[pv setDelegate:self];
[pv setTag:28];
[actionSheet addSubview:pv];
[actionSheet showInView:[[[self navigationController] tabBarController] view]];
[pv release];
Notice the "\n"s, you need those to make room for the Picker view. Here's the result:

Related

How to dismiss the UIAlertview on the click of UIViewController or on the UIScrollView in objective c

I am new in iOS and I am facing the problem regarding to dismiss the UIAlertview. I am showing Image in UIAlertview and I am using long press gesture to call the alertview. My code is like this
ImageName.userInteractionEnabled=YES;
UILongPressGestureRecognizer *longpressgestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[longpressgestureRecognizer addTarget:self action:#selector(imgLongPressed:)];
longpressgestureRecognizer.delegate = self;
[ImageName addGestureRecognizer: longpressgestureRecognizer];
- (void) imgLongPressed:(UILongPressGestureRecognizer*)sender
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)];
UIImage *wonImage = ImageName.image;
imageView.contentMode=UIViewContentModeCenter;
[imageView setImage:wonImage];
alertView = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alertView setValue:imageView forKey:#"accessoryView"];
}else{
[alertView addSubview:imageView];
}
[alertView show];
[self performSelector:#selector(dismiss:) withObject:alertView afterDelay:1.0];
}
-(void)dismiss:(UIAlertView*)alert
{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
This code give me output like this Image
I need to dismiss the UIAlertview on the touch of UIViewController or UIScrollView.
UIAlertView is depreciated. It is generally not advised to use depreciated elements since they might cause some problems with latest versions of iOS. For the given functionality, I think it's better to use thirdparty AlertViews like this one: https://cocoapods.org/pods/SCLAlertView-Objective-C

UIReferenceLibraryViewController in a popover

I have this portion of code to pull up the dictionary if a word is searched:
- (IBAction)searchButtonPressed:(id)sender
{
NSString *searchTerm = self.searchTextField.text;
if([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:searchTerm])
{
UIReferenceLibraryViewController *referenceLibraryVC = [[UIReferenceLibraryViewController alloc] initWithTerm:searchTerm];
[self presentModalViewController:referenceLibraryVC animated:NO];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Word not found" message:#"no definition" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
however, it only seems to work on the main view in the xib and covers the entier iPad screen. How can I get it so that it only opens up in a subview, just a portion of the screen?
Use a UIPopoverController, like it is used in native applications.
self.popover = [[UIPopoverController alloc] initWithContentViewController:referenceLibraryVC];
[self.popover presentPopoverFromRect:[sender frame] inView:[sender superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You will need to retain the popover controller until it is dismissed. Set the delegate and you can listen when it is dismissed so you can release it.

UIActionSheet With PickerView not showing up in iPad

I am using the following code to add an actionsheet with a picker view on it to my app. From the iPhone, it displays perfectly, but when viewing on iPad (Universal app), it shows a very small pickerview with none of the other buttons right on the center of the page.
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Select Chapter"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Select"
otherButtonTitles:nil];
// Add the picker
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,185,0,0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES; // note this is default to NO
[menu addSubview:pickerView];
[menu showFromTabBar:self.tabBarController.tabBar];
[menu setBounds:CGRectMake(0,0,320, 700)];
[pickerView release];
[menu release];

Selection indicator not showing in picker view inside an action sheet

Here's my implementation, I'm basically creating the action sheet and picker view on the fly. The problem is the indicator to show which item you have selected isn't showing.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100, 320, 216)];
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0, 320, 411)];
It looks like you're missing this line:
[picker setShowsSelectionIndicator:YES];

Is having a UIPickerView in a UIActionSheet acceptable?

Is it acceptable (aka would Apple consider it acceptable) to have a UIPickerView in a UIActionSheet?
Yes, It is totally acceptable. To some degree its even encouraged.
Example code: HERE by Erica Sadun in her book iPhone Developer's cookbook. Chapter 11 Recipe 21
Yes. It is acceptable.
set the frame of the UIPickerView
add the UIPickerView to the actionView
As I remember, actionView will be full screen in this case
UIActionSheet *actionView = [[UIActionSheet alloc] initWith...];
UIPickerView *pickerView = [UIPickerView alloc] init...];
pickerView.frame = CGRect(....);
[actionView addSubview:pickerView];
[pickerView release];
[actionView showInView:theView];
[actionView release];
This should do it :
NSString *title = #"\n\n\n\n\n\n\n\n\n\n\n\n"; // <--- Taken from Erica Sadun´s CookBook
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet setBounds:CGRectMake(0,0,320,485)];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
[actionSheet addSubview:picker];
[actionSheet showFromTabBar:[[self tabBarController] tabBar]];
Please remember that you have to set UIPickerView´s dataSource and delegate .

Resources