iphone:自定義UIAlertView


由於項目中有這樣一個需求:需要在保存是彈出框選擇保存的地點。選擇UIAlertView來實現,但是要在UIAlertView中增加UISwitch的控件,這就需要自定義一個繼承UIAlertView的類來自定義UIAlertView了。

實現效果如下:(還沒加圖的)

我需要在點擊確定的時候,知道兩個Switch的狀態,才能進一步做相應的功能。

自定義了SaveAlertView類。

在.h中,需要自定義一個@protocol,作為把switch狀態傳出去的出口。

聲明相應的委托。看源碼

#import <UIKit/UIKit.h>

@protocol SaveAlertViewDelegate <NSObject>

@optional
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex save2File:(BOOL) save2File save2Album:(BOOL) save2Album;
@end


@interface SaveAlertView : UIAlertView

@property(nonatomic, assign) id<SaveAlertViewDelegate> SaveAlertDelegate;
@property(readwrite, retain) UIImage *backgroundImage;
@property(readwrite, retain) UIImage *contentImage;

@property(strong, nonatomic) IBOutlet UISwitch *switch1;
@property(strong, nonatomic) IBOutlet UISwitch *switch2;

- (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content;

@end

在.m中主要是把系統的原來控件隱藏掉(在layoutSubviews中實現),在添加自己控件,及其點擊相應代碼。

在layoutSubviews中隱藏系統的控件

    for (UIView *v in [self subviews]) {
        if ([v class] == [UIImageView class]){
            [v setHidden:YES];
        }     
        if ([v isKindOfClass:[UIButton class]] ||
            [v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {
            [v setHidden:YES];
        }
    }

看完整的.m代碼

#import "SaveAlertView.h"

@implementation SaveAlertView
@synthesize SaveAlertDelegate;
@synthesize backgroundImage;
@synthesize contentImage;
@synthesize switch1;
@synthesize switch2;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content{
    if (self == [super init]) {
        self.backgroundImage = image;
        self.contentImage = content;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGSize imageSize = self.backgroundImage.size;
    [self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
}

- (void) layoutSubviews {
    //屏蔽系統的ImageView 和 UIButton
    for (UIView *v in [self subviews]) {
        if ([v class] == [UIImageView class]){
            [v setHidden:YES];
        }
        
        
        if ([v isKindOfClass:[UIButton class]] ||
            [v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {
            [v setHidden:YES];
        }
    }    
    if (contentImage) {
        UIImageView *contentview = [[UIImageView alloc] initWithImage:self.contentImage];
        contentview.frame = CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height);
        [self addSubview:contentview];
    }
    
    UILabel *label1 = [[UILabel alloc]  initWithFrame:CGRectMake(20, 20, 136, 21)];
    label1.text = @"保存為可編輯文件";
    [self addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc]  initWithFrame:CGRectMake(20, 65, 85, 21)];
    label2.text = @"另存到相冊";
    [self addSubview:label2];
    
    switch1 = [[UISwitch alloc] initWithFrame:CGRectMake(206, 17, 79, 27)];
    [switch1 setOn:YES];
    [self addSubview:switch1];
    
    switch2 = [[UISwitch alloc] initWithFrame:CGRectMake(206, 62, 79, 27)];
    [self addSubview:switch2];
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 118, 86, 36)];
    button1.tag = 1;
    [button1 setTitle:@"確定" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor blueColor];
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button1];
    
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(199, 118, 86, 36)];
    button2.tag = 2;
    [button2 setTitle:@"取消" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor blueColor];
    [button2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button2];
    
    self.backgroundColor = [UIColor grayColor];
}

-(void) buttonClicked:(id)sender
{
    UIButton *btn = (UIButton *) sender;
    
    if (SaveAlertDelegate) {
        if ([SaveAlertDelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
        {
            [SaveAlertDelegate alertView:self clickedButtonAtIndex:btn.tag save2File:[switch1 isOn] save2Album:[switch2 isOn]];
        }
    }
    
    [self dismissWithClickedButtonIndex:0 animated:YES];
    
}

- (void) show {
    [super show];
//    CGSize imageSize = self.backgroundImage.size;
//    self.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height);
    self.frame = CGRectMake(350, 300, 320, 191);
}
@end

然后是調用,不要忘記設置委托

            SaveAlertView *savealert = [[SaveAlertView alloc] initWithFrame:CGRectMake(340, 221, 305, 191)];
            savealert.SaveAlertDelegate = self;
            [savealert show];

 

差不多就是這樣了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM