The iOS app crashed right after clicking on a UIAlertview [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The iOS app crashed right after clicking the button on a UIAlertview
I tried to dial a number with the phone app after the user click on a button on a UIAlertview. The phone app did open, but the original app crashed right after clicking the button on the UIAlertview. Does anyone one know the reason? I did try to make sure I released everything that should be released. The error is 0x3beb85b0: ldr r3, [r4, #8] EXC_BAD_ACCESS (code=1, address=0x7269634f) Any help would be appreciated. Thanks! Below is the code:
-(IBAction)dialButtonPressed:(UIButton *)numberButton
{
if ([company isEqualToString:#"Not Found"]==true){
message = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"No replace number found. Would you like to dial anyway?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
message.tag = 1;
if(phoneLinkString)
{
[phoneLinkString release];
phoneLinkString = nil;
}
[message show];
[message autorelease];
phoneLinkString = [[NSString stringWithFormat:#"tel:%#",replace]retain];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(message.tag == 1 && buttonIndex == 1){
NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
[[UIApplication sharedApplication] openURL:phoneLinkURL];
message = nil;
}
}
- (void)dealloc {
[phoneNumberString release];
[phoneNumberLabel release];
[super dealloc];
}

Could you try didDismissWithButtonIndex instead of clickedButtonAtIndex. I am guessing let me know.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

Related

Prevent from UIAlertView to repeat when click on tableViewCell

I'm kinda stuck with a stupid problem, in my app when a user press on a TableViewCell he can change some value in the server, before i am sending the information to server i want to show UIAlertView to the user to make sure he wants to do that.
so in didSelectRowAtIndexPath:
i wrote this
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Caution!" message:#"bla bla " delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
then its calls the UIAlertView method clickedButtonAtIndex:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSString *url = #"some url";
NSDictionary *paramerter = #{#"parameter":parameter};
[self.jsonHandler startParseWithParameters:paramerter andUrlAdress:url withCompletion:^(int errValue) {
if (errValue == 1) {
NSLog(#"ERROR");
}else if (errValue == 0){
[self AlertViewWithTitle:#"success" andMessage:#"success"];
}
}];
}
}
the problem is that the if (buttonIndex == 0) called for every cell and change them all!
and i get UIAlertView message for every cell in the tableView.
how can i prevent it?
EDIT
Ok the problem caused because i use the same buttonIndex for both UIAlertViews
i solved it using this:
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"buttonString"]){
//Do what ever you need
}
hope someone find it helpful.

Basic use of UIAlertView causes EXC_BAD_ACCESS

I'm implementing a very simple iOS application just to practice showing a pop up alert in, and I get an error when I press the alert button:
Thread 1:EXC_BAD_ACCESS(code=1,address=0x676f6f57)
This is the code:
- (IBAction)AlertButton {
alert = [[UIAlertView alloc]
initWithTitle:#"Alert" message:#"Alert"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"Apple", "Google" ,nil];
[alert show];}
-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"http://apple.com"]];
}
if(buttonIndex == 2){
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"http://google.com"]];
}}
the problem is with the constructor of the UIAlertView, in the line:
otherButtonTitles:#"Apple", "Google" ,nil];
your forget the # before "Google". And finally change:
-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{
by
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
The real problem is you miss an # in front of "Google", so it is not an NSString and hence the crash.
use this
.h
no need for IBOutlet.just
UIAlertView *alert;
.m
alert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:#"Alert"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"Apple", #"Google", nil
];

iOS EXC_BAD_ACCESS error in class

