How to Clear UITextfield Text and Ok button in UIAlertView in IOS? - ios

I Created Password Validation. Password created successfully it will show one UIAlertView and press when user presses the OK, the Password Text field will get clear. How to do that? Please any Example?

Add UIAlertViewDelegate protocol:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
Show Alert View using :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE_HERE"
message:#"ALERT_MESSAGE HERE."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
After user presses OK, this method will called automatically:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
// do something here...
yourTextField.text = nil;
}
}

Your question is not clear. If you want the password field text to be empty after displaying the UIAlert, please add the following code to make your password filed empty.
yourTextField.text = ""
Here's an example.
if(passwordValid==YES)
{
UIAlertView *passwordValidated = [[UIAlertView alloc] initWithTitle:#"Password Validated!"
message:#"Your password is successfully validated. Press OK to continue"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
yourTextField.text = "";
[updateMessage show];

Related

textFieldIndex (0) is outside of the bounds of the array of text fields

I have searched far and wide for an answer but I haven't found anything close.
The following code:
UIAlertView *welcome = [[UIAlertView alloc] initWithTitle:#"Welcome"
message:#"Please Enter Your Credentials to Proceed"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[welcome show];
Gives the following error:
textFieldIndex (0) is outside of the bounds of the array of text fields
If I change the alertView type to UIAlertViewStylePlainTextInput then it works and I cant figure out why!
Any help would be appreciated.
Somewhere you are calling -textFieldAtIndex: on an alertView object that does not or is not meant to have a textField.
Check for things like:
[welcome textFieldAtIndex:0];
or...
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//...
[alertView textFieldAtIndex:0];
//...
}
This could be if you're using multiple alertView objects and haven't handled done case-handling to bifurcate the implementation for an alertView object that has a textField and another alertView that doesn't have a textField

UIAlert and push segues

I have an IF statement that checks labels to see if a label is empty, if it is show an alert.
if ([_DOBDate.text length] == 0)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Cannot Proceed"
message:#"Please Submit your DOB and Gender"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
later on in the function I have perform segue, like this :
[self performSegueWithIdentifier:#"SetUptoMain" sender:self];
True, the alert does fire when the label is blank, but it also performs the segue. This perform segue line is not in the IF statement. So I would have thought it would have ran the IF statement and stayed there till I pressed OK. OK would be staying on the same view controller.
The segue is performed, is this due to Blocks ? any advice ?
So if the USER pressed Ok from the UIAlert the VC does not move, it stays where it was so the user can enter the details required.
This is my code :
- (IBAction)SettingsSave:(id)sender {
if ([_DOBDate.text length] == 0)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Cannot Proceed"
message:#"Please Submit your DOB and Gender"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
more code...
then at the end
[self performSegueWithIdentifier:#"SetUptoMain" sender:self];
}
thanks
Seems to be an if/else logic issue. Read about it.
-(IBAction)yourAction:(id)sender
{
if ([_DOBDate.text length] == 0)
{
//Show your AlertView as you did
}
else
{
//put the rest of your code, including the performSegue
}
}
UIAlertView doesn't block execution. It runs asynchronously. Therefore the code path will go into your if statement, then continue past it.
If you only want the segue to be performed after the user presses the alert view button then you need to implement UIAlertViewDelegate.
In your header file add something like this:
#interface MyController : UIViewController <UIAlertViewDelegate>
When you create the alert view do it like this:
UIAlertView *message = [[UIAlertView alloc] initWithTitle: #"Cannot Proceed"
message: #"Please Submit your DOB and Gender"
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[message show];
And add this method to implement the UIAlertViewDelegate.
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex {
// perform segue here. You can also check what button was pressed based on the button index.
}

UIAlertView button responds to "Return" key

I have a UIAlertView with UIAlertViewStyleSecureTextInput. When the user taps "Login" and the password is correct, it will push the next ViewController. If the password is incorrect, it displays another UIAlertView prompting a single "Dismiss" button. What I am trying to do is when the user taps the return key, the "Login" button will be triggered. As it sits now, when the return key is pressed the alert just dismisses, regardless if the password is correct or not. Maybe there is a more logical solution than what I have attempted? Sorry if the title is a little confusing, I don't know how else to explain it. Any help is greatly appreciated. Thanks.
I have declared the first alert in my .h file, and conformed to the UIAlertViewDelegate and UITextFieldDelegate:
#interface EndViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate>
#property (retain, strong) UIAlertView *loginRequiredMsg;
Method for the "Login" alert:
- (IBAction)resultsBtnPressed:(UIButton *)sender {
self.loginRequiredMsg = [[UIAlertView alloc]initWithTitle:#"Login Required"
message:#"Please enter the admin password"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Login", nil];
self.loginRequiredMsg.alertViewStyle = UIAlertViewStyleSecureTextInput;
[self.loginRequiredMsg textFieldAtIndex:0].delegate = self;
[self.loginRequiredMsg show];
}
Method for dismissing the keyboard when return key is tapped (I think I need to somehow call the next alert here if the password is incorrect, as currently it just dismisses the alert):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Dismiss keyboard when return key is pressed
[self.loginRequiredMsg dismissWithClickedButtonIndex:self.loginRequiredMsg.firstOtherButtonIndex animated:YES];
return YES;
}
And finally the method containing the outcome of the password entered:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Login"]) {
UITextField *password = [alertView textFieldAtIndex:0];
// Basic login authentication
if ([password.text isEqualToString:#"admin"]) {
// Allocate & initialise ViewController
ResultsViewController *Results = [[ResultsViewController alloc]initWithNibName:#"ResultsViewController" bundle:nil];
// Push next ViewController
[self.navigationController pushViewController:Results animated:YES];
NSLog(#"Show results");
} else {
UIAlertView *errorMsg = [[UIAlertView alloc]initWithTitle:#"Error"
message:#"Admin password is incorrect. Please try again."
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[errorMsg show];
}
}
}
As discussed in the comments, you need to implement alertView:didDismissWithButtonIndex: which tells you when the alert view has been dismissed. When you tap the return key, it calls that method with the index of the OK button.

UIAlertView dismissal in utility class

In my application i want alertview in many views.So what i did is just wrote a single alertview in a utility class and use it everywhere.This is working fine.
I even tried by setting <UIAlertViewDelegate> but in vain.
Utility Class
#interface SSUtility: NSObject<UIAlertViewDelegate> {
}
+(void)showAllert;
#end
#implementation SSUtility
+(void)showAllert{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"gotoappAppstore",#"") message:#"" delegate:self cancelButtonTitle:NSLocalizedString(#"Ok",#"") otherButtonTitles:nil];
[alert show];
[alert release];
}
#end
Now from my view
-(void)pressButton{
[SSutility showAllert]
}
Now i want to give a button action for alert view click and call a method on that button action.
So im stuck with,in which class i want to implement this method.I tried it in utility class and viewc controller but the method is not getting triggered when "ok" button is pressed.
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
Can anyone please help me on this?
Thanks in advance.
You wire the alert view button response method by setting your alert view object delegate usually to the owner object and implementing the – alertView:clickedButtonAtIndex: method.
You need 4 parts in your code:
instantiate your UIAlertView object
send show message to your UIAlertView object
set delegate
implement the delegate method
Example:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"myTitle" message:#"myMessage" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitle:#"Another button"];
[myAlertView setDelegate:self];
[myAlertView show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) //index 0 is cancel, I believe
{
// code for handling cancel tap in your alert view
}
else if (buttonIndex == 1)
{
// code for handling button with index 1
}
}
I would recommend you get more familiar with how delegates work. This'll come back again a lot.
You set delegate:nil in your UIAlertView's init.
You should set to delegate:self, like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"gotoappAppstore",#"") message:#"" delegate:self cancelButtonTitle:NSLocalizedString(#"Ok",#"") otherButtonTitles:nil];
in order to use the delegate in the same class (a.k.a. self).
As a sidenote, if you use Automatic Reference Counting (ARC), you do not need [alert release] (your Xcode compiler should warn you about this)

Prompt for user input and store the data

I just started iOS development, and now I'm stuck. I have a table cell, where the user cann see data. Next to it, there are two buttons. One to delete, one to edit. The edit part is where I'm stuck. I have a label field. When the user clicks on the edit button a prompt should come where the user can type in data. I already found something to do it. But there's no save button and I don't know how I could then save the variable to an NSString
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Name der neuen Liste" message:#"" delegate:self cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
I currently have this. lNow a prompt pops up, where I can write something, but not save. I just can press cancel afterwards. How do I work with user input in iOS? I don't want to use the label as an UITextField since it has to be clickable to perform another action.
Try this:
- (void)noteInput
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"input"
message:#"input"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *noteText = [alertView textFieldAtIndex:0];
NSString *note = noteText.text;
}
}
Try adding a Save button in otherButtonTitles. Then, in your UIAlertViewDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Save button pressed
}
}

Resources