EXC_BAD_ACCESS(code=1 address=0x2f9cd928) when running project on device - ios

on the simulator everything worked fine but on my ipod touch 6.1.5 i am getting error when clicking on any button in the alert view.
-(void)optionsButtonPressed {
UIAlertView *optionsAlertView = [[UIAlertView alloc] initWithTitle:#"Options" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add Note",#"Recover Last Note",#"Recover All", nil];
[optionsAlertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Add Note"]) {
AddNoteViewController *addNoteViewController = [[AddNoteViewController alloc] initWithNibName:nil bundle:NULL];
// ...
}
// ...
}
i wont post the whole method because its long.... the problem is that when the alertview appears, and as soon as i press on any button it is crashing which was not happening in the simulator.
i am using xcode 4.6.3
here is the error: Thread 1: EXC_BAD_ACCESS(code=1 address=0x2f9cd928)
NOTE: in the first viewController(first to load), i have an alert view that is working just fine, and has a method to handle which button is pressed.

Related

viewWillAppear UIAlertView not showing

I have two different view controllers, one for a dashboard and one for registration. I do not want the user to be able to interact with anything on the dashboard until the user logs in through an alertview. So every time the user navigates back to the dashboard or presses cancel and they are not logged in, I want the login alert to popup.
This works perfectly in all cases, including when the user hits the back button on the navigation bar in the registration view, but does not work when the user clicks OK on the alert in the registration page.
the dashboard view contains this code:
#property(strong) UIAlertView * alert;
//...
-(void)viewWillAppear:(BOOL)animated
{
user_email = [[NSUserDefaults standardUserDefaults] stringForKey:#"email"];
if ( user_email==nil ){
[self auto_login];
} else //...
}
-(void)auto_login
{
alert = [[UIAlertView alloc] initWithTitle:#"Login" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login",#"Forgot Password",#"Register",nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
{
self.debug.text = #"Cancel";
[self auto_login];
break;
}
//...
default:
{
self.debug.text = #"Register";
[self nav_register];
break;
}
}
}
-(void)nav_register
{
RegisterProfileController *rvc = [[RegisterProfileController alloc] init];
[self.navigationController pushViewController:rvc animated:YES];
}
The registration view controller contains this code:
-(void)catch_registration
{
NSString *response = [[NSString alloc] initWithData:self.httpdata encoding:NSASCIIStringEncoding];
if( [response isEqualToString:#"OK"] ){
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"..." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
successAlert.alertViewStyle = UIAlertViewStyleDefault;
[successAlert show];
}
else //...
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView.title isEqualToString:#"Success"])
[self.navigationController popViewControllerAnimated:TRUE];
}
After debugging, I know that after clickedButtonAtIndex runs in the registration view controller, [alert show] runs in the dashboard view controller, and clickedButtonAtIndex does NOT run in the dashboard view controller, but no alert shows up.
Why isn't the alert showing or how can I debug this further?
If clickedButtonAtIndex "does NOT run" as you said, then the delegate might not be set correctly. In addition, you should likely move the code from viewWillAppear: to viewDidAppear: because the view is not in the view hierarchy at that point. Your solution might be a combination of these two issues.

Dismiss alert view when performing delegate method

hi there when i run my delegate method which is parsing json data the alert view appears to freeze whilst it is performing the method is there anyway to hide the alert view whilst the app is running the code I've tried
- (IBAction)btnAdd:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Add Source" message:#"Enter the web address of the json data" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert setTag:0];
[alert dismissWithClickedButtonIndex:-1 animated:YES];
[alert show];
}
this doesn't actually do anything.
any advice?
*UPDATE
in the delegate method i get the same result
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 0) {
if (buttonIndex == 1) {
NSString *textEnteredraw = [[alertView textFieldAtIndex:0] text];
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
From method
- (IBAction)btnAdd:(id)sender
Remove
[alert dismissWithClickedButtonIndex:-1 animated:YES];
Because as #Dima said, you are dismissing the alertView before you are even showing it.
You call the code to hide the alert before you even show it. This method is meant to be called after the alert is shown.

How do i stop/cancel the segue when UIAlertView triggered

I got a button that leads to a tableViewController.
I created UIAlertView that gets triggered when the resultArray is null, which also means there is nothing to show on tableView but when user clicks ok, nothing changes and it goes to empty tableView.
How can I stop going into the other view controller?
this is my code
if (refilteredArray.count==0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No result"
message:#"Please enter a different combination."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
The segue is not triggered with code, its a button triggered IBAction
- (IBAction)motorButton:(id)sender {
...
}
you can use following method
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
in the method body you can verify if your array is empty or not and if it is return NO. To be more specyfic you should check your segue identifier (remember to add it to your segue in IB first)
You need to go for if.. else block
if (refilteredArray.count==0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No result"
message:#"Please enter a different combination."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}else{
[self performSegueWithIdentifier:#"yourSegue" sender:sender];
}
For more info, check this link about segue

Show Next Screen Only After User Closes Alert

I'm working on a losing screen and I'd like to let users view their game before showing the stats screen. I've decided to use an alert to show users the current screen. After they hit OK, they should be taken to the stats screen.
The problem is, the stat screen pops up at the same time as the alert. How can I make sure the stats screen opens after the user closes the alert?
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
You need to use delegate method of UIAlertView and also need to set delegate of UIAlertView = self,
Following is delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
check buttonIndex in this method it's give you NSInteger and manage click of button.
For more information read this UIAlertView Delegates.
You need to set the alert view delegate. In this example self will conform to the UIAlertViewDelegate protocol.
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.delegate = self;
[alert show];
}
You can then listen to a delegate method for when the alert view is dismissed :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
For this situation need to use delegate method,
For simple Example Refer Here: http://www.idev101.com/code/User_Interface/UIAlertView.html

How to create a pop-up when exiting a view?

So I'm making a photo editing app for school from last semesters project. When a user clicks the back button (in editor mode) which in my case is called "Product Selection" I'd like to have a pop-up come up and say "Do you want to delete everything and go back to product selection?" to see if they want to discard their work, and if the user selects yes, the entire project is discarded and they are put back in the lobby.
Where do I put this code to do this? I found the "Product Selection" button in the min story board but not sure what to do from there.
The code for the pop-up I was going to use is:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification"
message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert show];
[alert release]
Any help/wisdom would be extremely appreciated!
I would create a "Cancel" UIBarButton which executes a custom function:
- (void)cancelTapped {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert setTag:1];
[alert show];
}
And listens for the alert to return on:
- alertView:didDismissWithButtonIndex:
From there, I would write the logic to hide the page and pop the view.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1 && buttonIndex == 1) {
// Delete data and return to lobby
[self.navigationController popViewControllerAnimated:YES];
}
}
Use this delegate method (don't forget adding UIAlertViewDelegate on your .h):
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
alert.cancelButtonIndex = 0;
[alert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// If user confirmed:
if (buttonIndex != 0) {
// Do what you need.
}
}

Resources