I'v encountered EXC_BAD_ACCESS error in my app. Or to be more specific in one of my classes. It is Custom UIAlertView class. I couldn't catch when it throws EXC_BAD_ACCESS in usage. Sometimes it works great just as expected, and in all suden it craches... Here is whole class
#implementation AlertPassword
int counter = 3;
#synthesize done;
#synthesize alertText;
#synthesize msg;
- (void) showAlert :(NSString*) title
{
if(counter != 3){
if(counter == 1)
{
NSString *msgs = #"Last warning";
msg = msgs;
}
else
{
NSString *msgs = [NSString stringWithFormat:#"WRONG PIN. %d times remaining",counter];
msg = msgs;
}
}
else
{
NSString *msgs = #"Enter your pin";
msg = msgs;
}
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Security" message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil];
_alert = alert;
_alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
alertText = [_alert textFieldAtIndex:0];
alertText.keyboardType = UIKeyboardTypeNumberPad;
alertText.placeholder = #"Pin";
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(controlTextDidChange:)
name:UITextFieldTextDidChangeNotification object:alertText];
[_alert show];
[_alert release];
[[NSNotificationCenter defaultCenter] removeObserver:UITextFieldTextDidChangeNotification];
}
- (void)controlTextDidChange:(NSNotification *)notification {
{
NSString *pin = [[NSUserDefaults standardUserDefaults] stringForKey:#"Pin"];
if ([notification object] == alertText)
{
if (alertText.text.length == pin.length)
{
if(counter != 0)
{
if([alertText.text isEqualToString:pin])
{
[_alert dismissWithClickedButtonIndex:0 animated:NO];
[self.tableViewController openSettings];
counter = 3;
}
else
{
counter--;
[_alert dismissWithClickedButtonIndex:0 animated:NO];
[self showAlert:#""];
}
}
else
{
[_alert dismissWithClickedButtonIndex:0 animated:NO];
[[NSUserDefaults standardUserDefaults] setObject:NULL
forKey:#"Telephone"];
[[NSUserDefaults standardUserDefaults] setObject:NULL
forKey:#"Pin"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"" message:AMLocalizedString(#"EraseData", nil) delegate:nil cancelButtonTitle:AMLocalizedString(#"Ok", nil) otherButtonTitles:nil];
counter = 3;
[av show];
[av release];
}
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *pincheck = [[NSUserDefaults standardUserDefaults] stringForKey:#"pinCheck"];
if (buttonIndex == 0)
{
if(pincheck.intValue == 1)
{
NSLog(#"app kill");
exit(0);
}
else
{
NSLog(#"dismiss");
}
}
}
#end
Here is where i initialize and use this class.
case 5:
NSLog(#"Add remove");
if (pincheck != NULL && pin != NULL){
if([pincheck isEqualToString:#"0"])
{
AlertPassword *alert = [AlertPassword alloc];
alert.tableViewController = self;
NSString *msg = #"Enter your pin code to access:";
[alert showAlert:msg];
// [alert release];
}
break;
}
else
{
NSLog(#"Is null");
[Menu load2View:self];
}
break;
I though maybe it was because i do not release alert. But adding [alert release]; Made to have EXC_BAD_ACCESS directly after the user tries to enter something. Without [alert release]; it works. But sometimes it craches with EXC_BAD_ACCESS
Also sometimes it gets
2012-11-08 12:11:27.451 kodinisRaktas[2485:19d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ dismissWithClickedButtonIndex:animated:]: unrecognized selector sent to instance 0x947aae0'
But I also have no idea why this happends
Please help, I'm pretty new to objective-c and ios, and I have no idea how to get rid of this, I guess someone with a bit of experience will see whats wrong in my code.
I'v just saw, that EXC_BAD_ACCESS or unrecognized selector throws if you push cancel for 4-5 times or more, and then try to type something.
EXC_BAD_ACCESS is mostly due to bad memory handling. The alert has most likely become an zombie... I would have the alert as a property with strong/retain. You should hold on to it while displaying. Not release after "show".
When you set up the first you can do like this
_alert = [[UIAlertView alloc] initWithTitle:#"Security" message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil]; // retain count: 1
Note calling "show" will also retain it, but that does not change the fact that you need to too.
[_alert show]; // retain count: 2
Wait for the delegate callback and release it.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[_alert release], _alert = nil; // retain count in next run will be 0
}
Tip: It may be easier to handle if you use UIAlertView combined with blocks. http://gkoreman.com/blog/2011/02/15/uialertview-with-blocks/
I don't see where you initialize alert, anyway I suppose it's a class field, so retain it.If it's not a class instance variable, make it be so, this way you will always have a pointer to it.
[__NSMallocBlock__ dismissWithClickedButtonIndex:animated:]
You are sending this message to a raw malloc block, so probably alert has been released and points to a memory used for something else, something that's not an objc object.
Try to retain alert and see what happens.
May be you are passing wrong values to this "dismissWithClickedButtonIndex:animated:" method which is not recognizing the value signature please do a double check for that;
the excepted answer does make sense and is likely the cause
but what is this?
[NSNotificationCenter defaultCenter] removeObserver:UITextFieldTextDidChangeNotification];

The iOS app crashed right after clicking the button on a UIAlertview

I tried to dial a number with the phone app after the user click on a button on a UIAlertview. The phone app did open, but the original app crashed right after clicking the button on the UIAlertview. Does anyone one know the reason? I did try to make sure I released everything that should be released. Thanks! Below is the code:
-(IBAction)dialButtonPressed:(UIButton *)numberButton
{
if ([company isEqualToString:#"Not Found"]==true){
message = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"No replace number found. Would you like to dial anyway?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
message.tag = 0;
if(phoneLinkString)
{
[phoneLinkString release];
phoneLinkString = nil;
}
[message show];
[message autorelease];
phoneLinkString = [[NSString stringWithFormat:#"tel:%#",replace]retain];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
[self release];
message = nil;
if(message.tag == 0 && buttonIndex == 1){
NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
[[UIApplication sharedApplication] openURL:phoneLinkURL];
}
- (void)dealloc {
[phoneNumberString release];
[phoneNumberLabel release];
[self release];
[message release];
[super dealloc];
}
The newest code
-(IBAction)dialButtonPressed:(UIButton *)numberButton
{
if ([company isEqualToString:#"Not Found"]==true){
message = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"No replace number found. Would you like to dial anyway?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
message.tag = 1;
if(phoneLinkString)
{
[phoneLinkString release];
phoneLinkString = nil;
}
[message show];
[message autorelease];
phoneLinkString = [[NSString stringWithFormat:#"tel:%#",replace]retain];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(message.tag == 1 && buttonIndex == 1){
NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
[[UIApplication sharedApplication] openURL:phoneLinkURL];
message = nil;
}
}
- (void)dealloc {
[phoneNumberString release];
[phoneNumberLabel release];
[super dealloc];
}
but it still crashed after clicking the button on the UIAlertview. The error is 0x3beb85b0: ldr r3, [r4, #8] EXC_BAD_ACCESS (code=1, address=0x7269634f) Any help would be appreciated. Thanks!
The problem might be that you set your alert to nil before your if statement. Try putting it after.
The crash is happening due to this code. [self release];.
When you call self release the view in which the alert is displayed will released and deallocated, not the alertView. That's the cause of crash.
You are already releasing the alertViews memory in the dialButtonPressed: method using [message autorelease];
So no need to release the alertView again in clickedButtonAtIndex. So change the method like:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if(alertView.tag == 0 && buttonIndex == 1)
{
NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
[[UIApplication sharedApplication] openURL:phoneLinkURL];
}
message = nil;
}
Your crash is being caused by poor memory management. The primary issue is calling [self release]. It's a pretty rare case that this is appropriate.
Another issue is your attempt to check the message.tag right after setting message to nil. Calling the tag property on a nil object will always result in a value of 0.
Your dealloc method is all wrong. Don't call [self release]. Don't call [message release] since you autoreleased it when you showed it.
BTW - never use a tag of 0. This is the default. If you want to use the tag, always use a non-zero value so you can distinguish the value from the default.

FaceBook UIAlertView issues

I used Facebook SDK on my app. After i pressed "Add feed to your wall", the alert pops up with "YES" or "NO". If i choose "YES" the FBStreamDialoy just flash away.
First , click the button " Add feed to your wall" to call changeFeed: function:
-(IBAction) changeFeed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sporting Summer Festival Monte-Carlo" message:#"Are you attending this concert?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alert show];
alert.tag = 1;
self.alertView =alert;
[alert release];
}
Then, press "YES" button. Call this function:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(#"NO");
}
else
{
NSLog(#"YES");
[self showAddFeed];
}
}
And this is the showAddFeed function , which is defined in front of clickButtonAtIndex.
-(void)showAddFeed
{
FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.delegate= self;
dialog.userMessagePrompt = #"*****";
[dialog show];
}
Just cant work well. I don't know why? Thank you for your help.
------------Answer is--------------------------
[self performSelector:#selector(fbButtonClick) withObject:nil afterDelay:0.10];
Call it after a delay of 0.1 sec [self performSelector:#selector(fbButtonClick) withObject:nil afterDelay:0.10];
The problem is that you listen to:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Which should be:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Because at the clickedButtonAtIndex function the view isn't closed and this can cause problems.
Edit:
To make it work in iOS 4 add your facebook login call to a method and call the following function in the didDismissWithButtonIndex:
[self performSelectorOnMainThread:#selector(facebookOpenAction) withObject:nil waitUntilDone:NO];