UIAlertView and cocos2d memory mangement - iphone

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.

Related

UIAlertView with NO buttons and how to close it

I have activity indicator in alert view that I use until my app gets the server response. The app send data to server and the alert view shows, how to close it when the server sends me the response. Here is code from my alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Canceling reservation" message:#"please wait" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator to place at the bottom of the dialog window.
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height-50);
[indicator startAnimating];
[alert addSubview:indicator];
[alert dismissWithClickedButtonIndex:0 animated:YES];
You can use MBProgressHUD instead of standart UIAlertView for something like that.
You can use the dismissWithClickedButtonIndex: delegate method for dismissing the alertView.
[alert dismissWithClickedButtonIndex:0 animated:YES];
Make sure that alert is declared on the #Interface.
dismissWithClickedButtonIndex:animated:
Dismisses the receiver, optionally with animation.
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated Parameters
buttonIndex
The index of the button that was clicked just before invoking this method. The button indices start at 0.
animated
YES if the receiver should be removed by animating it first; otherwise, NO if it should be removed immediately with no animation.
Discussion
In iOS 4.0, you may want to call this method whenever your application
moves to the background. An alert view is not dismissed automatically
when an application moves to the background. This behavior differs
from previous versions of the operating system, where they were
canceled automatically when the application was terminated. Dismissing
the alert view gives your application a chance to save changes or
abort the operation and perform any necessary cleanup in case your
application is terminated later. Availability
Available in iOS 2.0 and later.
Declared In UIAlertView.h
Please refer UIAlertView

Close Alert View Via Code

I've got an alert:
UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Connection Failed" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
I want to know how I can programatically close an alert view when I hit a certain event in my code. I know there is no distinct call to just close out an alert, and I've seen people make calls straight to buttons, however, I'm not sure how to call the cancel button on an alert.
Is this even the right way to go about it? Or is there a better path?
You can use -dismissWithClickedButtonIndex:animated: method for dismissing alert view:
[connectionError dismissWithClickedButtonIndex: connectionError.cancelButtonIndex
animated: YES];
That's the only public API for dismissing alerts, so you should use it.
Declare you alert view in .h file
then call this function
[connectionError dismissWithClickedButtonIndex:0 animated:YES];
Hope it helps..

UIAlertView in ARC app

I have iphone sample application with one button. On tap it calls code:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"test"
message:#"test"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
Application has ARC enabled.
The problem is that if I click on OK button in alert, application crashes with EXC_BAD_ADDRESS - probably because of alert is already removed by arc.
What is recommended way to solve this? without adding property to viewcontroller
thanks
My guess is either you haven't implemented the UIAlertViewDelegate methods or that self has gone out of scope.
If you don't care about being alerted when someone dismisses the alert box, change the delegate to nil.
e.g.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"test"
message:#"test"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
The delegate property of UIAlertView is declared as a weak reference (indicated by the assign keyword). This means ARC will not increment the retain count and you will need to retain a reference to the delegate object (whether it is self or a separate object). This reference should be maintained for the full life of the UIAlertView.
#property(nonatomic,assign) id /*<UIAlertViewDelegate>*/ delegate; // weak reference
I assume that the reason for this is to prevent a loop and failure to release as in the common case the object creating the UIAlertView will retain a reference to it and be it's delegate so closing the loop and preventing release.
This is probably a common pattern for delegates but I hadn't realised until I hit exactly the same issue. I ran in the simulator with zombie detection enabled and it clearly indicated the reference counts and I saw that it was not being retained by the UIAlertView. That was when I checked the header file.
The answer to this question provides some additional information about delegates on system classes and the assign keyword.
iPhone ARC Release Notes - dealloc on system classes delegates?

iPhone - a UI component that looks like an Alert box - Beginner

I need to know what this UI component is called ? It looks like an Alert but has textfeilds and buttons in it ?
1.) What is this UIComponent called ?
2.) Is there any video tutorial which shows how to implement this ? (If so link) or any tutorials that discuss the implementation of this
It's UIAlertView
Here is the tutorial to make custom UIAlertView
That is an Android component, there is no equivalent in iOS.
There is the UIAlertView, but you cannot edit text within it.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/cl/UIAlertView
If you want to be able to let users edit text in the component, you have to create it from scratch, e.g. create a UIView and add a UITextView to it and some UIButtons and create the functionality to display/dismiss the component yourself.
AlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
The component on iPhone is called a UIAlertView. You can add subviews in it to get text-fields, although this is not recommended by Apple - but you won't get rejected either. In iOS5, there are dedicated UIAlertView types for this. If you are building for iOS5, this is the method you should use.

Bug in AlertView on iPhone?

I write a piece of code to "do something->show alert1->do something->show alert2".
//do something
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Alert 1"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//do something
UIAlertView *alert2 = [[UIAlertView alloc]
initWithTitle:#"Alert 2"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
[alert2 release];
And suddenly a strange thing happened to multiple AlertViews: It shows "Alert 1"->"Alert 2"(Press 'OK')->"Alert 1". Why "Alert 1" shows again? I haven't written any delegate method yet. Maybe a bug?(Thanks to cobbal, alert1 is still there when alert2 appears.)
I find -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works well. Is the delegate method a common way to show multiple alertViews?
I would guess that alert 1 is shown and then covered by alert 2 since show isn't modal. When alert 2 is closed, alert 1 is still open.
To your second question, alertView:didDismissWithButtonIndex: may work better, but I haven't actually tested that.
The delegate is so that you can be notified when the alert is dismissed, and which button was used to dismiss it. It doesn't impact whether the alert is dismissed at all.
The alert will remain visible until it is dismissed either by you tapping a button (if any - they are not required) or you call either [UIAlertView dismissWithClickedButtonIndex:animated] or the (undocumented) dismiss method of the alert instance.
It looks like (as Cobbal suggested), alert 2 is popping up over alert 1, you dismiss alert 2, and alert 1 remains there (until it itself is dismissed).
Is there a particular reason you want to show a new alert while another is still showing? Perhaps some more context would help us to get to the root of the issue, which I suspect may be a design issue.
[edit] coming back to this and reading again, I wonder if what you are asking about with the delegate method is whether you should be showing alert 2 from there? In which case that's probably what you want - whether directly or indirectly. By indirectly I mean that you may have some state set elsewhere that determines whether alert 2 should be shown (or the circumstances that lead to it). That state (a flag, perhaps) could be set when you show the first alert, and cleared when the alert is dismissed (from the delegate method).
The reason this happens is because UIAlertView doesn't block while it's showing. Any code written after showing an alertview will run straight after the alert is shown.
What you should have is two different methods. One that does something and then shows an alert, and then another that does something and shows another alert.
Kick off the first method to do something and show an alert, and then hook into the alert's delegate method, and when you get the callback from the alertview, run the other method.
This way, the second part of the process won't happen until the user has pressed OK on the alert in first part of the process.