Exc_Bad_Access in alertview show - iphone

Getting Exc_Bad_Access in UIAlertView show message.
UIAlertView *systemAlert1 = [[UIAlertView alloc]initWithTitle:#"System message" message:#"note" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[systemAlert1 show]; **//Crashing on this line [EXC_BAD_ACCESS]**
[systemAlert1 release];
Why i am getting this?? Please help

ANY UI stuff including the showing of alerts should be done on Main thread.
If you are doing this on some other thread, it will definitely crash.

It may be because your alert gets invoked from your background thread and not the main thread. It is recommended that user interface related changes should only be made on Main Thread to avoid this kind of behaviors of the application
Try this code:
UIAlertView *systemAlert1 = [[UIAlertView alloc]initWithTitle:#"System message" message:#"note" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[systemAlert1 performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
[systemAlert1 release];
Hope this helps. Let me know if you need anything else.

Related

UIAlertView after completion of NSLog

I am new to iphone..
Actually i wanted to know is it possible to put an UIAlertView after the completion of NSLog as i wanted to check whether the statement really works or not as i cant check NSLog in the iphone.
Please suggest me whether it is possible and how?
NSLog will still work on iPhone..
You can only see it in the console if you have attached the device to your mac..and then use the app.
Anyways.
You can show your AlertView after log like this.( This Nslog is an example..replace with what is your actual log)
NSLog(#" Image Loaded");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log" message:#"Image Loaded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
[alert release];
If the NSLog statement works, it'll show up in the XCode console. So if you debug the app on your phone via XCode, you should see the NSLog. Or did you have something else on your mind ?

How to exit the app after an error in user friendly manner?

I need to protect my code against possible errors. If they arise then there's no point to run the app further, so I need to bring to the user some message and then exit the app. So, I'm checking the conditions and then bringing alert:
if (someError){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"No database file exist. App will close now." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
And in the delegate method I'm closing the app using NSAssert:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSAssert(0, #"closing");
}
}
Also, I have included delegate protocol in header. However, the app just brings alert but after pressing OK it just freezes, and I'm getting some message "CoreAnimation: ignoring exception: closing". What am I missing or what other options exits?
You should not do that, it is against Apple HIG (Human Interface Guidelines):
iPhone applications should never quit programmatically because doing so looks like a crash to the user.
It is always better to provide some kind of feedback to the user about the error that occured and provide a way to restart the process without restarting the app. However, if you really, really want, you can use
exit(0);

UIAlertView and cocos2d memory mangement

I display a UIAlertView above my cocos2d layer.
I release it after I how it.
Is it really gone? Do I have to somehow remove it from my UIView?
If you just do
UIAlertView* alert = [[UIAlertView alloc] initWithTitle...];
[alert show];
[alert release];
then there's no need to worry about it. However, if you set the alert's delegate to some object, make sure that object still exists when the user taps a button in the alert - otherwise your app will crash when the alert will try to call alertView:didDismissWithButtonIndex: delegate method.

Cocoa Touch - Dialogue boxes?

How can i get a plain basic dialogue box in cocoa touch.seems so simple but I can figure it out...
Thanks!
You want the UIAlertView class. For example:
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Something happened"
message:#"And here is some more information about what it is."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
Create an NS Window using the Interface Builder. In your appdelegate, open the window.
Another option is CFUserNotification. The advantage to UIAlertView is that it does not require you to link against UIKit.

Best Practices for implementing a modal Alert View using Cocoa Touch?

Does anyone have any best practices, tips & tricks, or recommendations for how to create modal Alert Views with Cocoa Touch?
I like to think there is a way to make this difficult task trivial or at least easier.
You can use something like this:
void AlertWithMessage(NSString *message)
{
/* open an alert with an OK button */
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Name of the Application"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
I just use UIAlertView to display modal alerts. What additional functionality are you looking for?
Check out the UICatalog sample code from Apple. It shows the usage of both Alerts and Sheets on the phone.