my app needs alert msg and with yes button click another alert msg which decides the final action.
I have used - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
this method.
Please help me.
Do what #Adrian Pirvulescu said but before showing the alert do alert.tag = 1; and then when - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex is called do:
if (alertView.tag == 1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"2nd Alert"
message:#"My message" delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.tag = 2;
[alert show];
[alert release];
}
else if (alertView.tag == 2) {
[self CallSomeMethod];
}
else {
//do what ever you want here
}
Check this out
http://www.timeister.com/2010/06/objc-show-alert-iphone/
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"UIAlertView"
message:#"My message" delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
Related
Below is the scenario.
I have created a UIAlertView in viewDidLoad of the Controller in which I display the Alert.
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
I call it as [alert show] and it is displayed.
When user press Ok, it goes to textEndEditing delegate method and from if nil/worng value was entered, I call [alert show] again.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text length] > 0)
{
}
else
{
NSLog(#"Checking");
[loginAlert show];
}
}
But its not displayed again. Please tell me what to do?
Instead of delegate:nil, put delegate:self
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
First change your alert view delegate to self.
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[loginAlert show];
Then in the alert view delegate method check the length of the text and show the the alert again if empty.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([[loginAlert textFieldAtIndex:0].text length] > 0)
{
}
else
{
NSLog(#"Checking");
[loginAlert show];
}
}
}
How about making it simple .. ?
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text length] > 0)
{
}
else
{
NSLog(#"Checking");
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[loginAlert show];
}
}
Try this:
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
[yourTextField resignFirstResponder];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField==yourTextField) {
if ([yourTextField.text isEqualToString:#""] || yourTextField.text == nil) {
[yourAlert show];
}
}
}
Also see this for your problem:
Keep UIAlertView displayed
uialertview called more than once
Set delegate self instead of nil and implement my code.
loginAlert = [[UIAlertView alloc] initWithTitle:#"Check"
message:#"Ok"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
[self.view endEditing:YES];
}
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
{
// Your code write in this method and show your alert
}
else
{
[loginalert show];
}
}
i hope this code useful for you.
I am new in iPhone developer,
I want to implement 2 alert view one after another, like when user press delete button, 1st alert view will ask Are you sure want to Delete ? with two buttons yes and no
Now, if user presses yes , then 2nd alert view will come with message Deleted Successfully ! this alert view contains only OK button, now on click of this OK button i want to call one method.
and if user presses No then nothing should happen and alert should dismiss.
Here is my code snippet,
-(void)DeletebtnCliked:(id)sender
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Are you sure want to delete ?"
message:nil delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Yes",#"No",nil];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:#"Deleted Successfully !"
message:nil delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertew show];
[alertew release];
if (buttonIndex == 0)
{
[self MethodCall];
}
}
else if (buttonIndex == 1)
{
[alertView dismissWithClickedButtonIndex:1 animated:TRUE];
}
}
after writing this code i am inside Infinite loop.
Any help will be appreciated.
alertView.tag = 1;
alertew.tag = 2;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 2)
{
//Do something
}
else
{
//Do something else
}
}
Set the second alert view's delegate to nil:
UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:#"Deleted Successfully !"
message:nil delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil];
Either use tags to tackle the situation like following or simply just set Delegate nil for the inner alertView which is inside the delegate methode so that it will never call.
-(void)DeletebtnCliked:(id)sender
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Are you sure want to delete ?"
message:nil delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Yes",#"No",nil];
alertView.tag = 1;
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0 && alertView.tag == 1)
{
UIAlertView* innerAlert = [[UIAlertView alloc] initWithTitle:#"Deleted Successfully !"
message:nil delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
innerAlert.tag = 2;
[innerAlert show];
[innerAlert release];
if (buttonIndex == 0 && alertView.tag == 1)
{
[self MethodCall];
}
}
else if (buttonIndex == 1 && alertView.tag == 1)
{
[alertView dismissWithClickedButtonIndex:1 animated:TRUE];
}
}
try this:-
-(void)button:(UIButton *)buttonDelete{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"delete" message:#"Do you Want to delete" delegate:self cancelButtonTitle:#"Cancle" otherButtonTitles:#"Yes", nil];
alertView.delegate = self;
alertView.tag = 2000;
[alertView show];
}
-(void)buttonUpdate:(UIButton *)buttonEdit{
UIAlertView *alertView2 = [[UIAlertView alloc] initWithTitle:#"Update" message:#"Do you Want to Update" delegate:self cancelButtonTitle:#"Cancle" otherButtonTitles:#"Yes", nil];
alertView2.delegate = self;
alertView2.tag = 20001;
[alertView show];
}
-(void)buttonAdd:(UIButton *)buttonAdd{
UIAlertView *alertView3 = [[UIAlertView alloc] initWithTitle:#"Add" message:#"Do you Want to Add" delegate:self cancelButtonTitle:#"Cancle" otherButtonTitles:#"Yes", nil];
alertView3.delegate = self;
alertView3.tag = 20002;
[alertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 2000){
if (buttonIndex==0) {
NSLog(#"Delete Cancel Button click");
}
else{
NSLog(#"Delete yes Button is click");
}
}
if (alertView.tag == 20001){
if (buttonIndex==0) {
NSLog(#"update Cancel Button click");
}
else{
NSLog(#"update yes Button is click");
}
}
if (alertView.tag == 20002){
if (buttonIndex==0) {
NSLog(#"Add Cancel Button click");
}
else{
NSLog(#"Add yes Button is click");
}
}
}
I am new to iOS.
I am working on alertviews. Here is my code. Here there are 2 alertviews: successfulallert and unsuccessfulallert for login page. I am using alertview delegate also here, it will work for both alertviews but I want to work only for successful alertview and navigation should be done only for successful alertview. If anybody knows this please help me.
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
}
Why don't you put "OK" as cancelButtonTitle? Everything will be handled automatically.
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[successfulAllert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//POP here with this:
[self.navigationController pushViewController:addItemView animated:NO];
}
}
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 2)
{
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
else
{
//[[self navigationController]pushViewController:registerUserScreen animated:NO];
// OR
return;
}
}
You have many ways to correct your code, the first and very common is to use the tag property (integer) of the UIView. Since UIAlertview inherits from UIView, it has the tag property, so each time you want create an alert (or a view), set the tag and the check your condition like:
...
alert.tag=1;
[alert show];
then to know wich alert is calling the callback:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==theTagOfYourAlert){
//do your stuff
}
}
another way, in your case, could be:
if([alertView.title isEqualToString:#"Alert"]){
//do your stuff
}
}
Add tag to the two alert views and check for tag in alert view delegate.
Sample code:
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==2 && buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
Yes the delegate would work for both the alertviews but you can assign a tag to each alertview Object and check for the tag in the delegate and then perform event if the tag for that particular AlertView onject matches.If u need code , i will provide.
For things like Login status updates, you might want to have the "Login Successful" message disappear automatically. Try this instead:
https://github.com/camclendenin/flashbox
This works nicely and comes in handy for situations like this. Plus you don't have to deal with all the clutter involved with UIAlertViews.
In delegate method ofUIalerview I am trying to show another alert after finishing the process on clicking button index. Is it possible to do so? Also I want to call a method on button click of alert. How can I do that?
I am trying in this way. Is is correct?
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
You want to use tags for your UIAlertViews
Assigning tags
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Do something first" message:#"This is the first UIAlertView" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[firstAlert setTag: 0];
[firstAlert show];
[firstAlert release];
Handling those tags
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (alertView.tag)
{
case 0: /* firstAlert */
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchaseDone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchaseDone setTag: 1];
[purchaseDone show];
[purchaseDone release];
}
}
break;
case 1: /* purchaseDone */
{
/* purchaseDone uialertview was triggered, handle it here. */
}
break;
}
}
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"a" message:#"b" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"ca",nil];
[a show];
[a release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = 55;
[purchasedone show];
[purchasedone release];
}
if([alertView tag] == 55 && buttonIndex == 0)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"New Alert" message:#"New Alert Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
write UIAlertViewDelegate in .h file.
put the following line on top of your .m file
`#define PREVIOUS_ALERT 101`
`#define NEW_ALERT 102`
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//you need to make a condition here for checking the alert else it will go into infinite loom of showing alert after alert
//you can do it by setting tag to alert something like this
if(alertView.tag == PREVIOUS_ALERT) //sot the previous alert tag to this where you created it
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = NEW_ALERT;
[purchasedone show];
[purchasedone release];
}
}
else
{
//code for your second alert
}
}
I am trying to act upon whichever button is pressed on an alert. I have the following code and the first alert pop's up but it never gets to the second one.
I have set it up so that the UIAlertViewProtocol is defined in the header too.
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex != [actionSheet cancelButtonIndex])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something actually happened" message:#"Something was done" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"test",nil];
[alert show];
}
}
-(void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==0)
{
NSLog(#"tetetete");
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[a show];
[a release];
[alert release];
}
}
The simplest explanation is that the delegate is not set properly. Set the debugger to
if(buttonIndex ==0) to make sure the delegate method is being called. Alternatively, the button index might not be zero so the second alert is never created. The debugger can check that as well.
You should move the line...
[alert release];
... to the first method.
I've never tried to daisy chain alerts like this. It's theoretically possible that since alerts are modal and attached to the window and not the top view, that you can't add a second alert until the first one has been completely removed from the window. If the window merely releases the alert it might persist in a property of the window if the originating object has not yet released it. Having the view retained until after the second view is shown might cause a collision of some sort in the window object.
I have modified your code check it
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex != [actionSheet cancelButtonIndex])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something actually happened" message:#"Something was done" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"test",nil];
[alert show];
[alert release];
}
}
-(void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==0)
{
NSLog(#"tetetete");
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[a show];
[a release];
[a release];
}
}