How to hide and unhide the UIAlertView.? - ios

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

Related

Keyboard doesn't hide in iOS even on resignFirstResponder

I have a login page with username,password textfield and a submit button.
If the focus is on password textfield and the user presses the submit button without pressing the return key on keyboard.
The keyboard is dismissed as i uses resignFirstResponder to dismiss the keyboard but when i hit the server and the alert comes of invalid credentials,then within nano seconds when alert is shown,keyboard also appears.
Used the below line of code to dismiss keyboard on Login Button click.
[self.passwdTxtFld setDelegate:self];
[self.usernameTxtFld setDelegate:self];
[self.view endEditing:YES];
Below is the code from where i get the response from server and also displays the alert.
- (void)checkLogin
{
NSLog(#"???%#",self.usernameTxtFld.text);
NSDictionary *checkLogin=[pwInteract initializeWithOrgId:#"JVVC" AppId:#"JVVC" LoginId:self.usernameTxtFld.text Password:self.passwdTxtFld.text];
NSLog(#"checklogin is %#",checkLogin);
if(checkLogin)
{
if(![[checkLogin objectForKey:#"NOTIFICATION"]isEqualToString:#"Y"] && ![[checkLogin objectForKey:#"NOTIFICATION"]isEqualToString:#"NV"]){
[loggingInAlert dismissWithClickedButtonIndex:loggingInAlert.cancelButtonIndex animated:YES];
[loggingInAlert removeFromSuperview];
NSLog(#"?? response is %#",checkLogin);
NSString *result = [checkLogin objectForKey:#"IS_AUTH"];
NSString *error = [checkLogin objectForKey:#"ERROR"];
if ([result isEqualToString:#"Y"] )
{
if([self.usernameTxtFld.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:#"username"]]){
[[NSUserDefaults standardUserDefaults] setObject:#"olduser" forKey:#"usertype"];
}
else{
[[NSUserDefaults standardUserDefaults] setObject:#"newuser" forKey:#"usertype"];
}
[[NSUserDefaults standardUserDefaults]setObject:#"" forKey:#"fetch"];
[[NSUserDefaults standardUserDefaults]setObject:#"" forKey:#"state"];
[[NSUserDefaults standardUserDefaults]setObject:#"success" forKey:#"state"];
[[NSUserDefaults standardUserDefaults]setObject:self.usernameTxtFld.text forKey:#"username"];
[self performSegueWithIdentifier:#"success" sender:nil];
}
else if (![error isEqualToString:#""])
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:#"Error" message:error delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
networkAlertView.tag = 1;
[networkAlertView show];
[self.view endEditing:YES];
if([self.passwdTxtFld isFirstResponder]){
NSLog(#"pass");
}
if([self.usernameTxtFld isFirstResponder]){
NSLog(#"ddd");
}
}
else if ([result isEqualToString:#""])
{
NSString *error = [checkLogin objectForKey:#"ERROR"];
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:#"Error" message:error delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
networkAlertView.tag = 3;
[networkAlertView show];
}
else
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:#"" message:#"Oops! Either Username or Password is incorrect. Please type correct Username and Password to proceed." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
networkAlertView.tag = 3;
[networkAlertView show];
}
}
else if([[checkLogin objectForKey:#"NOTIFICATION"]isEqualToString:#"NV"]){
[customAlert hideActivityIndicator];
}
}
else
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:#"Server Error" message:#"No Response From Server." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
networkAlertView.tag = 3;
[networkAlertView show];
}
}
On click event of login button have resigned both the username and password textfield,also the UITextFieldDelegate are connected.
Mistake:-
According to your code you are just resigning your keyboard in one "else if" condition:-
else if (![error isEqualToString:#""])
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:#"Error" message:error delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
networkAlertView.tag = 1;
[networkAlertView show];
[self.view endEditing:YES];
What about the others else if condition, Things will be resolved if you resign keyboard [self.view endEditing:YES] in the very first statement of (void)checkLogin method.
Try [self.view endEditing:YES] when you tap login button or just before you show alert.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
or try
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return YES;
}
// It is important for you to hide the keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Also check UITextFieldDelegate delegate method

Multiple UIAlertViews to Enter Data

I have two buttons that will pop up an alertview with textfield to input data. However, only certain characters are allowed in each of the two textfields. Somehow, if I press the second button, the character set from the first button is used. What's going on here?
Also, what would be a more elegant form of inputting data without having to use an alertview? Could I use a modal view? If so, how?
- (IBAction)editRate
{
if(!self.powerOn) return;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit Jail Fee Rate"
message:#"Enter New Daily Rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
UITextField *field = [alertView textFieldAtIndex:0];
field.placeholder = #"Enter New Rate";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers are allowed in this field."delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil];
[errorAlert show];
FeeRate.text=#"0.00";
}
else
{
FeeRate.text = field.text;
[[NSUserDefaults standardUserDefaults] setObject:field.text forKey:RATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
else
{
}
}
- (IBAction)editDate
{
if(!self.powerOn) return;
UIAlertView *alertDate = [[UIAlertView alloc] initWithTitle:#"Edit Jail Fee Date"
message:#"Enter New Date"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertDate.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alertDate textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alertDate show];
}
- (void)alertDate:(UIAlertView *)alertDate clickedButtonAtIndex:(NSInteger)buttonIndex2
{
if (buttonIndex2 != alertDate.cancelButtonIndex)
{
UITextField *fieldDate = [alertDate textFieldAtIndex:0];
fieldDate.placeholder = #"Enter New Date";
NSCharacterSet * setnew = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789/"] invertedSet];
if ([fieldDate.text rangeOfCharacterFromSet:setnew].location != NSNotFound)
{
UIAlertView *errorAlert1 = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers and slashes are allowed in this field."delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil];
[errorAlert1 show];
FeeDate.text=#"00/00/0000";
}
else
{
FeeDate.text = fieldDate.text;
[[NSUserDefaults standardUserDefaults] setObject:fieldDate.text forKey:DATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
else
{
}
}
When a UIAlertView is dismissed then a function called
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
is called.
So, both your alert views call this function.
One way to tell which alertView is calling it, you could create an ivar or better two properties like this:
#property (nonatomic, strong) UIAlertView *rateAlert;
#property (nonatomic, strong) UIAlertView *dateAlert;
and you should initialize like this:
[self setRateAlert:[[UIAlertView alloc] initWithTitle...
[self.rateAlert show];
and
[self setDateAlert:[[UIAlertView alloc] initWithTitle...
[self.dateAlert show];
and then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView==self.rateAlert) {
//do whatever for rateAlert
} else {
//do whatever with dateAlert
}
}

UIAlertView keeps re-appearing after dismissWithClickedButtonIndex included in performSelector: withObject: afterDelay:

I have a button which I want to implement with password before triggering a segue if the password is correct. it all looks fine up to the moment when you type in wrong password and I have implemented another alertView to tell the user the password is wrong. When the alert view pops out and dismisses after some delay, it keeps re-appearing and disappearing and nothing else can be done on the screen!
How to stop the re appearing?
Below is my part of the code that deals with this:
- (IBAction)editLeagues:(id)sender {
[self presentAlertViewForPassword];
}
-(void)presentAlertViewForPassword
{
_passwordAlert = [[UIAlertView alloc]initWithTitle:#"Password"
message:#"Enter Password to edit Leagues"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[_passwordAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
_passwordField = [_passwordAlert textFieldAtIndex:0];
_passwordField.delegate = self;
_passwordField.autocapitalizationType = UITextAutocapitalizationTypeWords;
_passwordField.tag = textFieldPassword;
[_passwordAlert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSString *password = [NSString stringWithFormat:#"55555"];
if ( ![_passwordField.text isEqual:password]) {
_wrongPassword = [[UIAlertView alloc] initWithTitle:#"Wrong Password"
message:#"You are not authorised to use this feature!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[_wrongPassword show];
[self performSelector:#selector(allertViewDelayedDissmiss:) withObject:nil afterDelay:2];
}
else
{
[self performSegueWithIdentifier:#"addLeague" sender:[alertView buttonTitleAtIndex:0]];
}
}
-(void) allertViewDelayedDissmiss:(UIAlertView *)alertView
{
[_wrongPassword dismissWithClickedButtonIndex:-1 animated:YES];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 4 )
{
return YES;
}
else
{
return NO;
}
}
[_wrongPassword dismissWithClickedButtonIndex:-1 animated:YES]; will call the delegate method alertView:didDismissWithButtonIndex:
You have two options:
don't set a delegate on the wrong password alert
check for the correct alert in alertView:didDismissWithButtonIndex: e.g.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alert == _passwordAlert) {
NSString *password = [NSString stringWithFormat:#"55555"];
// and so on
}
}
Issue is causing because when you dismiss the wrong password alert it'll also call the didDismissWithButtonIndex delegate method.
Solution 1
Set the delegate of wrong password alert to nil.
wrongPassword = [[UIAlertView alloc] initWithTitle:#"Wrong Password"
message:#"You are not authorised to use this feature!"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
Solution 2
Add a tag to your alertView. And change your methods like:
-(void)presentAlertViewForPassword
{
_passwordAlert = [[UIAlertView alloc]initWithTitle:#"Password"
message:#"Enter Password to edit Leagues"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[_passwordAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
passwordAlert.tag = 7;
_passwordField = [_passwordAlert textFieldAtIndex:0];
_passwordField.delegate = self;
_passwordField.autocapitalizationType = UITextAutocapitalizationTypeWords;
_passwordField.tag = textFieldPassword;
[_passwordAlert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 7)
{
NSString *password = [NSString stringWithFormat:#"55555"];
if ( ![_passwordField.text isEqual:password])
{
_wrongPassword = [[UIAlertView alloc] initWithTitle:#"Wrong Password"
message:#"You are not authorised to use this feature!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[_wrongPassword show];
[self performSelector:#selector(allertViewDelayedDissmiss:) withObject:nil afterDelay:2];
}
else
{
[self performSegueWithIdentifier:#"addLeague" sender:[alertView buttonTitleAtIndex:0]];
}
}
}

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

How do I make an alert show how long it took to complete a certain amount of questions in xcode?

I want to show the user how long it took him or her to answer a certain amount of questions (for example 5) as an alert. I believe that I have the correct timer in place although I could be wrong. How would I finish the coding?
- (void)updateCounter:(NSTimer *)theTimer {
static int count = 0;
count += 1;
NSString *s = [[NSString alloc]
initWithFormat:#"%d", count];
self.timer.text = s;
[s release];
}
- (void)generate
{
int a = 1 + arc4random() % 12;
int b = 1 + arc4random() % 12;
int sum = a * b;
label.text = [NSString stringWithFormat: #"%d * %d = ", a, b];
label.tag = sum;
}
- (void)viewDidLoad
{NSLog(#"viewDidLoad");
[super viewDidLoad];
[self generate];
[self.answer becomeFirstResponder];
UIAlertView *alert;
{alert = [[UIAlertView alloc]
initWithTitle:#"Welcome to iCanMultiply!"
message:#"Tap 'Start' to start the game"
delegate: self
cancelButtonTitle:#"Start"
otherButtonTitles: nil];
}
[alert show];
[alert release];
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateCounter:)
userInfo:nil
repeats:YES];
}
- (IBAction)submit:(id)sender {
int num = [answer.text intValue];
UIAlertView *alert;
if (num == label.tag)
{
// answer was correct
alert = [[UIAlertView alloc]
initWithTitle:#"Correct"
message:#"Bet you can't do that twice!"
delegate:self
cancelButtonTitle:#"Next Question"
otherButtonTitles: nil];
// use the alert tag to mark that answer was correct
alert.tag = 1;
} else
{
// answer is incorrect
alert = [[UIAlertView alloc]
initWithTitle:#"Wrong!"
message:#"Sorry, try again..."
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: nil];
}
// show and release the alert
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
[self generate];
answer.text = #"";
}
}
Store that value and then display it in an alert view.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message#"It took you %# seconds.",time delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
Let me know if the ,time works. I haven't tested this yet, but it should work.

Resources