Showing posts with label UIActionSheetDelegate example. Show all posts
Showing posts with label UIActionSheetDelegate example. Show all posts

Monday, June 17, 2013

UIActionSheetDelegate willPresentActionSheet example in Objective C (iOS).


UIActionSheetDelegate willPresentActionSheet

Sent to the delegate before an action sheet is presented to the user.

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet

Parameters of [UIActionSheetDelegate willPresentActionSheet]
actionSheet
The action sheet that is about to be displayed.

UIActionSheetDelegate willPresentActionSheet example.
I assume you have a class which implements the UIActionSheetDelegate protocol and your class is a delegate class of the current UIActionSheet, so in that class you can change the whole title of the UIActionSheet like

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    for (UIView *_currentView in actionSheet.subviews) {
        if ([_currentView isKindOfClass:[UILabel class]]) {
            [((UILabel *)_currentView) setFont:[UIFont boldSystemFontOfSize:15.f]];
        }
    }
}

Example of [UIActionSheetDelegate willPresentActionSheet].
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{

UIImageView* df = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourpic.png"]];
df.center

[actionSheet addSubview:df];
//scale image aspect to fit image view
df.contentMode = UIViewContentModeScaleAspectFit;
//change width of frame
CGRect frame = df.frame;
frame.size.width = 50;
df.frame = frame;

} 

