Search code examples
iosuialertviewuiactionsheet

UIAlertView not firing, after UIActionSheet is displayed


I have been searching high and low for the answer to my issue. You see I have a UIActionSheet and I am firing it off when the user tries to press a button that they aren't supposed to: it works great, but I am trying to fire of a UIAlert letting the user know the current operation has been stopped and they need to press "Continue" ... my problem is in my - (void) actionSheet .... either the method is not called by the actionSheet or the buttonIndex is not set correctly.

.h

#import <UIKit/UIKit.h>

@interface DVRRemote : UIViewController <UIActionSheetDelegate>
@property (nonatomic) BOOL pwrOff;
@property (nonatomic) BOOL dvrState;
@property (nonatomic) BOOL playState;
@property (nonatomic) BOOL rcdState;
@property (nonatomic) BOOL rcdPlay;
@property (strong, nonatomic) IBOutlet UISwitch *pwrSwitch;
@property (strong, nonatomic) IBOutlet UITextField *dvrStateLbl;
@property (strong, nonatomic) IBOutlet UITextField *PwrLbl;
- (IBAction)buttonPressed:(UIButton *)sender;
- (IBAction)pwrPressed:(UISwitch *)sender;
@end

and here is the code UIActionSheet and UIAlertView ... please help. This work is already two weeks overdue.

.m

-(IBAction)buttonPressed:(UIButton*)sender
{
    if (_pwrOff == NO)
    {
        if (_rcdPlay == NO)
        {
            if ([sender.currentTitle isEqual: @"Play"])
            {
                _dvrStateLbl.text = @"Playing";
                _playState = YES;
                _rcdState = NO;
            }
        }
        else
        {
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Play not Allowed during Record" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Continue" otherButtonTitles:nil];
            [actionSheet showInView: self.view];
        }
    }
}

-(void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [actionSheet cancelButtonIndex])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Playing" message:[NSString stringWithFormat:@"Recording stopped"] delegate:self cancelButtonTitle:@"ok"  otherButtonTitles:nil];
        [alert show];
    }
}

please note: I have coded the alert under every buttonIndex == 0, == 1, == 2 ... it does not want to show.

Big thanks to someone who can help! Regards,


Solution

  • You need to set your delegate to your view controller (the object that you want to receive your action sheet delegate methods) via:

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Play not Allowed during Record" 
        delegate:self 
        cancelButtonTitle:@"Cancel" 
        destructiveButtonTitle:@"Continue" 
        otherButtonTitles:nil];