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];

差不多就是这样了。

时间: 2024-07-30 13:43:33

iphone:自定义UIAlertView的相关文章

自定义UIAlertView

code4App上面有很多很棒的UI特效代码,我们常感叹,好牛逼啊,这效果,太炫了,哇,怎么自己写不出来.其实,再炫的特效,都是根据苹果系统的框架而来,如果我们了解系统框架实现的原理,也就能写出属于自己自定义的控件,加上各种各样的动画. 这里,我就展示一个自定义的UIAlertView效果控件,视图出现的时候动画-先放大-再缩小-最后成正常比例,消失的时候缩小加渐隐.调用也很方便,不需要像系统先创建后[alert show],我在类内部就已经写好了,只需要alloc创建,调用各个按钮对应的响应b

自定义UIAlertView可以容纳多个按钮

下载:FSAlertView Customization UIView replace UIAlertView can include a lot of Buttons,the FSAlertView would be placed between Top Bar and Bottom Bar and automatically scroll well. Features Works like UIAlertView.So does the Init,Delegate. Runs on iOS5

iphone自定义铃声

Step1:下载iTunes Step2:连接手机登录iTunes并授权将音乐文件添加到资料库,修改音乐时间长度为40s Step3:在主界面选择音乐标签 Step4:选择一个mp3音乐文件,点击文件-转换-创建aac版本 Step5:将生成的后缀名.m4a文件改为.m4r文件 Step6:在主界面切换音乐选项卡为铃声选项卡,如果没有铃声选项卡则编辑菜单选择显示铃声选项卡 Step7:将上面生成为文件拖到iTunes中 Step8:在主界面依次选择文件-设备-同步即可 Step9:在手机中设置中

iTunes 无法添加 iPhone 自定义铃声

本篇文章由:http://xinpure.com/itunes-unable-to-add-iphone-custom-ringtones/ itunes 添加自定义铃声步骤 打开 itunes,选择菜单栏的 文件 - 将文件添加到资料库,选择要做铃声的歌曲,单击 打开 歌曲会出现在 资料库 的 音乐 里右击歌曲,选择 显示简介 选择 选项 填上 起始时间 和 停止时间 (在40秒以内),确定 再右击歌曲,选择 创建ACC版本 出现同样文件名,但时长在40秒以内的文件 右击新创建的歌曲,选择 在

iOS自定义提示弹出框(类似UIAlertView)

菜鸟一枚,大神勿喷.自己在牛刀小试的时候,发现系统的UIAlertView有点不喜欢,然后就自己自定义了一个UIAlertView,基本上实现了系统的UIAlertView,可以根据项目的需求修改UIAlertView的颜色.欢迎大神多多指导.不胜感激! 效果图: Paste_Image.png --------------直接贴代码---------不喜勿喷----------大神多多指导.不胜感激!------- #import <UIKit/UIKit.h> typedef void(^

转:常用的几个提高iOS开发效率的开源类库及工具

常用的几个提高iOS开发效率的开源类库及工具 iOS开发者 : 开发几个常用的开源类库及下载地址: 算上这个连接:http://wenku.baidu.com/view/bbeb0091dd88d0d233d46a00.html 1.json json编码解码2.GTMBase64 base64编码解码3.TouchXML xml解析4.SFHFKeychainUtils 安全保存用户密码到keychain中 5.MBProgressHUD 很棒的一个加载等待特效框架6.ASIHTTPReque

IOS开发-你不可缺少的资源汇总-知识分享-转

如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://github.com/carlbrown/PDKeychainBindingsController 对焦功能的实现: http://www.clingmarks.com/?p=612 自定义圆角Switch按件: https://github.com/domesticcatsoftware/DCRou

IOS地址

IOS开发-你不可缺少的资源汇总-知识分享-转 如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://github.com/carlbrown/PDKeychainBindingsController 对焦功能的实现: http://www.clingmarks.com/?p=612 自定义圆角Switch按件: https://github.com/d

常用的几个提高iOS开发效率的开源类库及工具

转自 iOS开发者 : 几个常用的开源类库及下载地址: 算上这个连接:http://wenku.baidu.com/view/bbeb0091dd88d0d233d46a00.html 1.json json编码解码2.GTMBase64 base64编码解码3.TouchXML xml解析4.SFHFKeychainUtils 安全保存用户密码到keychain中 5.MBProgressHUD 很棒的一个加载等待特效框架6.ASIHTTPRequest http等相关协议封装7.EGORef