executing a popup from another popup button in ios - 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
}
}
}

Related

Created 3 UITextField in UIAlertView but not getting action

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];

UIAlertViewStyleLoginAndPasswordInput placeholder text won't set

I have an app that uses a UIAlertViewStyleLoginAndPasswordInput alert view. Everything works fine with the exception that the password field's placeholder text has some strange behavior. On my sim it refuses to change from 'Password' regardless of what I set it as. And Ive been having reports that it flip flops between 'Password" and the value I wish to set it at. This seems to be all iOS7 related.
I am attempting to set these values in the 'willPresentAlertView' delegate.
- (void)willPresentAlertView:(UIAlertView *)alertView
{
...
else if (alertView.tag == UIAlertViewTagInactivityTimeOut)
{
badgeNumberTextField = [alertView textFieldAtIndex:0];
verifyBadgeNumberTextField = [alertView textFieldAtIndex:1];
[badgeNumberTextField setPlaceholder:#"Badge Number"];
[verifyBadgeNumberTextField setPlaceholder:#"Verify Badge Number"];
[verifyBadgeNumberTextField setSecureTextEntry:NO];
[badgeNumberTextField setFont:[UIFont systemFontOfSize:ALERT_VIEW_TEXTFIELD_TEXT_FONT]];
[verifyBadgeNumberTextField setFont:[UIFont systemFontOfSize:ALERT_VIEW_TEXTFIELD_TEXT_FONT]];
[badgeNumberTextField setKeyboardType:UIKeyboardTypeNamePhonePad];
[verifyBadgeNumberTextField setKeyboardType:UIKeyboardTypeNamePhonePad];
[badgeNumberTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[verifyBadgeNumberTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[badgeNumberTextField setDelegate:self];
[verifyBadgeNumberTextField setDelegate:self];
...
//other code
}
}
Have you tried set the placeholder where you create the UIAlertView?
Try this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert textFieldAtIndex:0].placeholder = #"Username placeholder";
[alert textFieldAtIndex:1].placeholder = #"Password placeholder";
[alert show];

iOS: Simplified UIAlertView Code So It Can Be Used Over And Over Again

this is my second day on learning XCode, so I'm totally newbie here and I have this .m code :
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[_fieldEmail setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
[_fieldPassword setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
[_titleLogin setFont:[UIFont fontWithName:#"Raleway-ExtraLight" size:28]];
UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldEmail.leftView = fieldEmail;
UIView *fieldPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldPassword.leftView = fieldPassword;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)validateEmail:(NSString *)emailStr {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}
- (IBAction)buttonRegister {
_labelOutput.text = #"Register?";
}
- (IBAction)buttonLogin {
if ([_fieldEmail.text length] > 0) {
if(![self validateEmail:[_fieldEmail text]]){
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Email Error"
message:#"That's not a valid email!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = #"Login?";
}
}else{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Please enter your email"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
}
- (IBAction)loginFacebook {
if ([_fieldPassword.text length] > 15) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Password Error"
message:#"Password must be less than 15 characters."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = #"Login with FB?";
}
}
- (IBAction)loginTwitter {
// _labelOutput.text = #"Login with Twitter ya?";
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Please enter your email correctly."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
#end
as you can see there, the code I used to display alert box is repeated. Is there any way to put this similar code somewhere up there so it can be called just by giving Title and Message as variable :
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Please enter your email correctly."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
In PHP world, this method named function, but I don't know what's name in Objective-C world. thank you very much.
To reduce your code why dont you create another method to show the AlertBox ?
- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)msg
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
Then simply call :
[self alertWithTitle:(YOUR TITLE) andMessage:(YOUR MESSAGE)];
You have written a handful of methods, have you considered just writing another one? E.g.
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString *)message
{
...
}
and then when you need an alert call this method, e.g.:
[self showAlertWithTitle:#"Login Error" andMessage:#"Please enter your email correctly."];
HTH

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