Created 3 UITextField in UIAlertView but not getting action - ios

I have created an UIAlertView with three (3) UITextField. How i can get the action of UIAlertView. The clickedButtonAtIndex delegate method is working.
But not getting the text i input in the field. Any idea ? Thanks in advance.
UIView *v_alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
v_alert.tag=10;
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = #"Your Previous Password";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[v_alert addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = #"Your New Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[v_alert addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = #"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[v_alert addSubview:textField3];
alertV = [[UIAlertView alloc] initWithTitle:#"Change Password" message:#"" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alertV setValue:v_alert forKey:#"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
In delegate :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"didDismissWithButtonIndex");
UITextField*txt1=[[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:0];
UITextField*txt2= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:1];
UITextField*txt3= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:2];
NSLog(#"%#--%#--%#",txt1.text,txt2.text,txt3.text);
}

Declare Your 3 TextField
UITextField *textField1, *textField2, *textField3;
Then replace this code with your code.
UIView *alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
alertview.backgroundColor = [UIColor whiteColor];
alertview.tag=10;
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = #"Your Previous Password";
textField1.secureTextEntry = YES;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[alertview addSubview:textField1];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = #"Your New Password";
textField2.secureTextEntry = YES;
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[alertview addSubview:textField2];
textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = #"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[alertview addSubview:textField3];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:#"Change Password" message:#"" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alertV setValue:alertview forKey:#"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
Now your UIAlertView Button Method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(#"Button No click");
}
if (buttonIndex == 1) {
NSLog(#"Button Yes Cancel");
}
NSLog(#"%#--%#--%#",textField1.text,textField2.text,textField3.text);
}
Output :
Gems--Moradiya--Himmoradiya#gmail.com

First set textField delegate
<UITextFieldDelegate>
Then I tried the below code
NSString *strAddress = #"14 UPPER CIRCULAR ROAD, Singapore 058412";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:strAddress delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = #"Username";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[v addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = #"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[v addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = #"Address";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
[v addSubview:textField3];
[alert setValue:v forKey:#"accessoryView"];
[alert show];
TextField Delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(#"The delegate method is called");
return YES;
}
Also see my screen shot
The output result is
The delegate method is called

That's the trick i accepted. But the standard way is for latest iOS:
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Change Password"
message:#""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
UITextField *alertText1 = [alert.textFields objectAtIndex:0];
UITextField *alertText2 = [alert.textFields objectAtIndex:1];
UITextField *alertText3 = [alert.textFields objectAtIndex:2];
if(alertText1.text.length>0&&alertText1.text.length>0)
{
NSLog(#"Resolving UIAlert Action for tapping OK Button:%#-%#-%#",alertText1.text,alertText2.text,alertText3.text);
oldpPass=alertText1.text;
NewPass=alertText2.text;
[self update_password];
}
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Resolving UIAlertActionController for tapping cancel button");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = #"Enter old password";
textField.accessibilityIdentifier = #"paswordTextField";
textField.accessibilityLabel = #"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = #"Enter New password";
textField.accessibilityIdentifier = #"paswordTextField";
textField.accessibilityLabel = #"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.accessibilityIdentifier = #"usernameTextField";
textField.placeholder = #"Enter email";
textField.text=self.emailTF.text;//
textField.accessibilityLabel = #"usernameLabel";
}];
[self presentViewController:alert animated:YES completion:nil];

Related

i want to add done button and return the date in textfiled in alertview controller

actually i want this in objective c
i need some help for this type of code.
if any can help then proceed step by step.
i need done button and return date to text field.
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"date";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}];
You can use this complete example as you want.
#import "ViewController.h"
#interface ViewController ()<UITextFieldDelegate>{
UIDatePicker *datepicker;
UIToolbar *toolbar;
UITextField *dateField;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
datepicker = [[UIDatePicker alloc]init];
datepicker.datePickerMode= UIDatePickerModeDate;
datepicker.backgroundColor = [UIColor whiteColor];
toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolbar setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(setDate)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancelDatePicker)];
[toolbar setItems:[NSArray arrayWithObjects:cancelBtn,flex,donebtn, nil]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)showAlertController:(id)sender {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Add Action"
message:#"Please enter notes or action"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* save = [UIAlertAction actionWithTitle:#"Save" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(#"Notes: %#", alert.textFields[0].text);
NSLog(#"Date: %#", alert.textFields[1].text);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:save];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Notes";
textField.delegate = self;
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Date";
textField.delegate = self;
[textField setInputView:datepicker];
[textField setInputAccessoryView:toolbar];
dateField = textField;
}];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)setDate{
NSDate *selectedDate = datepicker.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
dateField.text = [dateFormatter stringFromDate:selectedDate];
[dateField resignFirstResponder];
}
-(void) cancelDatePicker{
[dateField resignFirstResponder];
}
#end
set
[alert textFieldAtIndex:0].delegate = self;
implement
- (BOOL)textFieldShouldReturn:(UITextField *)alertTextField {//add any method you want to execute to here. This method will be called when user taps on the textfield in alertview.
[ alertTextField resignFirstResponder];// to dismiss the keyboard.
[alert dismissWithClickedButtonIndex:0 animated:YES];//this is called on alertview to dismiss it.
}
.h
#interface CustomViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate>
{
UIAlertView *alert;
}
delegate
alertTextField.delegate = self;
yourMethod
(void) yourMethod
{
alert = [[UIAlertView alloc] initWithTitle:#"YourTitle" message:#"YourMessage" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Load",nil];
}
UIAlertController *alert;
//...
-(void)showAlertController {
alert = [UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *button = [UIAlertAction actionWithTitle:#"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
//Do your thing
//NSString *strTextField_1 = alert.textFields[0].text;
//NSString *strTextField_2 = alert.textFields[1].text;
//NSString *strTextField_3 = alert.textFields[2].text;
}];
[alert addAction:button];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
[textField setPlaceholder:#"Notes"];
}];
__weak typeof(self) weakSelf = self;
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setMinimumDate:[NSDate date]];
[picker setTag:1];
[picker addTarget:weakSelf
action:#selector(datePickerUpdated:)
forControlEvents:UIControlEventValueChanged];
[textField setInputView:picker];
[textField setPlaceholder:#"date"];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setMinimumDate:[NSDate date]];
[picker setTag:2];
[picker addTarget:weakSelf
action:#selector(datePickerUpdated:)
forControlEvents:UIControlEventValueChanged];
[textField setInputView:picker];
[textField setPlaceholder:#"date"];
}];
[self presentViewController:alert
animated:YES
completion:nil];
}
-(void)datePickerUpdated:(UIDatePicker *)datePicker {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *string = [formatter stringFromDate:datePicker.date];
NSInteger currentIndex = datePicker.tag;
[alert.textFields[currentIndex] setText:string];
}