UIActionSheetDelegate willPresentActionSheet example.
- (void)notifyShowSuccessActionSheet:(NSNotification *) notification {

NSLog(@\"In ETITMainViewController notifyShowSuccessActionSheet\n\");

UIActionSheet *successActionSheet = [[[UIActionSheet alloc] initWithTitle:nil
delegate:self
   cancelButtonTitle:nil
  destructiveButtonTitle:nil
   otherButtonTitles:@\"Do More\", @\"Done\", @\"Another Test?\", @\"Yet Another\", nil] autorelease];
[successActionSheet setOpaque:NO];
[successActionSheet setAlpha:0.8];
[successActionSheet showFromToolbar:[[self naviController] toolbar]];

}

...and in willPresentActionSheet:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {

UIImageView* successImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"CheckMark64.png\"]];
NSInteger itemPadding;
NSInteger topPadding = [[[actionSheet subviews] objectAtIndex:0] frame].origin.y;

if ([[actionSheet subviews] count] > 1) {
itemPadding = [[[actionSheet subviews] objectAtIndex:1] frame].origin.y
- ([[[actionSheet subviews] objectAtIndex:0] frame].origin.y
   + [[[actionSheet subviews] objectAtIndex:0] frame].size.height);
}
else {
itemPadding = [[[actionSheet subviews] objectAtIndex:0] frame].size.height / 4;
}

// resize action sheet frame to make space for image
[actionSheet setFrame:CGRectMake([actionSheet frame].origin.x,
 [actionSheet frame].origin.y,
 [actionSheet frame].size.width,
 [actionSheet frame].size.height + [successImageView frame].size.height + itemPadding)];

 // re-position buttons
 for (UIControl *button in [actionSheet subviews]) {

[button setFrame:CGRectMake([button frame].origin.x,
[button frame].origin.y + [successImageView frame].size.height + itemPadding,
[button frame].size.width,
[button frame].size.height)];
}

[successImageView setFrame:CGRectMake(([[actionSheet superview] frame].size.width / 2) - [successImageView frame].size.width / 2,
  topPadding,
  [successImageView frame].size.width,
  [successImageView frame].size.height)];

[actionSheet addSubview:successImageView];

}

End of UIActionSheetDelegate willPresentActionSheet example article.

UIActionSheetDelegate didPresentActionSheet example in Objective C (iOS).


UIActionSheetDelegate didPresentActionSheet

Sent to the delegate after an action sheet is presented to the user.

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet

Parameters of [UIActionSheetDelegate didPresentActionSheet]
actionSheet
The action sheet that was displayed.

UIActionSheetDelegate didPresentActionSheet example.
You can't redraw the border in a different color, so just remove the border and add your own:

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet {
    UIView *contentView = actionSheet.superview;
    UIView *popoverView = contentView.superview;

    UIView *chromeView;
    for (UIView *v in [popoverView subviews]) {
        if (v.subviews.count == 3) {
            chromeView = v;
            break;
        }
    }

    for (UIView *backgroundComponentView in [chromeView subviews]) {
        backgroundComponentView.hidden = YES;

        CGRect componentFrame = backgroundComponentView.frame;  // add your view with this frame
    }
}
Note that this won't work in *will*PresentActionSheet since actionSheet doesn't have a superview set at that point.

Example of [UIActionSheetDelegate didPresentActionSheet].
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet{
CGRect rect = CGRectMake(0,0, 320, 480);
UIButton* anImage = [[UIButton alloc] init];
[anImage setTitle:@"GHello worl" forState:UIControlStateNormal];
[anImage setTitle:@"GHello worl dfasd" forState:UIControlStateHighlighted];
[anImage setTitle:@"GHello worl selected" forState:UIControlStateSelected];
//[anImage setBackgroundImage:[UIImage imageNamed:@"photo.png"] forState:UIControlStateNormal];
[anImage addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
rect = CGRectMake(0,0, 320, 220);
anImage.frame =rect;

[actionSheet.superview addSubview:anImage];

[anImage release];
}

End of UIActionSheetDelegate didPresentActionSheet example article.

UIActionSheetDelegate actionSheetCancel example in Objective C (iOS).


UIActionSheetDelegate actionSheetCancel

Sent to the delegate before an action sheet is canceled.

- (void)actionSheetCancel:(UIActionSheet *)actionSheet

Parameters
actionSheet
The action sheet that will be canceled.

Discussion of [UIActionSheetDelegate actionSheetCancel]
If the action sheet’s delegate does not implement this method, clicking the cancel button is simulated and the action sheet is dismissed. Implement this method if you need to perform some actions before an action sheet is canceled. An action sheet can be canceled at any time by the system—for example, when the user taps the Home button. The actionSheet:willDismissWithButtonIndex: and actionSheet:didDismissWithButtonIndex: methods are invoked after this method.

UIActionSheetDelegate actionSheetCancel example.
To be sure to receive the delegate calls of your UIActionSheet, be sure to indicate in your controller's interface declaration (.h):

@interface YourViewController : UIViewController<UIActionSheetDelegate>

@end
Then in the controller's implementation (.m) :

- (void)actionSheetCancel:(UIActionSheet *)actionSheet {

    NSLog(@"action sheet is about to be cancelled");
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSLog(@"action sheet was dismissed");
}

Example of [UIActionSheetDelegate actionSheetCancel].
-(void) showSheet:(UITabBar *)tabBar displayTitle:(NSString *)name{

    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:name
                                                      delegate:self
                                             cancelButtonTitle:@"Done"
                 destructiveButtonTitle:@"Cancel"otherButtonTitles:nil];

   [menu showFromTabBar:tabBar];
   [menu setBounds:CGRectMake(0,0,320, 700)];

}

// actionsheet delegate protocol item
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex{

    NSLog(@"button index = %d", buttonIndex);
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet{

    NSLog(@"in action canceled method");
}

End of UIActionSheetDelegate actionSheetCancel example article.

UIActionSheetDelegate actionSheet willDismissWithButtonIndex example in Objective C (iOS).


UIActionSheetDelegate actionSheet willDismissWithButtonIndex

Sent to the delegate before an action sheet is dismissed.

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex

Parameters of [UIActionSheetDelegate actionSheet willDismissWithButtonIndex]
actionSheet
The action sheet that is about to be dismissed.
buttonIndex
The index of the button that was clicked. If this is the cancel button index, the action sheet is canceling. If -1, the cancel button index is not set.

Discussion of [UIActionSheetDelegate actionSheet willDismissWithButtonIndex]
This method is invoked before the animation begins and the view is hidden.

UIActionSheetDelegate actionSheet willDismissWithButtonIndex example.
NSString * videoLink;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIActionSheet *sheet = [[UIActionSheet alloc]

videoLink = \"URL to my movie\";
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)indexPath
{
    NSLog(videoLink);
}

Example of [UIActionSheetDelegate actionSheet willDismissWithButtonIndex].
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Lanch movie player here with video url at indexPath
//NSString * yourMoviePath = @\"http://vpodcast.dr.dk/DR2/Soeinding/2009/Soeinding_0910272030.mp4\";

// Navigation logic
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * description = [[stories objectAtIndex: storyIndex] objectForKey: @\"summary\"];

UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle: NSLocalizedString(description, \"\")
delegate:self
cancelButtonTitle:NSLocalizedString(@\"Cancel\", \"\")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@\"Watch video\", \"\"), nil];
[sheet showInView:self.view];
[sheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)indexPath
{
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    switch (indexPath)
{
        case 0:
{
NSString * videoLink = @\"http://vpodcast.dr.dk/DR2/Soeinding/2009/Soeinding_0910132030.mp4\";
NSLog(@\"Playing video: %@\", videoLink);

NSURL *movieURL = [[NSURL URLWithString:videoLink] retain];

MPMoviePlayerController *DingPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[DingPlayer play];
        } break;
    }
    [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}

End of UIActionSheetDelegate actionSheet willDismissWithButtonIndex example article.

UIActionSheetDelegate actionSheet didDismissWithButtonIndex example in Objective C (iOS).


UIActionSheetDelegate actionSheet didDismissWithButtonIndex

Sent to the delegate after an action sheet is dismissed from the screen.

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

Parameters of [UIActionSheetDelegate actionSheet didDismissWithButtonIndex]
actionSheet
The action sheet that was dismissed.
buttonIndex
The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the action sheet is canceling. If -1, the cancel button index is not set.

Discussion of [UIActionSheetDelegate actionSheet didDismissWithButtonIndex]
This method is invoked after the animation ends and the view is hidden.

UIActionSheetDelegate actionSheet didDismissWithButtonIndex example.
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == [actionSheet cancelButtonIndex])
    {
       // cancelled, nothing happen
       return;
    }

    // obtain a human-readable option string
    NSString *option = [actionSheet buttonTitleAtIndex:buttonIndex];
    if ([option isEqualToString:@"Twitter"])
    {
        //...
    } else if ([option isEqualToString:@"FaceBook"])
    {
        //...
    }
}

Example of [UIActionSheetDelegate actionSheet didDismissWithButtonIndex].
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == [actionSheet cancelButtonIndex]) {
        return;
    }

    NSString *methodName = [@"action" stringByAppendingString:[actionSheet buttonTitleAtIndex:buttonIndex]];
    SEL actionMethod = NSSelectorFromString(methodName);
    if ([self respondsToSelector:actionMethod]) {
        [self performSelector:actionMethod];
    } else {
        NSLog(@"Not yet implemented")
    }
}

UIActionSheetDelegate actionSheet didDismissWithButtonIndex example.
-(void)popupActionSheet
{ 
    isActiveSupr=(BOOL)YES;
    UIActionSheet *popupQuery = [[UIActionSheet alloc]
                                 initWithTitle:@"Delete ? "
                                 delegate:self                             
                                 cancelButtonTitle:@"Cancel"
                                 destructiveButtonTitle:@"Confirm"
                                 otherButtonTitles:nil ,nil];
    popupQuery.tag = 1;
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showInView:self.tabBarController.view];
    [popupQuery release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 1)
    {
        if(buttonIndex==0)
        {
            [self send_requestDelete];          
        }
    }
    else if (actionSheet.tag == 2)
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

End of UIActionSheetDelegate actionSheet didDismissWithButtonIndex example article.