iOS7 UIAlertView EXC_BAD_ACCESS when set UIAlertViewStylePlainTextInput - ios

I'm trying to show UIAlertView for iOS 7 device with input field so i have method
- (void)createAlertView
{
NSString *msg = #"Description (optional)";
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:lastMarkAsOutdatedActionTitle
message:msg
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Confirm", nil];
myAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[myAlert show];
}
which I'm calling like this
[self performSelectorOnMainThread:#selector(createAlertView) withObject:nil waitUntilDone:NO];
but at the line myAlert.alertViewStyle = UIAlertViewStylePlainTextInput; I'm getting error Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)
I created clean project and tested this code - it works so I stucked with it. Any ideas?
Update
Screens at the moment of error

Related

iOS ARC UIAlertView leaking memory

I have simple method showing AlertView with textfield. Instruments showing memory leak in this. Please explain.
- (void)method {
NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:#"key"];
if (value == nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alertView.tag = 101;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *txtGroup = [alertView textFieldAtIndex:0];
[txtGroup becomeFirstResponder];
[alertView show];
alertView = nil;
}
}
Please find the screenshot of Instruments:
You need to create alertView as :
static UIAlertView *alertView = nil;
if (!alertView){
alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
}

iOS UIAlertView being shown more than once

Hi I have used the following function to call an alert view with a specific message. The NSLogs are being printed only once but alert view is called more than once.
I am using UITabBarController for any additional info.
- (void)showUIAlertWithMessage:(NSString *)message {
NSLog(#"showUIAlertWithMessage called");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something went wrong!😕😕"
message:message
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}

Order of executing methods

I have a problem about order of executing methods.
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
When I run this code snippet, I want to first show an alert view. However, it calls the getData method before showing alert view. Once the getData method is completed, alertView comes to the window.
How can I correct this?
The function is called asynchronously therefore appData gets called before the first alert view is visible. Change your code to this:
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
The below method will be called when the user presses OK on your first alert, but it means that while the first alert is visible, your code for appData would not start.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
Note: Remember to add UIAlertViewDelegate in your view controller
I don't have Xcode with me, but I'll take a stab at it anyway. Your problem is because the alert won't show until the main loop iterates. And it won't iterate until your getData method is executed since you're doing this on the main thread. So you need to fork.
Try wrapping your if() in something like this:
dispatch_async(dispatch_get_main_queue(),
^ {
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
});
It's because the run loop must continue running so [UIAlertView show] is asynchronous and will not block the current thread. If the main thread was blocked then no user interface events could be delivered.
If you want something to occur after the first alert view is dismissed then do it within the alertView:clickedButtonAtIndex: delegate method.

Alert View in iOS7

I'm very new to iOS and Objective C. I have the following code to create an alert view in iOS7 for an iPhone application:
if ([tracker exceededGoal]){
[UIAlertView] *alert = [[UIAlertView alloc] initWithTitle:#"Done" message:#"You acheived your goal!" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
I'm getting the following error:
Use of Undeclared identifier 'alert'
Expected identifier
What did I do wrong?
Remove the brackets here: UIAlertView *alert =

UIAlertView crash in iOS 6

I have this code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
The problem is this code crash before it shows the alert, I have tested it on lower iOS and it work but on iOS 6 it crash.
I found the answer. I coded:
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
instead of
[alert show];
it crash because the process might not be performed in the main thread.
Source from:
https://stackoverflow.com/a/12475858/1179680

Resources