UIAlertView easy way to tell if cancel button is selected - iphone

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...
}
}

Related

How to create UIAlertView with multiple options?

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
}

UIActionSheet from UIAlertView

I'm trying to show a UIActionSheet when the user touches a button in a UIAlertView:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIActionSheet *actionSheet = ...
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}
}
When the action sheet is shown the alert view is still on the screen behind the action sheet, and when I touch a button in the action sheet - the action sheet disappears but the whole screen is dimmed with the alert view still on and I can't dismiss it.
I tried several things, such as showing the action sheet after a short delay or dismissing the alert view programmatically, but nothing worked. In the best case (dismissing the alert view programmatically) the alert view did disappear after a somewhat-strange transition but I got a "wait-fence failed to receive reply" error in the log when it did.
How can I show an action sheet from an alert view in an orderly manner?
In this case, you should use
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
method rather than,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
so your code wil be:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIActionSheet *actionSheet = ...
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}
}
Thanks,
Just call dismissWithClickedButtonIndex:animated: method for UIAlertView
if (buttonIndex == 0)
{
[alertView dismissWithClickedButtonIndex:0 animated:YES];
UIActionSheet *actionSheet = ...
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}

Linking UIActionSheet buttons with a current IBAction

I have got created a UIActionsheet programatically however I can't figure out how to link it to an existing IBAction.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
self.label.text = #"Destructive Button Clicked";
}}
This is my code where i click on a button and I want it to link to the following IBAction that i have already got.
- (IBAction)doButton;
So, how would i go about linking this? Any help would be greatly appreciated.
Thanks
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
if (buttonIndex == 0) {
self.label.text = #"Destructive Button Clicked";
[self doButton];
}
}
The best way to do it is to add a UIActionSheetDelegate to your class and call the action sheet in your IBAction.
.h file
#interface ViewController : UIViewController <UIActionSheetDelegate>
-(IBAction)doButton;
#end
.m file
-(IBAction)doButton {
UIActionSheet *doSheet = [[UIActionSheet alloc] initWithTitle:#"Your title" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Button 0", #"Button 1", nil];
[doSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
if (buttonIndex == 0) {
//Do Something
}
if(buttonIndex == 1) {
//Do Something else
}
}

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.

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
}
}