How to create UIAlertView with multiple options? - iphone

I am trying to work out how I can use a UIAlertView to do more than one command.
Basically, in my ViewController, there is already an alertView, however I am now adding some storekit files, which, require there own alertView (to tell it whether to purchase the in-app or cancel etc.
Here is original alertView code;
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
if (buttonIndex == 1) {
[g_GameUtils removeAlbumFolder:deleteIndex];
[g_GameUtils readAllData];
[g_GameUtils getAlbumFolderList];
[m_pTable reloadData];
}
}
And here is what I need also - they are both called alertView so I cannot use both like this, is there a way to combine them? Or is it better to call one of them alertView2 ? If so, how does it know which one to call for the particular alert?
Thanks in advance!
Chris
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
//cancel button clicked. Do something here or nothing here
}
else{
//other button indexes clicked
[[MKStoreManager sharedManager] buyFeature:#"com.davis.apptoken.buy"];
}
}

Also you can use alertView.tag = 1; and alertView2.tag = 2; and add appropriate conditions to delegate:
if (alertView.tag == 1)
{
// First alert
}

Related

UIAlertView Button Event Problem

alright so i have a small problem here, i have this here.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//YES clicked ...do your action
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
else if (buttonIndex == 1)
{
//NO clicked
return;
}
}
This Allows me to capture events triggered from UIAlertView Buttons but i have assigned it a value and on the same page i need another value to be assigned to that class so:
if(buttonIndex == 2){//Proceed}
i mainly want it so that when the button is pushed on my second alert it will go back to the processes it was doing and not proceed with the event of (buttonIndex == 0).
So Does anyone know Where i could start?
Just store a reference in you .h file of the 2 UIAlertView's, and then do a check. For instance, in your .h file:
UIAlertView * alertView1;
UIAlertView * alertView2;
In your .m file, set up your UIAlertView's in you viewDidLoad method, and change the alertView:clickedButtonAtIndex: method to:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; {
if (alertView == alertView1) {
// Do what you want the alertView1 to do here.
if (buttonIndex == 0) {
//YES clicked ...do your action
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
else if (buttonIndex == 1) {
}// etc.
}
else if (alertView == alertView2) {
// Do what you want the alertView2 to do here.
}
}
Hope that Helps!
Something else you can do is use the alertView tag in case you have multiple alertviews in your app. For instance, you could do something like this:
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"Title" message:#"AThe message." delegate:self cancelButtonTitle:#"button 1" otherButtonTitles: #"button 2", nil];
alert1.tag = 0;
[alert1 show];
[alert1 release];
Then in your delegate method, simply put the following if clause:
if (alertView == alertView1)
before your code above.
If you don't figure this out, you could place a counter in the program that counts the number of times the alertview had been triggered, and then act upon that value.

UIAlertView easy way to tell if cancel button is selected

I know I've done this before but I just can't figure it out again.
What is the method I would use to see if a cancel button was pressed. I don't want to do it based on the button index. There is a way to do it, something like:
[alertView isCancelIndex:index];
Anyone know?
The UIAlertView has a property of cancel button index
#property(nonatomic) NSInteger cancelButtonIndex
Usage
[alertView cancelButtonIndex]
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView cancelButtonIndex]) {
NSLog(#"The cancel button was clicked for alertView");
}
// else do your stuff for the rest of the buttons (firstOtherButtonIndex, secondOtherButtonIndex, etc)
}
In the delegate of UIAlertView is the method
(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
And then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSInteger cancelIndex = [alertView cancelButtonIndex];
if (cancelIndex != -1 && cancelIndex == buttonIndex)
{
// Do something...
}
}

Simple question on UIAlertView and UIAlertViewDelegate

