UIAlertView Question - iphone

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

Related

Alert view is working in one class, but not the other

I have a class name viewController and it has the following code below and it works fine. However, when I call check from my subclass controller it doesn't work the way I want it to.The UIAlertView shows up, but it isn't able to detect when button index 0 is touched.Any workaround for this.
-(void)check{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose!"
message:#"Play Again?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"OK", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { // and they clicked OK.
ViewController*myNewVC = [[ViewController alloc] init];
[self presentModalViewController:myNewVC animated:NO];
}
}
Add UIAlertViewDelegate in your subclass .h file like bellow..
#interface yourSubClassViewController :UIViewController <UIAlertViewDelegate>{
/// your code
}
#end
The Cancel button will be the index "0", Try for index==1.
Use this method:
-(void)check{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose!"
message:#"Play Again?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"PopUp", nil];
[alert show];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog("Cancel Button clicked);
}
else if (buttonIndex == 1) {
ViewController*myNewVC = [[ViewController alloc] init];
[self presentModalViewController:myNewVC animated:NO];
}
Don't use cancel button. Use two buttons otherButtonTitles and use the buttons index == 1 and button index == 2 . That may help.
You need to set the delegate to your subclass.
-(void)check:(id)delegate{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose!"
message:#"Play Again?"
delegate:delegate
cancelButtonTitle:#"OK"
otherButtonTitles:#"OK", nil];
[alert show];
}
and you call it [self check:self];

How to dismiss alertview when click on the button in that alertview

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.

UIAlertView not responding to UIAlertViewDelegate

I'm using logos for iPhone (MobileSubstrate addons), with a .h file for my
#interface MyClass : NSObject <UIAlertViewDelegate>
and the
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
is in the .m, but nothing is working, when tapping the buttons on the alert, it doesn't invoke what I have set for each buttonIndex.
Thanks.
Edit: Here's what I've got;
#import "Tweak.h"
%hook ASApplicationPageHeaderView
- (void)_showPurchaseConfirmation {
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"title"];
[alert setMessage:#"message"];
[alert setDelegate:self];
[alert addButtonWithTitle:#"button 1"];
[alert addButtonWithTitle:#"continue"];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { //also tried (UIAlertView *)alertView
UIAlertView *lol = [[UIAlertView alloc] init];
[lol setTitle:#"button 1"];
[lol setMessage:#"button 1"];
[lol setDelegate:self];
[lol addButtonWithTitle:#"lol"];
[lol show];
[lol release];
} else {
%orig;
}
}
%end
You'll most likely need to register your class as the delegate at some point using something along the lines of:
[yourAlertViewObject setDelegate:self];
As the UIAlertViewDelegate Protocol Reference docs say (emphasis mine):
If you add your own buttons or
customize the behavior of an alert
view, implement a delegate conforming
to this protocol to handle the
corresponding delegate messages. Use
the delegate property of an alert view
to specify one of your application
objects as the delegate.
Define your alert within that class and declare the alert delegate to self hope it start working to you
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert View "
"
message:#"Would you like to do something?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Button1", #"Button2", nil];
[alert show];
[alert release];
You just need to put %new in front of the alertView delegate:
%new
-(void) alertView:...

Detecting which UIAlertView was clicked

i need to pop up alert when my application loaded... I called it didfinished launching..
after clicking ok button need to show another alert message i use clickedButtonAtIndex...
Now when I clicked the ok button its calling again and again.. the alertview..
I need to call only once... what to do?
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
viewControllersList = [[NSMutableArray alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Alow this app to use your GPS location"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
NSLog(#"NO");
}
else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
}
#thanks in advance.
set delegate:nil in second alertView
I mean
if (buttonIndex==0) { NSLog(#"NO"); } else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:nil cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
Define each UIAlertView and in the delegate look for which Alert to respond to:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert1) {
if (buttonIndex==0) {
NSLog(#"NO");
} else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages." delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
} else {
/* the second alertview using the same buttonIndex */
}
}
You can also add a tag to your AlertView and te check the tag later on the clickedButtonAtIndex method
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:#"" message:#"All local datawill be erased. Erase local data?" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Erase", #"Cancel",nil];
alert.tag =123; // added the tag so we can prevent other message boxes ok button to mix up
[alert show];
then
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex ==0 && alertView.tag==123){
// Do what ever you wish
}
}
If what you want is to receive the location, just request it and the system will automatically show the message for you. Follow this example:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:#"Location Services Disabled" message:#"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
[manager release];
}
http://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_AppDelegate_m.html%23//apple_ref/doc/uid/DTS40007801-Classes_AppDelegate_m-DontLinkElementID_4
Same for push notifications.
though u got many implementation solutions already ...but i think better practice would be to assign a tag to each of your AlertView and before detecting the button tapped check the tag of invoking AlertView.Hope this helps.
#Gerard:Though both location manager and push notification service raise system generated messages ,these can be checked off by users not to be shown again.So better HIG complaint method is application generating message whenever push or location manager are required.

UIAlert View-for yes/no condition

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