1

このチュートリアルを使用して、カスタム アラート ビューを実装しようとしています。このチュートリアルではすべてうまく機能しますが、UIAlertButton のカスタマイズについては触れていません。次に、layoutSubviews メソッドを使用してカスタマイズしようとしています。

- (void)layoutSubviews
{
    for (UIView *subview in self.subviews){ //Fast Enumeration
        if ([subview isMemberOfClass:[UIImageView class]]) {
            subview.hidden = YES; //Hide UIImageView Containing Blue Background
        }
        if ([subview isMemberOfClass:[UILabel class]]) { //Point to UILabels To Change Text
            UILabel *label = (UILabel*)subview; //Cast From UIView to UILabel
            label.textColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0f];
            label.shadowColor = [UIColor blackColor];
            label.shadowOffset = CGSizeMake(0.0f, 1.0f);
        }
        if ([subview isMemberOfClass:[UIAlertButton class]]) {
            // do blablabla
        }
    }
}

特に私が追加したのは:

if ([subview isMemberOfClass:[UIAlertButton class]]) {
     // do blablabla
}

しかし、UIAlertButton クラスが見つかりません。なぜですか? ありがとう

編集: nsguliver のおかげで、これが私の最終的な作業コードです。

CustomAlertView.h

#import <UIKit/UIKit.h>

@interface CustomAlertView: UIAlertView {
    NSMutableArray *fakeButtonIndexList;
    NSMutableArray *buttonList;
}

-(void)addCustomButton:(NSString*)title;

@end

CustomAlertView.m

#import "CustomAlertView.h"

@implementation CustomAlertView


-(id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];

    if (self) {
        buttonList = [NSMutableArray array];
        fakeButtonIndexList = [NSMutableArray array];
    }

    return self;
}


-(void)layoutSubviews {
    for (UIView *subview in self.subviews){ //Fast Enumeration

        // Image de fond
        if ([subview isMemberOfClass:[UIImageView class]]) {
            subview.hidden = YES; //Hide UIImageView Containing Blue Background
        }
        // Label
        if ([subview isMemberOfClass:[UILabel class]]) { //Point to UILabels To Change Text
            UILabel *label = (UILabel*)subview; //Cast From UIView to UILabel
            label.shadowColor = [UIColor blackColor];
            label.shadowOffset = CGSizeMake(0.0f, 1.0f);
        }
    }
}

-(void)drawRect:(CGRect)rect {
    //////////////GET REFERENCE TO CURRENT GRAPHICS CONTEXT
    CGContextRef context = UIGraphicsGetCurrentContext();

    //////////////CREATE BASE SHAPE WITH ROUNDED CORNERS FROM BOUNDS
    CGRect activeBounds = self.bounds;
    CGFloat cornerRadius = 10.0f;
    CGFloat inset = 6.5f;
    CGFloat originX = activeBounds.origin.x + inset;
    CGFloat originY = activeBounds.origin.y + inset;
    CGFloat width = activeBounds.size.width - (inset*2.0f);
    CGFloat height = activeBounds.size.height - (inset*2.0f);

    CGRect bPathFrame = CGRectMake(originX, originY, width, height);
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:bPathFrame cornerRadius:cornerRadius].CGPath;

    //////////////CREATE BASE SHAPE WITH FILL AND SHADOW
    CGContextAddPath(context, path);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:40.0f/255.0f green:40.0f/255.0f blue:40.0f/255.0f alpha:1.0f].CGColor);
    CGContextDrawPath(context, kCGPathFill);

    //////////////CLIP STATE
    CGContextSaveGState(context); //Save Context State Before Clipping To "path"
    CGContextAddPath(context, path);
    CGContextClip(context);

}

-(void)show {
    [super show];

    // On redéfinit les frames des boutons
    NSInteger indexCustomButton = 0;
    for (NSString *indexStr in fakeButtonIndexList) {

        UIButton *customButton = (UIButton*)[buttonList objectAtIndex:indexCustomButton];
        UIButton *fakeButton = (UIButton*)[self.subviews objectAtIndex:[indexStr integerValue]];
        [customButton setFrame:fakeButton.frame];

        indexCustomButton++;
    }
}

-(void)addCustomButton:(NSString *)title {
    [self addButtonWithTitle:title];
    UIButton *fakeButton = [self.subviews lastObject];
    [fakeButton setHidden:YES];
    [fakeButtonIndexList addObject:[NSString stringWithFormat:@"%d",[self.subviews count]]];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:fakeButton.frame];
    [button setTitle:title forState:nil];
    [button setBackgroundColor:[Templates getColor:@"color"]];
    [button.layer setCornerRadius:5.0f];

    [buttonList addObject:button];
    [self addSubview:button];
}

@end
4

3 に答える 3

2

UIAlertView最初に両方のボタンのタイトルを保持してからnilカスタムボタンを追加することで、ボタンをカスタマイズする1つの方法 は、以下のコードを自分でテストしたことを確認subviewしてください。CustomAlertView

CustomAlertVIew *alert =[[CustomAlertVIew alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];

// add the custom button
UIImage *buttonImage = [UIImage imageNamed:@"image.png"];

//create the button and assign the image
UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.alertButton setFrame:CGRectMake(x,y,buttonImage.size.width,buttonImage.size.height)];

[alertButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
alertButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[alertButton setTitle:@"Done" forState:UIControlStateNormal];
[alertButton addTarget:self action:@selector(backMethod)  forControlEvents:UIControlEventTouchUpInside];

[alert addSubview:alertButton];
[alert show];
于 2013-03-05T16:11:14.947 に答える
0

あなたの質問はすでに十分に回答されていますが、私の解決策はおそらく短くてきれいです。まず、subviews-array 内のボタン インデックスを追跡する変数を作成しました。AlertViewButtons が最後のサブビューであると仮定します。

int buttonIndexInSubviews = (self.subviews.count - self.numberOfButtons);

次に、示されているようにサブビューを繰り返し処理しましたが、if ステートメントは異なる形式になっています。秘訣は、AlertViewButton をスーパークラスにキャストすることです。

if (subview == [self.subviews objectAtIndex:buttonIndexInSubviews]) {
        UIButton *button = (UIButton*)subview;

        //Here you can change the button appearance via the variable
        //E.g. button.titleLabel.shadowOffset = CGSizeMake(0.0f, 0.0f);

        buttonIndexInSubviews++;
    }

ちなみに、この回避策は Apple によって承認されています。

于 2013-07-27T12:30:39.767 に答える
0

または、次のように UIAlertButton クラスをテストできます。

if ([subview isMemberOfClass:NSClassFromString(@"UIAlertButton")]) {
    // do your customizing here...
}
于 2013-08-22T15:40:05.203 に答える