gpt4 book ai didi

iphone - 如何自定义UIActionSheet中的按钮?

转载 作者:行者123 更新时间:2023-12-03 18:45:20 24 4
gpt4 key购买 nike

我想更改UIActionSheet按钮图像,每个都有图像 >按钮,我希望每个按钮显示我想要的图像。我在网上搜索了很多答案,但都不起作用。

这里是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];

}

最佳答案

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

我像 UIActionSheet 一样使用 CustomActionSheet,只是我必须在 willPresentActionSheet 委托(delegate)中调用子类的 customGUI 方法:

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


(自定义操作表.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的想法

关于iphone - 如何自定义UIActionSheet中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6568308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com