I have multiple textFields. In one of them, i want to display a UIPickerView.
I did it this way:
-(void)showPicker{
[subject becomeFirstResponder];
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
picker = [[UIPickerView alloc] initWithFrame:pickerFrame];
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
[picker release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObject:#"Close"]];
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:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)dismissActionSheet{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
When i close it, the sheet is being dismissed, but the keyboard on the subject textField appears.
Now:
How can i do to not to show the keyboard on that subject textField?
Here is where i resign the keyboard:
- (void) resignKeyboard:(id)sender{
if ([name isFirstResponder])
[name resignFirstResponder];
else if ([email isFirstResponder])
[email resignFirstResponder];
else if ([message isFirstResponder])
[message resignFirstResponder];
else if ([subject isFirstResponder]) {
[subject resignFirstResponder];
}
}
Use UItextField delegates: For that u need add #interface YourViewController :UIViewController
If UItextField taken from xib by right clicking add its delegate to fileowner
Now use this method in code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == subject)
{
return NO;
}
return YES;
}
It is possible to prevent the keyboard from popping up.
Firstly set your class as the delegate of UITexfield
textField.delegate = self
In your Interface Declaration add:
<UITextFieldDelegate>
Now implement textFieldShouldBeginEditing::
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
return NO;
}
Solved by using a transparent button which handles the action to show the picker. The "subject" textField was disabled.
Related
want to get input from pickerView in textField when alert show Objective C
NOTE: Opening actionsheet from alertview first dismiss your alertview then open actionsheet.My code may help you regarding opening actionsheet from alertview
#import "ViewController.h"
#interface ViewController ()<UITextFieldDelegate,UIActionSheetDelegate>
{
NSString *alertSelectValue;
UITextField * alertTextField;
}
#end
#implementation ViewController
- (void)viewDidLoad {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Hello!" message:#"Please enter your name:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.tag=101;
alertTextField.delegate=self;
alertTextField.keyboardType = UIKeyboardTypeNumberPad; alertTextField.placeholder = #"Enter your name";
[alert show];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField.tag==101)
{
[textField setUserInteractionEnabled:YES];
[textField resignFirstResponder];
NSString*String = [NSString stringWithFormat:#"%#",textField.text];
[self openSheet:String];
}
}
-(void)openSheet:(NSString*)text
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select" delegate:self cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:text,#"Test" ,nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
UIViewController *topvc=[self topMostController];
[actionSheet showInView:topvc.view];
}
//-------- Get top Most view to -----
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Index = %d - Title = %#", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
if(buttonIndex == 0)
{
alertTextField.text= [actionSheet buttonTitleAtIndex:buttonIndex];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have a UITableView set up programatically, and it is populated with all the names of the states. When you click on one of the cells, for ex. ALABAMA, a UIAlertView will appear With the title being that of the state you just clicked on (ALABAMA) and a number that correlates to that state, which is in another NSMutable Array. Every state has a different number to correlate with it.
This number is actually a placeholder text in the UIAlert, and the purpose of the alert is so that you can input a number in the text field, and when you hit the CHANGE button, the number that was in the placeholder text (from the Array) will be changed with the number that the user imputed; permanently. All is good until I hit change, and then I get a SIGABRT error, and in output it reads:
2013-02-21 20:57:33.750 final[10046:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil'
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[NSString stringWithFormat:#"%#", [states objectAtIndex:indexPath.row]]
message:[NSString stringWithFormat:#"%#", [tax objectAtIndex:53]]
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Change", nil];
UITextField *myTextField =[[UITextField alloc] initWithFrame:CGRectMake(42, 50, 200, 25)];
CGAffineTransform myTransform =CGAffineTransformMakeTranslation(0, 0);
[alertView setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor clearColor]];
[alertView addSubview:myTextField];
myTextField.placeholder = [NSString stringWithFormat:#"%#", [tax objectAtIndex:indexPath.row]];
myTextField.borderStyle = UITextBorderStyleNone;
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.textColor = [UIColor grayColor];
myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
myTextField.textAlignment = UITextAlignmentCenter;
myTextField.delegate = self;
[alertView show];
[alertView release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *theTaxer = [myTextField text];
[tax replaceObjectAtIndex:1 withObject:theTaxer];
}
}
At the point when alertView:clickedBUttonAtIndex is called myTextField is no longer defined since myTextField isn't defined as a class ivar you can only reference it in the function you created it in. If you want to reference myTextField elsewhere in your class (in the alertView callback for instance) make it a class iVar.
Try this,
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[NSString stringWithFormat:#"%#", [states objectAtIndex:indexPath.row]]
message:[NSString stringWithFormat:#"%#", [tax objectAtIndex:53]]
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Change", nil];
alertView.tag =indexPath.row;
UITextField *myTextField =[[UITextField alloc] initWithFrame:CGRectMake(42, 50, 200, 25)];
[myTextField setTag:99];
CGAffineTransform myTransform =CGAffineTransformMakeTranslation(0, 0);
[alertView setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor clearColor]];
[alertView addSubview:myTextField];
myTextField.placeholder = [NSString stringWithFormat:#"%#", [tax objectAtIndex:indexPath.row]];
myTextField.borderStyle = UITextBorderStyleNone;
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.textColor = [UIColor grayColor];
myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
myTextField.textAlignment = UITextAlignmentCenter;
myTextField.delegate = self;
[alertView show];
[alertView release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UITextField * myTextField = (UITextField *)[alertView viewWithTag:99];
NSString *theTaxer = [myTextField text];
[tax replaceObjectAtIndex:1 withObject:theTaxer];
}
}
Shizam is right, I think if you want get the reference of the textview, just give a tag to the textview when you create the alertview like this: myTextField.tag = 111;
and then in the alertView delegate method:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UITextField *textField = [alertView viewWithTag:111];
NSString *theTaxer = [textField text];
[tax replaceObjectAtIndex:1 withObject:theTaxer];
}
}
So I have a picker that pops up instead of the keyboard, however it does not change values when you scroll to different points on the picker. I test this by having an alert show what age was selected, and it never changes from the first option. I would think the alert is broken, but the blue selector outline over the currently selected option in the picker is not showing up either. Here is the code for both the picker and the alert:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:#"18-29",#"30-39",#"40-49",#"50-59",#"60+",nil];
self.pickerData = array;
}
-(IBAction)UITextFieldEditingDidBegin
{
UIPickerView *picker = [[UIPickerView alloc]
initWithFrame:CGRectMake(0, 244, 320, 270)];
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
[picker release];
}
-(IBAction)donePressed
{
NSInteger row = [agePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:
#"You selected %#!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message : #"Thank you for choosing."
delegate:nil
cancelButtonTitle :#"Okay!"
otherButtonTitles :nil];
[alert show];
[agePicker resignFirstResponder];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return[pickerData objectAtIndex:row];
}
Edit,
to help dismiss your picker,
make it an instance var, declare your picker in the interface,
MyClass.h
#interface MyClass:UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
UIPickerView *picker
}
MyClass.m
#interface MyClass()
property (nonatomic, retain) *selectedVal; //make it a NSString
#end
-(IBAction)UITextFieldEditingDidBegin
{
picker = [[UIPickerView alloc]
initWithFrame:CGRectMake(0, 244, 320, 270)];
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
//[picker release]; //Place your picker release in the dealloc method
}
You have to implement the delegate for the picker,
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.selectedVal = [pickerData objectAtIndex:row]
NSLog(#"Selected option: %#. ", self.selectedVal );
}
Then on your done button, you have the selected val in your property for use
-(IBAction)donePressed
{
NSString *title = [[NSString alloc] initWithFormat:
#"You selected %#!", self.selectedVal];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message : #"Thank you for choosing."
delegate:nil
cancelButtonTitle :#"Okay!"
otherButtonTitles :nil];
[alert show];
[alert release]; //I noticed you release the picker, so not using ARC, so release alert!!
[picker resignFirstResponder];
//after done, remove the picker from the view
[picker removeFromSuperview];
}
I'm having an issue with my iOS app freezing whenever I trigger a UIActionSheet with a UIPickerView inside it. The picker wheel scrolls fine until I try to hit the "Done" button on the UIActionSheet, at which point the UI freezes. However, XCode isn't registering any kind of crash in the program, so I'm pretty confused.
Has anyone else run into this problem before? How can I solve it?
I never Face this type of problem.I think this one is hekp you to solve your problem. I used the PickerView in same way
UIActionSheet *actionSheet;
NSString *pickerType;
- (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];
}
#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;
}
}
Hi In my app I have a button so when you click it a UIPopover comes up with an add contact view in it. It all workers except when you press save. It doesn't Dismiss.
-(IBAction) addcontact
{
ABNewPersonViewController *contacts = [[ABNewPersonViewController alloc] init];
// imagePicker.delegate = self;
// UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:contacts];
UINavigationController *addContactNavController = [[UINavigationController alloc] initWithRootViewController:contacts];
popover = [[UIPopoverController alloc] initWithContentViewController:addContactNavController];
popover.popoverContentSize = CGSizeMake(320, 1000);
[popover presentPopoverFromRect:CGRectMake(935, 270, 175, 300)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionRight
animated:YES];
[popover retain];
[addContactNavController release];
[contacts release];
}
Implement the ABNewPersonViewControllerDelegate protocol and assign the delegate in your method above –
contacts.newPersonViewDelegate = self;
You can then dismiss the popover in the delegate function –
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView
didCompleteWithNewPerson:(ABRecordRef)person {
[popOver dismissPopoverAnimated:YES];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[popOver release];
}
well,
[self dismissModalViewControllerAnimated:YES];
respecively
[popover dismissPopoverAnimated:YES];
should do it?
Edit: to be more concrete:
....
popover = [[UIPopoverController alloc] initWithContentViewController:addContactNavController];
addContactNavController.delegate = self;
now on saving, do something like:
-(IBAction) saveStuff {
... saving...
[delegate closePopup];
}
and in your File with -(IBAction) addcontact you do:
-(void) closePopup {
[self dismissModalViewCotroller...];
}
makes sense?
and yes, you should add a delegate-propery to your controller if not done yet