I am having UIAlertview with UITextField in that.Mine is iPad application
-(void)showAlertToAddLink
{
alertToAddLink =[[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"ADD", nil];
alertToAddLink.tag = 2;
alertToAddLink.alertViewStyle = UIAlertViewStylePlainTextInput;
[txtLinkName setDelegate:self];
/*
UITextField *txtLinkName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 50.0)];
[txtLinkName setDelegate:self];
// txtLinkName.text=#"";
[txtLinkName setFont:[UIFont fontWithName:#"Helvetica" size:17.0]];
[txtLinkName setTextColor:[UIColor colorWithRed:71.0/255.0 green:78.0/255.0 blue:80.0/255.0 alpha:1]];
[txtLinkName setBorderStyle:UITextBorderStyleRoundedRect];
[txtLinkName setBackgroundColor:[UIColor whiteColor]];
[txtLinkName setKeyboardAppearance:UIKeyboardAppearanceAlert];
[txtLinkName setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtLinkName setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[txtLinkName setClearButtonMode:UITextFieldViewModeWhileEditing];
[txtLinkName setTextAlignment:UITextAlignmentLeft];
// [txtLinkName becomeFirstResponder];
[alertToAddLink addSubview:txtLinkName];
*/[alertToAddLink show];
}
I tried with
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[alertToAddLink resignFirstResponder];
return YES;
}
But alertview is not being resig.
What is the way to do that
Add this line to your textFieldShouldReturn:.
This will trigger dismissal of your alert view when textFieldShouldReturn: will be called:
[alertToAddLink dismissWithClickedButtonIndex:alertToAddLink.cancelButtonIndex animated:YES];
Hope it helps
Related
I have a textfield. once I tap on that I want a UIPickerView to pop out and select an item from the UIPickerView and after once I am done with the selection I want to confirm the selection by tapping on an UIActionSheet which has confirm button on it.So my requirement is to have a UIActionSheet and beneath that a picker view. I am just a beginner in iOS development and I am still not familiar with the technical terminologies. So please bear with my informal way of asking questions.
Here is my block of code:
creating UIPickerView and UIActionSheet
UIActionSheet *confirmRoomSelectionAS =[[UIActionSheet alloc]initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Done",nil];
UIPickerView *roomTypePicker=[[UIPickerView alloc]init];
[confirmRoomSelectionAS addSubview:roomTypePicker];
[confirmRoomSelectionAS showInView:self.view];
[confirmRoomSelectionAS setBounds:CGRectMake(0, 0, 320, 600)];
[roomTypePicker setFrame:CGRectMake(0, 170, 320, 216)];
[roomTypePicker setDataSource:self];
[roomTypePicker setDelegate: self];
roomTypePicker.showsSelectionIndicator = YES;
Try like this,
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet showInView:[self.view window]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 390)];
[actionSheet addSubview:[self createToolbar:#"Test"]];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 162);
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
- (UIView *)createToolbar:(NSString *)titleString {
UIToolbar *inputAccessoryView = [[UIToolbar alloc] init];
inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[inputAccessoryView sizeToFit];
CGRect frame = inputAccessoryView.frame;
frame.size.height = 44.0f;
inputAccessoryView.frame = frame;
UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancelBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel:)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 , 11.0f, 100, 21.0f)];
[titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:14]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:titleString];
[titleLabel setTextAlignment:UITextAlignmentCenter];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
NSMutableArray *array = [NSMutableArray arrayWithObjects:cancelBtn,flexibleSpaceLeft,title,flexibleSpaceLeft, doneBtn, nil];
[inputAccessoryView setItems:array];
[doneBtn release];
[title release];
[titleLabel release];
[flexibleSpaceLeft release];
[cancelBtn release];
return [inputAccessoryView autorelease];
}
- (void)done:(id)sender {
}
- (void)cancel:(id)sender {
}
It will be like,
3 things I thought you should modify:
1) add your cancel button manually, to later better check the cancel button pressing event
UIActionSheet * confirmRoomSelectionAS = [[UIActionSheet alloc]
initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[confirmRoomSelectionAS addButtonWithTitle:#"Done"];
confirmRoomSelectionAS.cancelButtonIndex = [confirmRoomSelectionAS addButtonWithTitle: #"Cancel"];
[confirmRoomSelectionAS showInView:self.view];
2) implement your actionsheet delegate method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) {
// cancel button pressed;
return;
}
}
3) You should declare *UIPickerView picker in your .h to later check of its selected value in the previous delegate method (also including the Done check) by adding sthing like this:
NSInteger row;
row = [picker selectedRowInComponent:0];
Got the following code:
-(void)addGrade {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Text" message:#"\n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
// Textfield1
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[utextfield becomeFirstResponder];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
utextfield.leftView = paddingView;
utextfield.leftViewMode = UITextFieldViewModeAlways;
utextfield.placeholder = #"Subject";
[utextfield setBackgroundColor:[UIColor whiteColor]];
utextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[alertview addSubview:utextfield];
// Textfield2
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 90.0, 25.0)];
ptextfield.placeholder = #"Another Subject";
ptextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
ptextfield.textAlignment = NSTextAlignmentCenter;
[ptextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:ptextfield];
// Textfield3
UITextField *ctextfield = [[UITextField alloc] initWithFrame:CGRectMake(161.0, 85.0, 27.0, 25.0)];
ctextfield.placeholder = #"3";
[ctextfield setBackgroundColor:[UIColor whiteColor]];
ctextfield.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
ctextfield.textAlignment = NSTextAlignmentCenter;
[alertview addSubview:ctextfield];
// Label1
UILabel *telt = [[UILabel alloc] initWithFrame:CGRectMake(121.0, 85.0, 40.0, 25.0)];
telt.text = #"Text";
telt.textColor = [UIColor whiteColor];
telt.backgroundColor = [UIColor clearColor];
[alertview addSubview:telt];
// Label2
UILabel *keermee = [[UILabel alloc] initWithFrame:CGRectMake(199.0, 85.0, 100.0, 25.0)];
keermee.text = #"more text";
keermee.textColor = [UIColor whiteColor];
keermee.backgroundColor = [UIColor clearColor];
[alertview addSubview:keermee];
[alertview show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Add"])
{
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (#"%#", field.text);
}
}
This code makes 3 Textfields and 2 labels in an UIAlertView.
With UITextField *field = (UITextField *)[[alertView subviews] lastObject]; I got back "more text" which is true because you are asking about the last object.
Now is my question how to get the content of all the UITextfield not only the label?
for (UIView *view in [alertView subviews]){
if ([view isMemberOfClass:[UITextField class]]){
UITextField *textField = (UITextField *)view;
NSLog(#"%#", textField.text);
}
}
Assign tags and retrieve them with those.
myTextField.tag = 100;
Then to find it use this function:
myTextField = [view subviewWithTag: 100];
I created a UIAlertView that is in ViewDidLoad, as the following:
prompt = [[UIAlertView alloc] init];
prompt.title = #"تسجيل الدخول";
prompt.message = #"\n\n\n";
prompt.delegate = self;
[prompt addButtonWithTitle:#"الغاء"];
[prompt addButtonWithTitle:#"دخول"];
userNameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[userNameTxtField setBackgroundColor:[UIColor whiteColor]];
[userNameTxtField setPlaceholder:#"اسم المستخدم"];
[prompt addSubview:userNameTxtField];
passwordTxtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
[passwordTxtField setBackgroundColor:[UIColor whiteColor]];
[passwordTxtField setPlaceholder:#"كلمة المرور"];
[passwordTxtField setSecureTextEntry:YES];
[prompt addSubview:passwordTxtField];
[prompt show];
//[prompt release];
// set cursor and show keyboard
[userNameTxtField becomeFirstResponder];
and the clickedButtonAtIndex function is as following:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex ==0) {
NSLog(#"cancel");
}else {
NSLog(#"will login isA");
}
}
but when I click any buttons, I see the output and then the app crashes and gives me the following Exception:
[MaktabatyTableViewController respondsToSelector:]: message sent to deallocated instance 0x894cfa0
Any help ????
i have tried your code. your code working nicely .
-(void)viewDidLoad
{
UIAlertView *prompt = [[UIAlertView alloc] init];
prompt.title = #"تسجيل الدخول";
prompt.message = #"\n\n\n";
prompt.delegate = self;
[prompt addButtonWithTitle:#"الغاء"];
[prompt addButtonWithTitle:#"دخول"];
UITextField *userNameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[userNameTxtField setBackgroundColor:[UIColor whiteColor]];
[userNameTxtField setPlaceholder:#"اسم المستخدم"];
[prompt addSubview:userNameTxtField];
UITextField *passwordTxtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
[passwordTxtField setBackgroundColor:[UIColor whiteColor]];
[passwordTxtField setPlaceholder:#"كلمة المرور"];
[passwordTxtField setSecureTextEntry:YES];
[prompt addSubview:passwordTxtField];
[prompt show];
//[prompt release];
// set cursor and show keyboard
[userNameTxtField becomeFirstResponder];
}
I am new to iphone.Is it possible to keep 3 textfields, 3 labels and 2 uibuttons in an actionsheet or alertview. Kindly give information.
Thanks in advance.
You can try this as:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"\n\n\n\n\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *textfield1 = [[UITextField alloc] init];
textfield1.frame = CGRectMake(10.0, 30.0,260.0, 15.0);//set your frame as you required
textfield1.text=#"";
[textfield1 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:textfield1];
[textfield2 release];
UITextField *textfield2 = [[UITextField alloc] init];
textfield2.frame = CGRectMake(10.0, 48.0,260.0, 15.0);//set your frame as you required
textfield2.text=#"";
[textfield2 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:textfield2];
[textfield2 release];
UITextField *textfield3 = [[UITextField alloc] init];
textfield3.frame = CGRectMake(10.0, 65.0,260.0, 155.0);//set your frame as you required
textfield3.text=#"";
[textfield3 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:textfield3];
[textfield3 release];
UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(10.0, 80.0, 260.0, 100.0)];//set your frame as you required
tempView.backgroundColor = [UIColor clearColor];
[alert addSubview:tempView];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 5, 50, 15);//set your label frame here
label1.text = #"Enter text";
[tempView addSubView:label1];
[label1 release];
UILabel *label2 = [[UILabel alloc]init];
label2.frame = CGRectMake(0, 25, 50, 15);//set your label frame here
label2.text = #"Enter text";
[tempView addSubView:label2];
[label2 release];
UILabel *label3 = [[UILabel alloc]init];
label3.frame = CGRectMake(0, 45, 50, 15);//set your label frame here
label3.text = #"Enter text";
[tempView addSubView:label3];
[label3 release];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = //set your button frame here
[tempView addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = //set your button frame here
[tempView addSubview:button2];
[tempView release];
[alert show];
[alert release];
HI all below is the code i am using, the textField is created in .h file and when i run the code the text field doesnt respond to keyboard( no letters typed are visible) also when i click Done on keybpard it does not trigger textfieldshouldreturn method. please help!!!
UIActionSheet *aac =[[UIActionSheet alloc] initWithTitle:#"New list item" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0,100.0,0.0,0.0)];
self.textField=[[UITextField alloc] init];
self.textField.frame = CGRectMake(12, 45, 260, 25);
[self.textField setDelegate:self];
self.textField.textColor=[UIColor blackColor];
self.textField.backgroundColor=[UIColor whiteColor];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setReturnKeyType:UIReturnKeyDone];
textField.keyboardType=UIKeyboardAppearanceDefault;
[aac addSubview:textField];
[aac addSubview:theDatePicker];
[textField release];
[theDatePicker release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary=YES;
closeButton.frame = CGRectMake(270, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle=UISegmentedControlStyleBar;
closeButton.tintColor =[UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
[aac addSubview:closeButton];
[closeButton release];
[aac showInView:self.view];
[aac setBounds:CGRectMake(0, 0, 320, 685)];
Have you tried removing the self. from before textField? If you have defined it in your header, as well as used #property, and #synthesize in your .m, you should be good. Also, make sure you have the <UITextFieldDelegate> in your interface.
I just had the same problem; My textfield did bring up the keyboard when I touched it, but no letters appeared in the textfield when tapping on the keyboard.
In my case I had been messing with the AppDelegate.m and removed the [self.window makeKeyAndVisible];. So putting it back again (in application:didFinishLaunchingWithOptions:) solved it for me.