Uialertview and memory management - objective-c

If I alloc unit a UIAlertview and then show it. Should I release it after the show or should I autorelease it?

This is the common way of showing an alert:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Title:"
message: #"The Message"
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alert show];
[alert release];

Release it after the show. It will retain itself until no longer needed.

It is good programming style to release it manually. Autorelease will also do the same thing but at it's own time. So it might release it latter also.

Related

UIAlertView and releasing object after showing it

I am reviewing some source code and noticed this method below. It isn't releasing the message after allocating it. Shouldn't there be a [message release]; after the show?
- (void)service:(TestService*)service didFailWithError:(NSObject *)error
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Service Error"
message:errorMsg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
If ARC wasn't enabled, then yes, release should have been called after show.

How do I send the iOS user an instant local notification?

I simply want a notification to pop up when the user presses a button. No servers or timers needed. All the tutorials I can find seem to involve one of these two.
Maybe something like this:
-(IBAction) buttonPressed: (UIButton *)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: [NSString stringWithFormat:#"You pressed button %#", sender.tag]
message: #"message"
delegate: self
cancelButtonTitle: #"Ok"
otherButtonTitles: nil]];
[alert show];
}
It would look something like this:
You can also customize the alert view a bit, so it could potentially have a textfield for text entry:
With UIAlertViews, you can also implement protocol methods like – alertView:clickedButtonAtIndex: and – alertView:willDismissWithButtonIndex:to perform different actions depending on which alert button they pressed.
Here's a good tutorial on UIAlertViews and implementing their protocol methods: http://mobile.tutsplus.com/tutorials/iphone/uialertview/.
If you don't want to use UIAlertViews and instead want a more customizable modal view, check out these two great libraries called UAModalPanel and MJPopupViewController. You can check out the links for images, demos, and more info on the two libraries, including links to their github pages where you can download them.
Hope this helps!
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelMessage];
[alertView show];
It is important to make sure the alert is shown on the main thread, where all the UI is handled. Otherwise you may get some weird errors and/or crashes. You can use GCD to dispatch to the main thread:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert Test"
message:#"This is an alert test."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",
nil];
dispatch_async(dispatch_get_main_queue(), ^{
[alert show];
});

UIAlertView delegate methods don't response on iOS 5.1

I created an instance of UIAlertView with two buttons and in the interface file of my class(.h) I set the delegate too but still cant get any response when clicking the buttons. Here is my code:
//myClass.h
#interface MainMenu : UIViewController<UIAlertViewDelegate>
-(IBAction)secondAct:(id)sender;
And the implementation
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert autorelease];
}
and the delegate method:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"UIAlertView delegate works");//this line too isnt displayed
NSString *title=[ alertView buttonTitleAtIndex:buttonIndex ];
if ([title isEqualToString:#"OK"]) {
NSLog(#"OK Pressed");
}//i want to create something like this
}
I did all of the code above but still can't take any action. Whichever of the buttons i click, it dissmisses the alert. What is wrong with this code can any one help?
ANSWER
Dear PeterG. commented to change delegete:nil with delegate:self and it works now.
Delegate should probably be set to self. If it generates an error post it here.
Also I do not think you should autorelease the alert ivar. Try to skip that line and see what happens?
Just give the delegate "self".
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
}

Alert View in the Beginning and the End of a void

I have a Void. This void do something really slow, so at the beginning of Void I put an alert, and in the end I put another warning. Like here:
-(void)action {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Start" message:#"Start." delegate:self cancelButtonTitle:#"Ok." otherButtonTitles:nil];
[alerta show];
[alert release];
//Something really slow
UIAlertView *alertEnd = [[UIAlertView alloc] initWithTitle:#"End" message:#"End." delegate:self cancelButtonTitle:#"Ok." otherButtonTitles:nil];
[alertEnd show];
[alertEnd release];
}
But when I run this code, the alerts are only shown at the end of the Void, after all the slow action.
How can I fix this?
First of all: try to avoid using many alerts since it's not humane to you users. Show UIActivityIndicator instead.
If you must, then try this code for showing the first alert:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Start" message:#"Start." delegate:self cancelButtonTitle:#"Ok." otherButtonTitles:nil];
[alert performSelector: #selector(show)
onThread: [NSThread mainThread]
withObject: nil
waitUntilDone: NO];
you will have to separate the two in two methods and then call one using
[self performSelector: afterDelay:]
method

UIAlertView without any buttons

I wanted to know whether the following code is okay or not. I am trying to dismiss the alertView automatically after 2 seconds (and without any buttons in the alertView) from the "timedAlert" method.
//this is in another method
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[alert release];
[self timedAlert];
}
-(void)timedAlert
{
[self performSelector:#selector(dismissAlert:) withObject:alert afterDelay:2];
}
-(void)dismissAlert:(UIAlertView *) alertView
{
[alertView dismissWithClickedButtonIndex:nil animated:YES];
}
If the cancelButton of the alertView is set to "nil", how will the "[alertView dismissWithClickedButtonIndex:0 animated:YES];" thing work??? I tried making the cancelButton "nil" and it worked, but cant figure out how....
P.S: I call the timedAlert method from another
Any help is appreciated! Thank you!
First let me say it would be better if you handle this with a custom view, but with that said the problem looks to be with
[alert release];
You are releasing the object before you are done with it (I am surprise it does not crash).
Do something like this
// other code
alert = [[UIAlertView alloc] initWithTitle:nil message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[self performSelector:#selector(dismissAlert:) withObject:alert afterDelay:3.0f];
}
-(void)dismissAlert:(UIAlertView *) alertView
{
[alertView dismissWithClickedButtonIndex:nil animated:YES];
[alertView release];
}
Your code should work, and you should have no problems. I have done this in one of my previous apps. The button is not displayed because the title is nil but I think the instance of the button still exists. Put a breakpoint before closing your alert and take a look at the alert variable, and check to see if there is a buttons array or something, that should tell you how that works.