如何在UIActionSheet中自定义按钮? [英] How to customize Buttons in UIActionSheet?

查看:76
本文介绍了如何在UIActionSheet中自定义按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改图片按钮 UIActionSheet ,每个按钮我有 images ,我想要每个按钮显示我想要的图片。我在网上搜索了很多答案,但是它们没有用。

I want to change the image of the buttons of UIActionSheet, I have images for each button, and I want each button show the image I want. I searched the web I found a lot of answers but they didn't work.

这是 UIActionSheet

-(IBAction)showActionSheet:(id)sender {

    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:nil otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
    [popupQuery showInView:self.view];
    [popupQuery release];

}


推荐答案

我创建一个子类CustomActionSheet派生了UIActionSheet,并实现了一个名为customizeGUI的方法。


我使用像UIActionSheet这样的CustomActionSheet,除了我必须在willPresentActionSheet委托中调用子类的方法customizeGUI:

I create a subclass CustomActionSheet derived UIActionSheet, and implement a method called customizeGUI.

I use CustomActionSheet like UIActionSheet, except I must call method customizeGUI of subclass in willPresentActionSheet delegate:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    if ([actionSheet isKindOfClass:[CustomActionSheet class]]) {
        CustomActionSheet *sheet = (CustomActionSheet*)actionSheet;
        [sheet customizeGUI];
    }
}



(CustomActionSheet.m )


(CustomActionSheet.m)

- (void)customizeGUI {
    UIImageView *imgvwBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_actionsheet.png"]];
    CGRect rect = self.bounds;
    imgvwBackground.frame = rect;
    [self insertSubview:imgvwBackground atIndex:0];
    [imgvwBackground release];

    int buttonIndex = 0;
    UIImage *imgButtonDestructive = [UIImage imageNamed:@"btn_actionsheet_destructive.png"];
    UIImage *imgButtonCancel = [UIImage imageNamed:@"btn_actionsheet_cancel.png"];
    UIImage *imgButtonNormal = [UIImage imageNamed:@"btn_actionsheet_normal.png"];
    for (UIView *sub in self.subviews) {
        NSString *className = [NSString stringWithFormat:@"%@", [sub class]];
        if ( ([className isEqualToString:@"UIThreePartButton"]) //iOS 4
            || ([className isEqualToString:@"UIAlertButton"]) ) { //iOS 5
            rect = sub.frame;
            sub.hidden = YES;

            UIButton *btn = [[UIButton alloc] initWithFrame:rect];
            UIImage *imgForButton = nil;
            if (buttonIndex == self.cancelButtonIndex) {
                imgForButton = imgButtonCancel;
            }
            else if (buttonIndex == self.destructiveButtonIndex) {
                imgForButton = imgButtonDestructive;
            }
            else {
                imgForButton = imgButtonNormal;
            }
            NSString *sTitle = [self buttonTitleAtIndex:buttonIndex];
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
            [btn setBackgroundImage:imgForButton forState:UIControlStateNormal];
            [btn setTitle:sTitle forState:UIControlStateNormal];
            btn.tag = buttonIndex;
            [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:btn];
            [btn release];

            buttonIndex++;
        }
    }
}

- (void)buttonClicked:(id)sender {
    UIButton *btn = sender;
    if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
        [self.delegate actionSheet:self clickedButtonAtIndex:btn.tag];
    }
    [self dismissWithClickedButtonIndex:btn.tag animated:YES];
}

希望通过这种方式可以帮助您并为您提供自定义UIActionSheet的想法

Hope this way can help you and give you an idea to customize UIActionSheet

这篇关于如何在UIActionSheet中自定义按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