由于项目中有这样一个需求:需要在保存是弹出框选择保存的地点。选择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];

差不多就是这样了。

iphone:自定义UIAlertView的更多相关文章

  1. IOS7学习之路九(ios7自定义UIAlertView)

    IOS7的UIAlertView 不支持自定义,无法添加subview . 不过可以用第三方库git上的下载链接    https://github.com/wimagguc/ios-custom-a ...

  2. 自定义UIAlertView

    You can change accessoryView to any own customContentView in a standard alert view in iOS7 [alertVie ...

  3. iphone自定义铃声

    Step1:下载iTunes Step2:连接手机登录iTunes并授权将音乐文件添加到资料库,修改音乐时间长度为40s Step3:在主界面选择音乐标签 Step4:选择一个mp3音乐文件,点击文件 ...

  4. iTunes 无法添加 iPhone 自定义铃声

    本篇文章由:http://xinpure.com/itunes-unable-to-add-iphone-custom-ringtones/ 新版本 iTunes 需要在 菜单栏 -> 文件 中 ...

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

    菜鸟一枚,大神勿喷.自己在牛刀小试的时候,发现系统的UIAlertView有点不喜欢,然后就自己自定义了一个UIAlertView,基本上实现了系统的UIAlertView,可以根据项目的需求修改UI ...

  6. iPhone开发 - 常用库

    iPhone开发 - 常用库 这里总结了iPhone开发者开发过程中可能需要的一些资源 如何用Facebook graphic api上传视频: http://developers.facebook. ...

  7. iPhone开发资源汇总

    如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...

  8. (转) iphone开发资源汇总

    如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...

  9. iOS自定义多参数类型方法

    前几天做自定义UIAlertView的时候,想仿造系统自带的初始化方法做一个AlertView,里面涉及到不确定多参数的设置和使用问题.这里做一下记录. 我自定义了一个方法: - (instancet ...

随机推荐

  1. MySql按指定天数进行分组数据统计分析 1

    这几天,在做数据统计,在对数据库数据进行统计过程中,有个需求就是要按照指定天数进行分组, 之前一直没有找到好的方法,就先取出数据,在程序中进行分组. 后发现,可以在SQL语句中实现按天数分组. 例: ...

  2. Javascript获取当前时间戳的方法

    定义日期:   Date 对象用于处理日期和时间.   可以通过 new 关键词来定义 Date 对象.以下代码定义了名为 myDate 的 Date 对象:   var myDate=new Dat ...

  3. opencv如何截取子图像

     首先用GetSubRect函数确定子图像的区域 GetSubRect 返回输入的图像或矩阵的矩形数组子集的矩阵头 CvMat* cvGetSubRect( const CvArr* arr, CvM ...

  4. java猜数字小游戏

    /* * * 猜数字小游戏 * * 先由系统生成一个2-100之间的随机数字, * * 然后捕获用户从控制台中输入的数字是否与系统生成的随机数字相同, * * 如果相同则统计用户所猜的次数,并给出相应 ...

  5. 谈谈ILDasm的功能限制与解除

    原文:谈谈ILDasm的功能限制与解除 首先,我在此申明,此文并不是教别人突破限制,我们只是用学习的眼光看问题 大家都知道ILDasm是.NET程序的反编译工具,它是由Microsoft提供的反编译工 ...

  6. ubuntu_安装aptana3

    下面记录下偶怎么安装aptana3(aptana2应该也适用). 安装java运行时,偷看这里 说明:实际上偶并没有执行这步,因为发现在安装aptana3之前 java的运行时已经安装过了. 貌似是安 ...

  7. java 数字前自动补零实现

    /** * 里数字转字符串前面自动补0的实现. * */ public class TestStringFormat { public static void main(String[] args) ...

  8. nginx-gridfs 的安装配置和使用

    (一)安装nginx前的准备 安装nginx需要安装openssl和pcre,具体安装步骤请参考nginx安装的相关博文 (二)nginx和nginx-gridfs 联合编译安装 nginx-grid ...

  9. ubuntu KDE/GNOME vnc

  10. 将Controller中的数据传递到View中显示

    如何将Controller 中的数据传送到View 步骤: (1)要有数据,如果要用到对象可以在Model 中定义对应的类 (2)要有装数据的容器: System.Text.StringBuilder ...