I have a class called FlightRecorder and BookmarkTableViewController. Now, I am presenting the BookmarkTableViewController. And now I am dismissing that view controller. Here is what the code looks like:
[self dismissViewControllerAnimated:YES completion:^{
FlightRecorder *fl = [[FlightRecorder alloc] init];
[fl endBookmarkProcessWithBookmarkCollection: dict];
}
This is getting called after selection of a table view cell. So, FlightRecorder has a method called endBookmarkProcessWithBookmarkCollection: where you pass on an NSDictionary which looks like this:
- (void)endBookmarkProcessWithBookmarkCollection: (NSDictionary *)dict {
NSString *compiledText = #"Random Airline";
NSString *string = [NSString stringWithFormat: #"\nMiles: %.2f\nFlight: %#\nDate: %#", [[dict objectForKey: #"miles"] floatValue], compiledText, [[NSUserDefaults standardUserDefaults] objectForKey:#"tempD"]];
self.bkBookmarkAlertView = [[UIAlertView alloc] initWithTitle:#"Confirmation" message:string delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
[bkBookmarkAlertView show];
}
So as you can see, I use the dictionary and some other variables to piece together the message of an alert view! The alert view has two buttons, Cancel and Add. But the problem is that when you press either of them, they both crash, and the clickedButtonAtIndex: delegate method is not called. I have not had this experience with any other alert views in the exact same view controller.
I looked at some other questions of similar manner, but they're solutions were using ARC. Bottomline they said that self, the delegate of the alert view, is nil or overreleased or something like that. So I thought, maybe that method is getting called before the view loads, even though it is called on apparent completion (the completion block in BookmarksTableViewController).
So I decided to put NSLog's in both viewDidLoad and endBookmarkProcessWithBookmarkCollection:, but viewDidLoad is getting called first, so I am not sure where the problem is.
Thanks for any help!
That block has finished executing and f1 has gone out of scope and been deallocated well before you tap the buttons. I'm not familiar with the architecture of your app but you'll need to find an owner for that object while waiting for the tap.
Related
I'm having a terrible time getting a UIAlertView to work within my custom NSObject class. In the research I've done it appears it should be possible but here's what I've run into.
First, here's my code:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"clickedButtonAtIndex: %d", buttonIndex);
}
-(void)testAlertView {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"List Contains Items"
message:#"List contains items. Remove all items & delete?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alertView show];
}
If I set the delegate to self this code crashes as soon as I tap a button. If I set it to nil clickedButtonAtIndex is never called. I've tried with and without using the <UIAlertViewDelegate>.
I know someone will ask 'why are you doing this in NSObject instead of in your UIViewController?'. Primarily because I want to separate this code out so I can use it from multiple places in my app. But also because this is a small piece of a larger block of logic that makes sense to be on it's own.
Any ideas what I'm doing wrong?
Thanks,
Rich
I had the same problem using ARC. The root of the problem was the same. I solved it by putting my custom NSObject into a "strong" property to make sure the object exists as long as the calling object (an UIVIewCOntroller in my case) exists, so when the delegate of my alert view is called I still have my custom object around and the delegate method works fine.
Add the NSObject as strong property:
#import "Logout.h" // is NSObject
.
.
.
#property (nonatomic, strong) Logout *logout;
Then you will get the delegatemethods called in your NSObject.
Don´t forget to register the delegate for the UIAlertView:
#interface Logout () <UIAlertViewDelegate>
and in your method:
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"title"
message:#"message" delegate:self cancelButtonTitle:#"cancel"
otherButtonTitles:#"ok", nil];
[a show];
How To Present An Alert View Using UIAlertController When You Don't Have A View Controller. Detail description.
Yes, you can only use UIAlertController only in UIViewController classes. So how can we do it in NSObject classes. If you see the description link given above you will get to the answer. To summarise in a line for the above description: Create a new window above the the current window. This new window will be our viewController where we display alert. So using this viewController you can call the method [presentViewController: animated: completion:].
Answer:
dispatch_async(dispatch_get_main_queue(), ^{
UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = [UIViewController new];
window.windowLevel = UIWindowLevelAlert + 1;
NSString *msg=#“Your mssg";
UIAlertController* alertCtrl = [UIAlertController alertControllerWithTitle:#“Title" message:msg preferredStyle:UIAlertControllerStyleAlert];
[alertCtrl addAction:[UIAlertAction actionWithTitle:NSLocalizedString(#"Yes",#"Generic confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// do your stuff
// very important to hide the window afterwards.
window.hidden = YES;
}]];
UIAlertAction *cancelAction= [UIAlertAction actionWithTitle:#"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
window.hidden = YES;
}];
[alertCtrl addAction:cancelAction];
//http://stackoverflow.com/questions/25260290/makekeywindow-vs-makekeyandvisible
[window makeKeyAndVisible]; //The makeKeyAndVisible message makes a window key, and moves it to be in front of any other windows on its level
[window.rootViewController presentViewController:alertCtrl animated:YES completion:nil];
});
I'm not understanding why this is giving me a EXC_BAD_ACCESS. Basic background, im collecting and processing some information, and then using a modal view to let the user confirm if he want to continue.
I have a button on the navigation bar called continue, that calls my data prepare function.
- (void)viewDidLoad {
//Other stuff
UIBarButtonItem *next = [[UIBarButtonItem alloc]
initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(prepData)];
self.navigationItem.rightBarButtonItem = next;
[next release];
[super viewDidLoad];
}
prepData:
-(void)prepData{
/*
There's a bunch of stuff going on here, if "mensaje" is not an empty NSString, there is some kind of error that wont let me go on, if not, everything in the data is fine
*/
if(![mensaje isEqualToString:#""]){
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil
message:mensaje
delegate:nil
cancelButtonTitle:#"Aceptar"
otherButtonTitles:nil];
[alert show];
[alert release];
}else{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"¿Esta seguro que desea realizar estas operaciones?"
delegate:self
cancelButtonTitle:#"Cancelar"
destructiveButtonTitle:#"Aceptar"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
}
If I debug, I can get all the way through the prepData(), as soon as I press continue I get a EXC_BAD_ACCESS. If I comment out the [actionSheet release]; I dont get the exception, but it is to my knowledge, that just like alert views, action sheets "stick around" until they show.
At least all books I've read state that, but its quite possible that I'm not understanding something in the autoreleasing.
Just for reference, the alert pops up just fine.
Anybody have any idea whats going on here?
Thanks, Stefano.
Edit: Figured it out, the above code for actions sheets and alert views is fine, the issue was that I was releasing something that was later trying to be autoreleased.
I have for loop that does this:
for(someConditions){
NSString *montoFormateado = [[[NSString alloc] initWithFormat:#"%.2lf",[monto doubleValue]] stringByReplacingOccurrencesOfString:#"." withString:#","];
[_postBuild setObject:[NSString stringWithString:montoFormateado] forKey:[NSString stringWithString:iidvar]];
[montoFormateado release];
}
postBuild = [_postBuild mutableCopy];
[_postBuild release];
Now, it seems the error was in the fact that by using [NSString strintWithString:montoFormateado] I was leaving that string up for autorelease later, but when I released _postBuild that string was getting released also, I removed that and just used setObject:montoFormateado and its working fine.
Its leaking memory, but I think thats for a diferent question, the exc_bad_access got solved.
The easiest way to find this kind of bug is to use the NSZombieEnabled environment variable.
When you get an EXC_BAD_ACCESS at the end of a call, it usually means that something is being autoreleased, but it's retain count is already 0 from you releasing it.
Check this post, it'll save your life a lot of times. If that doesn't work, leave a comment and we'll put our debugging caps back on :)
I think there's one release too many in the code you didn't show (in the "There's a bunch of stuff going on here, if "mensaje" is not an empty NSString" block).
There's nothing wrong with your actionSheet code. As you say, it should deallocate itself once it's no longer on screen. But if you over-released one of the sub-views of self.view then it could cause a crash when the autorelease pool is drained.
In both cases, if you're gonna "release", you better "retain" first.
In the case of the UIActionSheet - I believe the view passed into showInView is retaining a copy of the actionsheet.
In the AlertView this is not the case.
Maybe you are releasing the UIAlertView before its used, try removing the release statement and declare the UIAlertView as auto release (though it should be fine the way you have it)
if(![mensaje isEqualToString:#""]){
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil
message:mensaje
delegate:nil
cancelButtonTitle:#"Aceptar"
otherButtonTitles:nil] autorelease];
[alert show];
Another thing that it might be is that you are releasing these guys again on some call back or something, you should check you arent doing that, and like the poster above me said, you can use zombies to figure this stuff out as well.
I have such error: when I click navigationbar.backItemButton I'm showing UIAlertView with two buttons. When I press on any of them application terminates just with EXC_BAD_ACCESS. Method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex don't called. How can I solve it? Thanx!
//h - file
#interface DetailsTableViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>
//m - file
- (void)viewWillDisappear:(BOOL)animated
{
//if changes unsaved - alert reask window
if (isDirty)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Save changes?"
message:#"Press YES if you want to save changes before exit, NO - other case."
delegate: self
cancelButtonTitle: #"NO"
otherButtonTitles: #"YES", nil];
[message show];
[message autorelease];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex: buttonIndex];
if([title isEqualToString: #"YES"])
{
[self saveBtnUserClick];
}
}
I think the problem is that after you tapped back button your current controller is removed from navigation stack and deallocated, so when alert tries to call its delegate methods it calls them on deallocated object which results in EXC_BAD_ACCESS error. To workaround the problem I see 2 obvious options (although there may be better solutions):
Extra retain your controller somewhere (in previous controller may be), but you need to find way to release it when you're done.
Create your custom button instead of standard "back" and just show alert when it tapped. Then in alert's delegate method pop your current controller from navigation stack.
Try Changing delegate to nil instead of self. It fixed my issue.
Is your view controller implementing the UIAlertViewDelegate? If not, add in you interface declaration before the { starts.
Also try NSLogging inside the clickedButtonAtIndex method and print the buttonIndex values and see the console.
Edit: Reading your post again, I guess you indeed have missed the UIAlertViewDelegate in your interface declaration.
Probably [message autorelease];
is you mistake use
[message release];
Because you have used [[UIAlertView alloc] init.....]; there for you should release the memory.
autorelease is something will work with the structure which memory is compiler dependent or you have not given the memory manually.
Enjoy.
"Try Changing delegate to nil instead of self. It fixed my issue." worked for me. Thanx
A UIAlertView is displayed if an error occurs. But in the meantime the view on which the UIAlertView were called has been dismissed (and therefore released). If the user clicks on OK the app crashes because a message to a released instance is sent. This will cause your app crashing:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
alertView = nil;
[self.navigationController popViewControllerAnimated:YES];
I thought the UIAlertView is an independent unit. But it seems it isn't. Is there a way how I could avoid the app crashing (except not dismissing the view)?
The delegate is called when the UIAlertView is dismissed, so in your case:
delegate:self
Delegates are not retained, like an object added to an array, or a subview would be. So in your case, when you call:
[self.navigationController popViewControllerAnimated:YES];
self is most likely being released, and when the the user dismisses the alert, self is called, but has been dealloc'd so it no longer exists.
An easy way to check this is to put a logger statement, like NSLog(#"I'm gone"); in self's dealloc method, if it's ran, then you know your self isn't around anymore, and any messages sent to it will cause a crash.
Make the UIAlertView a retained property of your view controller so that you can refer to it in your dealloc, then set the alert view's delegate to nil when the view controller is deallocated.
Be sure to properly release the retained alert view once it's been dismissed and on dealloc.
For instance:
#interface MyViewController : UIViewController <UIAlertViewDelegate> {
UIAlertView *alertView;
}
#property (nonatomic, retain) UIAlertView *alertView;
#implementation MyViewController
#synthesize alertView;
- (void)showAlert {
if (alertView) {
// if for some reason the code can trigger more alertviews before
// the user has dismissed prior alerts, make sure we only let one of
// them actually keep us as a delegate just to keep things simple
// and manageable. if you really need to be able to issue multiple
// alert views and be able to handle their delegate callbacks,
// you'll have to keep them in an array!
[alertView setDelegate:nil];
self.alertView = nil;
}
self.alertView = [[[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Something went wrong"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Retry",nil] autorelease];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertViewParam didDismissWithButtonIndex:(NSInteger)buttonIndex {
self.alertView = nil; // release it
// do something...
}
- (void)dealloc {
[alertView setDelegate:nil]; // this prevents the crash in the event that the alertview is still showing.
self.alertView = nil; // release it
[super dealloc];
}
The downside here is that you will not be able to handle the alert view's callback when the user dismisses it. However, since your controller is already gone/released, presumably you don't need to. If you do, you have to set the alert view's delegate to something that will persist.
If the UIAlertView object is to be usable from anywhere in the app, not just on the current view, then retain it inside something that is available from anywhere in the app, either some persistant root view controller under the entire possible view stack, or the app delegate.
Added:
This top level object can also retain the alert view's delegate until after it's done being needed (after alert view dismissal).
(People might wonder I am late by years in answering this question,but it might help someone else)
I guess your problem lies some where in popping the view controller,you are displaying the alert view and at the same time trying to navigate the user back to a view.I would recommend you to follow a hierarchal approach here i.e.:
First of all declare your alert view as a global object,i.e.
#property(nonatomic,retain) UIAlertView *sampleAlert;
Now write your alert view display code where ever desired,say for instance:
-(IBAction)buttonClicked:(id)sender
{
self.sampleAlert = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[sampleAlert show];
[sampleAlert release];
}
Finally try to navigate the user to the desired view when the "Ok" button is pressed,i.e. you need to make use of alertView didDismissWithButtonIndex method,i.e.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView == sampleAlert)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
Please note that if you have alert view with multiple buttons,you also need to check for button index for distinguishing actions,i.e. check using
if(alertView == sampleAlert && buttonIndex == 0)
{
//Do your stuff
}
else
{
//Do something else
}
This will definitely avoid application crash,thanks :)
Easier way that worked for me is to hold all the alert views in a Array and when the parent view is deallocated enumerate alertViews array and assign the delegate to nil. This will ensure that the touch event is ignored and app will function.
// ARC world
#property (strong, nonatomic) NSMutableArray *alertViews;
- (void)dealloc
{
[self.alertViews makeObjectsPerformSelector:#selector(setDelegate:) withObject:nil];
}
Make sure you are implementing the UIAlertViewDelegate protocol.
If you don't care about when the alert is dismissed just init with delegate as nil.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
My alertview appears twice and requires 2 user clicks to dismiss.
- (void) showAlert: (NSString *) message
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"You chose"
message: message
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"ok",nil];
av.tag = SLIDER_ALERT;
[av show];
}
I am then releasing the alertview in the delegate method "alertView: (UIAlertView *) alertView clickedButtonAtIndex: (int) index"
I suspect the problem is that I have built my own view hierarchy programmaticaly. I have one parent view for the viewcontroller. Under that i have 2 other views (parentView -> subview1 and subview2). I've tried to call [self.view addSubview: av] but that does not work. Neither does bringToFrontSubView:
Any help would be much appreciated
Peyman
The Alert code is fine (other than the release, mentioned in the comments).
[av show] is all that's required to show a view. You don't add UIAlertViews as subviews.
Call it after a delay of 0.1 sec [self performSelector:#selector(showAlert:) withObject:nil afterDelay:0.10];
Hope this will solve your problem.
With using autorelease the Alert View seems to be twice or 3 times. And for iOS4 it needs to be autoreleased otherwise it will crash.