clickedButtonAtIndex in appdelegate is not called - iphone

I am calling UIAlert with 2 buttons "Cancel" and "OK" in MyapplicationAppDelegate.m file , the alert is called but on tap of "Cancel" or "OK" button
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
method is not called.
I have added UIAlertViewDelegate in the MyapplicationAppDelegate.h file as below
#import UIKit/UIKit.h
#interface MyapplicationAppDelegate: NSObject UIApplicationDelegate,UIAlertViewDelegate
{
..
}
I want to know what else is required.

I am not sure whether its your typo while posting the question but you should call the delegate within <.. , .. >
#interface MyapplicationAppDelegate: NSObject <UIApplicationDelegate,UIAlertViewDelegate> { .. }
Here is sample code for UIAlertView
UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:#""message:#"Your message goes here" delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil];
[myAlertView show];
[myAlertView release];
I am not sure myAlertView.delgate = self is the right method but i do set delegate with initWithTitle

For me,
#define appDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate)
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
message:#"Say Yes or NO?" delegate:appDelegate
cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
alertView.tag = 1;
[alertView show];
[alertView release];
do the trick!
Now its going into, -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; in AppDelegate file without adding,
#interface MyapplicationAppDelegate: NSObject <UIApplicationDelegate,UIAlertViewDelegate> { .. }

Related

EXC_Bad_Request "only" after calling the uialert 3 or more times

I am a newbie in objective c;
I am getting an error at creating a UIAlertview instantiation statements (UIAlert * myAlert, they are in separate scopes)
I have referred the following stackoverflow entries such as This, this, this too and much more research over the internet.
I am unable to get a break through
Following are my alert calls
This is my view controller code where I have put up the "UIAlertViewDelegate"
#import <UIKit/UIKit.h>
#interface gameFirstViewController : UIViewController<UIAlertViewDelegate>
#end
This is my class declaration
#import <Foundation/Foundation.h>
#interface GameLogic : UIView
//all sorts of various non relevant property declarations
#end
Here is my implementation for the alerts
//action to take when an alert is shown
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger) buttonIndex
{ NSLog(#"OK Tapped");
NSUserDefaults *MySettingsData = [NSUserDefaults standardUserDefaults];
row= [MySettingsData integerForKey:#"Row_count"];
col = [MySettingsData integerForKey:#"Column_count"];
if(buttonIndex==0)
{
for(int i=0;i<row;++i)
{
for(int j=0;j<col;++j)
{
myarr[i][j]=0;
}
}
if(_TimerStatus ==1)
{
[mainTimer invalidate];
mainTimer=nil;
_TimerStatus=0;
}
[self super_reset];
[self setNeedsDisplay];
NSLog(#"Game reset");
return;
}
}
//my usage of the alerts at 2 different places
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: #"GAME OVER"
message:#"You clicked on a mine, tap on ok to reset"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[myAlert performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:YES];
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: #"You Won! Whoo Hoo"
message:#"You have successfully dodged every minefield"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[myAlert performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:YES];
I am not sure where I am going wrong, any help would be great!
Thanks.
The way you are creating the UIAlertView is not safe in ARC. It's fine if you use the object right away, but you're passing it the performSelectorOnMainThread method. ARC may dealloc your myAlert variable by the time the main thread gets to perform the selector. That's likely why you're seeing the EXC_Bad_Request.
Either make myAlert a strong property, so it survives, or delay the creation of the UIAlert until the main thread is able to call the show method. You can do that like this:
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:#"GAME OVER" message:#"You clicked on a mine, tap on ok to reset" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
});
Note: I removed your double nil for the titles. I don't think that would make a difference, but just in case.

My alertview for the UIWebView doesn't work

Why doesn't this code work? All I have is this:
-(void)_webview:(UIWebView *)_webview didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"You have no internet connection!" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil, nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
exit(0);
}
It should work, right?
Because you mistyped the name of the UIWebViewDelegate method. You have
_webview:didFailLoadWithError:
whereas the real name of this method is
webView:didFailLoadWithError:
The delegate method is mistyped, as stated in the above response. Also, did you set the delegate of the UIWebView to the instance of the class that has those methods implemented?
for example, if it's a view controller, it could be in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
_webView.delegate = self;
}

UIAlertView crash by adding clickedButtonAtIndex

I create a class to call UIAlertview show on my screen. I write the UIAlert function in another class. Both these two classes are not my viewController class.
I use this UIAlert, which is a UITextfield inside, to store texts into a plist file.
here is the class to call UIAlert:
#import "Story.h"
#implementation Story
...
+ (void)stage1
{
AlertClass *pointer = [AlertClass new];
[pointer doAlert];
}
here is the class AlertClass.m file:
- (void)doAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
//this makes crash!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
self.storyFlow.text = [alertView textFieldAtIndex:0].text;
}
Before I add UIAlertViewDelegate in the .h and override the method "clickedButtonAtIndex", it works great. However, I need to store some data from the UITextfield inside the alert view. I get crash and don't know the message it responds as following.
Please help me to solve this problem. Thanks.
[crash pic] https://dl.dropbox.com/u/47381923/crash.tiff
do an NSLog on the text you get back from the Alert View to see whether that is the crash or the subsequent 'self.storyFlow.text = ' is causing it. Perhaps self.storyFlow has not been created yet (with alloc/init)

IPhone: How to call AlertView from a Static Function

I have a class as follows
#import "UtilAlert.h"
#implementation UtilAlert
+(void) showAlert:(NSString *)message andTitle:(NSString *)title andDelegate:(UIViewController *) delegate
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
#end
The problem is that when call the function with necessary parameters...
[UtilAlert showAlert:#"hello" andTitle:#"hello" andDelegate:self] ;
i get a error: Thread 1: stopped at break point 3;
for the function call from an UIController class
This is not an error. You have a breakpoint in your code, which is the little blue arrow in the left margin of your code. Click the blue arrow again to make it really light blue to turn off the breakpoint.
Also, you should release the alert after showing it before exiting the function or you will leak memory.
Just a wild guess, but your alert object may be released when the function exits if your are working on ARC. In that case, this might work :
+(void) showAlert:(NSString *)message andTitle:(NSString *)title andDelegate:(UIViewController *) delegate {
static UIAlertView *alert;
if(!alert)
alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}

UIAlertView. Using otherbutton as a openURL.

I want it once the user taps the 'otherbutton' it goes to my link.
But at the moment once the button is pressed it doesn't do anything.
Sorry im on my iPhone so I've used pastebin for the code
http://pastebin.com/fvgk87ih
Thanks alot
Conform your controller class to the UIAlertViewDelegate protocol.
#interface MyViewController : UIViewController <UIAlertViewDelegate>
Create the alert view with delegate set to self:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Follow" message:#"me on twitter" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
Respond to the delegate method
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]) {
// open url
}
}
The delegate of your UIAlertView must be set to the class (self in most cases) that's using it. Make sure your class calling the UIAlertView does conform to the UIAlertViewDelegate delegate.
You need to set the UIAlertView's delegate property to self instead of nil and implement the protocol in the header.
Header File:
#interface MyViewController : UIViewController <UIAlertViewDelegate>
In addition, check for the buttonIndex. Currently, (after performing the above), it will open the URL if any button is pressed.
- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
//open url
}
}