ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

iphone:自定义UIAlertView

2019-06-29 19:24:11  阅读:315  来源: 互联网

标签:UIAlertView alloc 自定义 initWithFrame self CGRectMake backgroundImage iphone UIBut


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

 

差不多就是这样了。

转载于:https://www.cnblogs.com/haibosoft/p/4257062.html

标签:UIAlertView,alloc,自定义,initWithFrame,self,CGRectMake,backgroundImage,iphone,UIBut
来源: https://blog.csdn.net/weixin_34343000/article/details/94193173

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有