I hope this is pretty straight-forward. As you'll see by my code, I'm simply trying to get a UIAlertView button-press to pop me back to the root view.
I don't get any compile errors or warnings, and when I run the app, the "RedeemCoupon" method is called in the IBAction, and the UIAlertView pops up as it should, but it doesn't seem the "doneRedeeming" method gets called at all - I don't see anything from NSLog (yes I'm aware that I am setting buttonIndex to 0 - once I get this working I'll fix it). So, basically it doesn't work. I click the "cancel" button and the alert just goes away.
By the way I'm not sure if this matters, but this "RedeemCouponViewController" view is number 4 on the stack, and it was added by use of presentModalViewController in the previous view.
I'm open to other ways of doing this if needed - all suggestions welcome!
Thanks in advance!
// RedeemCouponViewController.h
#interface RedeemCouponViewController : UIViewController <UIAlertViewDelegate> {
// RedeemCouponViewController.m
- (IBAction) redeemYes: (UIButton*) sender {
CouponRedeem *redeem = [[CouponDatabase database] couponRedeem:_uniqueId];
[redeem release];
UIAlertView *doneRedeeming = [[UIAlertView alloc]initWithTitle:#"Coupon Redeemed!"
message:#"Thanks for shopping!"
delegate:self
cancelButtonTitle:#"Back to Main Menu"
otherButtonTitles:nil];
[doneRedeeming show];
[doneRedeeming release];
}
-(void) doneRedeeming: (UIAlertView *) doneRedeeming clickedButtonAtIndex: (NSInteger)buttonIndex {
if (buttonIndex = 0) {
NSLog(#"doneRedeemed method called");
[self.navigationController popToRootViewControllerAnimated:YES];
} else {
//do nothing
}
}
You want to have
if (buttonIndex == 0) {
in place of
if (buttonIndex = 0) {
The former checks for equality whereas the latter assigns.
Also, you want to have
– alertView:clickedButtonAtIndex:
where you have
- doneRedeeming:clickedButtonAtIndex:
You need to use UIAlertViewDelegate methods:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {}
not
-(void) doneRedeeming: (UIAlertView *) doneRedeeming clickedButtonAtIndex: (NSInteger)buttonIndex {}
Use the delegate method -alertView:didDismissWithButtonIndex: to listen for your cancel button index
#PengOne's answer is correct: your problem is this:
if (buttonIndex = 0) {
You said
I know it's not correct, but I just
wanted to be sure the statement was
true for now...
But buttonIndex = 0 evaluates to 0, making it equivalent to
if (0)
The code within that block will never execute, regardless of the value of buttonIndex. If you really want to do it unconditionally, change the if to if( 1 ), or just take the if out.
This would have been trivial to spot if you ran this code in the debugger. You might think you know what your code is doing, but if you don't watch it run, you don't.

How to check alertview values

This is my code :
self.myAlert = [[[UIAlertView alloc] initWithTitle:#"MNB" message:#"R u want to delete" delegate:self cancelButtonTitle:#"OK",nil otherButtonTitles:#"Cancel",nil] autorelease];
[myAlert show];
Here I would like to process if OK button click and also for cancel button, I would like to redirect the page if OK button clicked....I need the coding, IF condition statement when OK button clicked.....pls help me....
Read UIAlertViewDelegate Protocol Reference.
You can implement following methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Just need to write the Delegate method of UIAlertView like this
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==1){
NSLog(#"Cancelled Clicked");
}
if(buttonIndex==0){
NSLog(#"O.K Clicked");
}
}
Will surely work :)
You can use UIAlertView delegate methods. For example
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0) //your OK button
{
//Some code here
}
else if(buttonIndex == 1)
{
//Some other code
}
}

- (void)alertViewCancel:(UIAlertView *)alertView is not called

I've got the problem that the UIAlertViewDelegate method - (void)alertViewCancel:(UIAlertView *)alertView is not called when I cancel a AlertView with it's cancel button.
Weird is that the delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works perfectly.
Does anyone have an idea?
Thanks in advance
Sean
- (void)alertViewCancel:(UIAlertView *)alertView
{
if(![self aBooleanMethod])
{
exit(0);
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
}
I call this when a button is clicked:
- (void)ImagePickDone
{
UIAlertView *alertDone = [[UIAlertView alloc]
initWithTitle:#"Done"
message:#"Are u sure?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: #"Yes", nil];
[alertDone show];
[alertDone release];
}
The alertViewCancel is used for when the system dismisses your alert view, not when the user presses the "Cancel" button. Quote from apple docs:
Optionally, you can implement the
alertViewCancel: method to take the
appropriate action when the system
cancels your alert view. If the
delegate does not implement this
method, the default behavior is to
simulate the user clicking the cancel
button and closing the view.
If you want to capture when the user presses the "Cancel" button you should use the clickedButtonAtIndex method and check that the index corresponds to the index for the cancel button. To obtain this index use:
index = alertDone.cancelButtonIndex;
You can handle the Cancel at the index 0 of this delegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
//cancel button clicked. Do something here.
}
else{
//other button indexes clicked
}
}
This can be improved in two ways. First, it only handles the case that the user actually clicked a button. It doesn't handle the situation that [myAlert dismissWithClickedButtonIndex:] is called, or that the alert is dismissed in some other way. Second, button 0 is not necessarily the cancel button. In an alert with two buttons, the left one is at index 0, and the right one is at index 1. If you changed the titles so that the right button says "Cancel", then button 1 is logically the Cancel button. Instead of "willDismiss" you can implement "didDismiss" which will be called after the dialog has disappeared and not before.
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == alertView.cancelButtonIndex)
{
//cancel button clicked. Do something here.
}
else
{
//other button indexes clicked
}
}