How to hide and unhide the UIAlertView.?

I am opening an alert view when the view will appear. My alertview style is UIAlertViewStylePlainTextInput.I am saving the textfield text in NSUserDefaults. I want when the textfield have text in their textfield alert is not open But if the textfield is empty only then the alert is pop up on the screen.I am using the below code.enter code here
- (void)viewDidLoad {
[super viewDidLoad];
proAlert = [[UIAlertView alloc]initWithTitle:#"Pro-Tracking Number" message:#"Firstly enter the protracking number here" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[proAlert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
proTextField.text = [[proAlert textFieldAtIndex:0]text];
}
}
proTextField = [[UITextField alloc]initWithFrame:CGRectMake(170, 35, 150, 40)];
proTextField.textColor=[UIColor blackColor];
//proTextField.placeholder = #"Pro/Tracking no";
NSUserDefaults *proNum = [NSUserDefaults standardUserDefaults];
proTextField.text = [proNum valueForKey:#"proTracking"];
[view2 addSubview:proTextField];
-(void)viewWillAppear:(BOOL)animated
{
[activity stopAnimating];
NSString *textString =[[proAlert textFieldAtIndex:0]text];
[proTextField.text length];
myText =textString;
NSLog(#"Textfield text - %#",myText);
NSUInteger length = [myText length];
NSLog(#"LENGTH of string %lu",(unsigned long)length);
if(myText<0)
{
proAlert.hidden = NO;
}
else
{
proAlert.hidden = YES;
}
}
-(void)viewWillAppear:(BOOL)animated {
if (txtField.text.length<=0) {
proAlert = [[UIAlertView alloc]initWithTitle:#"Pro-Tracking Number" message:#"Firstly enter the protracking number here" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[proAlert show];
}
}

executing a popup from another popup button in ios

I am new in IOS. When I'm clicking on the following Login Button:
I'm using this code to generate a popup:
- (UIView *)createLoginView
{
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)];
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)];
email.tag=420;
email.layer.cornerRadius = 5;
[email setTag:99];
[email setTextAlignment:NSTextAlignmentCenter];
[email setKeyboardType:UIKeyboardTypeEmailAddress];
[email setPlaceholder:#"Email"];
[email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[email setReturnKeyType:UIReturnKeyNext];
[email setAutocorrectionType:UITextAutocorrectionTypeNo];
UITextField *pass = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, 280, 50)];
pass.layer.cornerRadius = 5;
[pass setTag:100];
[pass setTextAlignment:NSTextAlignmentCenter];
[pass setSecureTextEntry:YES];
[pass setPlaceholder:#"Password"];
[pass setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[pass setReturnKeyType:UIReturnKeyGo];
[alertView addSubview:email];
[alertView addSubview:pass];
return alertView;
}
The login popup is working properly but when I click on the 'Forget?' button of the popup, the forgot password popup does not show up. Here is the code for forget password popup.
- (UIView *)createForgotPasswordView
{
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)];
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)];
email.layer.cornerRadius = 5;
[email setTag:22];
[email setTextAlignment:NSTextAlignmentCenter];
[email setKeyboardType:UIKeyboardTypeEmailAddress];
[email setPlaceholder:#"Email"];
[email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[email setReturnKeyType:UIReturnKeyNext];
[email setAutocorrectionType:UITextAutocorrectionTypeNo];
[alertView addSubview:email];
return alertView;
}
And I'm using it like :
- (IBAction)LogInClick:(UIButton *)sender {
IOS7AlertView *applyMsg = [[IOS7AlertView alloc] init];
[applyMsg setContainerView:[self createLoginView]];
[applyMsg setButtonTitles:[NSMutableArray arrayWithObjects:#"Forgot?", #"Login", nil]];
[applyMsg setOnButtonTouchUpInside:^(IOS7AlertView *alertView, int buttonIndex) {
if(buttonIndex==0)
{
[alertView close];
NSLog(#"Forgot Section");
IOS7AlertView *applyPop = [[IOS7AlertView alloc] init];
[applyPop setParentView:[self createForgotPasswordView]];
[applyPop setButtonTitles:[NSMutableArray arrayWithObjects:#"Next", #"Cancel", nil]];
}
}];
But the problem is the next popup does not show up? What is the problem? Please help.
already UIalertview hierarchy has email and password options.
try this and this is native
- (IBAction)LogInClick:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Login"
message:nil
delegate:self
cancelButtonTitle:#"Signup"
otherButtonTitles:#"Login",#"Forgot password", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:0]setPlaceholder:#"Email address"];
[[alert textFieldAtIndex:1]setPlaceholder:#"Password"];
alert.tag=10;
[alert show];
}
and the alert view delegate method is
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag==65)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Login"
message:nil
delegate:self
cancelButtonTitle:#"Signup"
otherButtonTitles:#"Login",#"Forgot password", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:0]setPlaceholder:#"Email address"];
[[alert textFieldAtIndex:1]setPlaceholder:#"Password"];
alert.tag=10;
[alert show];
}
if (alertView.tag==10)
{
if (buttonIndex == 0) {
NSLog(#"signup");
//Navigate to Signup page
}
else if (buttonIndex == 2) {
NSLog(#"Forgot password");
// //Navigate to Forgot password page
}
else if (buttonIndex == 1) {
NSLog(#"YES");
NSString *usernameInput=[alertView textFieldAtIndex:0].text;
NSString *passwordInput=[alertView textFieldAtIndex:1].text;
//pass the email id , password
NSLog(#"1 %#", usernameInput);
NSLog(#"2 %#", passwordInput);
NSString *emailReg = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailReg];
// COMMENT: check if input information is valid or not
if([usernameInput isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:#"You must fill in Email address" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
alert.tag=65;
[alert show];
}
else if([emailTest evaluateWithObject:usernameInput] == NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!!" message:#"Please Enter Valid Email Address." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag=65;
[alert show];
}
else if([passwordInput isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:#"You must fill in Password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
alert.tag=65;
[alert show];
}
else
{
//pass teh data to server or login process
}
}
}

Resign is not working in UITextField of UIAlertView?

i created alert view,in alert view one text field will come but when i click return button on keyboard it doesn't disappear,even i add the delegate to .h file.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(IBAction)barButtonPressed:(id)sender
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Enter Data" message:#"\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.placeholder = #"Name";
textUserName.autocorrectionType = UITextAutocorrectionTypeNo;
textUserName.textAlignment=UITextAlignmentLeft;
textUserName.userInteractionEnabled = YES;
textUserName.enabled = YES;
textUserName.enablesReturnKeyAutomatically= NO;
textUserName.clearsOnBeginEditing = NO;
textUserName.borderStyle = UITextBorderStyleRoundedRect;
textUserName.keyboardType = UIKeyboardTypeDefault;
textUserName.delegate = self;
//[textUserName setReturnKeyType:UIReturnKeyDone];
**strong text**
[alert
addSubview:textUserName];
[alert show];
[self resignFirstResponder];
}
thank you
You can use UIAlertViewStylePlainTextInput of UIAlertView to get text field in alertView :
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Enter Data" message:#"\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
// Set alertView style
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
textUserName =[alert textFieldAtIndex:0];
// Then customize your textField
textUserName.placeholder = #"Name";
textUserName.autocorrectionType = UITextAutocorrectionTypeNo;
textUserName.textAlignment=UITextAlignmentLeft;
textUserName.userInteractionEnabled = YES;
textUserName.enabled = YES;
textUserName.enablesReturnKeyAutomatically= NO;
textUserName.clearsOnBeginEditing = NO;
textUserName.borderStyle = UITextBorderStyleRoundedRect;
textUserName.keyboardType = UIKeyboardTypeDefault;
[alert show];
In this no need of setting delegate separately..
You need to set delegate property of UITextField after initializing it.
use:
textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.delegate = self;

Uialertview text to NSMutableArray, ios 4.3

i have been trying to copy text input from alertview textfield to a NSMutableArray that i will use later, alertview pops up and i enter the input to text field but when i press OK alert view disappears but doesnt copy text to my mutable array
here is my code
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter a Stand Location"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = #"";
[dialog addSubview:nameField];
if ([nameField text]){
NSLog(#"Name Field %# ",nameField.text);
[addCustomStand addObject:nameField.text];
}
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
NSLog(#"Button 1 was selected.");
NSLog(#"StandLocations %# ",addCustomStand);
}
}
here is my output on log screen
2012-02-07 20:26:57.315 Avicii[1399:b603] Name Field
2012-02-07 20:26:59.720 Avicii[1399:b603] Button 1 was selected.
2012-02-07 20:26:59.721 Avicii[1399:b603] StandLocations (
""
)
anyone can help whats wrong with that code?
That's because [nameField text] doesn't have user entered value yet when you added it in your [addCustomStand addObject:nameField.text];
so change your adding into array in UIAlertView delegate method.
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter a Stand Location"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = #"";
// Note at this line
nameField.tag = 100;
//
[dialog addSubview:nameField];
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
// Note at this line
UITextField* nameField = (UITextField *)[alertView viewWithTag:100];
[addCustomStand addObject:nameField.text];
//
NSLog(#"Button 1 was selected.");
NSLog(#"StandLocations %# ",addCustomStand);
}
}
You are adding nameField.text to your addCustomStand array before you even show the alert dialog. At the time you add it to the array the value is an empty string.
Instead you need to copy the value into your array during your clickedButtonAtIndex: method, by doing something like this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
NSString *location;
UIView *view;
for (view in [alertView subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
location = [(UITextField*)view text];
}
}
if (location) {
[addCustomStand addObject:location];
}
}
}

Resources