适用iOS 7的自定义弹窗(uialertview)

今天心血来潮,突然想实现个功能,随时随地的调用类试图,现实需要的结果调用。所以就试着写了这么个demo.此demo尚有诸多不足之处。

.h:

#import <UIKit/UIKit.h>
@protocol Customaltviewdelete;


@protocol Customaltviewdelete <NSObject>
默认必须实现
//-(void)showmessage;
//
@required 必须实现
@optional///表示不必须实现
-(void)showmessage;
-(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index;

@end

@interface CustomAlertview : UIView<Customaltviewdelete>
{
//   id <Customalertdelete> _delegate;
 
    float altheight;

}
//初始化显示结果
-(void)CreatAlt:(UIImage*)backimg altTitle:(NSString *)Title Content:(NSString *)content altButton:(NSArray *)altbtnArray altbtnTcolor:(UIColor *)color altselectbtnTcolor:(UIColor *)selectcolor;
//显示
-(void)show;
//隐藏
-(void)hide;
@property(nonatomic)  float  altwidth;
@property(nonatomic,retain)  UIView *view;
@property(nonatomic,assign)id<Customaltviewdelete> deleta;

@end
.m

#import "CustomAlertview.h"

@implementation CustomAlertview
{
 
}
@synthesize view;
@synthesize altwidth;
@synthesize deleta;

-(void)CreatAlt:(UIImage*)backimg altTitle:(NSString *)Title Content:(NSString *)content altButton:(NSArray *)altbtnArray altbtnTcolor:(UIColor *)color altselectbtnTcolor:(UIColor *)selectcolor
{
    
    view=[[UIView alloc] init];
      UILabel *alttitle=[[UILabel alloc] init];
    alttitle.text=Title;
    [alttitle setTextAlignment:NSTextAlignmentCenter];
    [alttitle setFont:[UIFont systemFontOfSize:16]];
    [alttitle setTextColor:[UIColor  redColor]];
    [alttitle setFrame:CGRectMake(0, 0, altwidth, 30)];
    [view addSubview:alttitle];
    
    UIImageView *Ximg=[[UIImageView alloc] initWithFrame:CGRectMake(altwidth-20, 0, 20, 20)];
    [Ximg setImage:[UIImage imageNamed:@"1.png"]];
    [Ximg setUserInteractionEnabled:YES];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [Ximg addGestureRecognizer:singleTap];
    [view addSubview:Ximg];
    
    
    UILabel *altcontent=[[UILabel alloc] init];
    [altcontent setText:content];
    [altcontent setFont:[UIFont systemFontOfSize:12.0f]];
    [altcontent setTextAlignment:NSTextAlignmentLeft];
    [altcontent setTextColor:[UIColor colorWithRed:0.3f green:0.3f blue:0.3f alpha:1]];
    [altcontent setLineBreakMode:NSLineBreakByCharWrapping];
 
    CGSize size=[altcontent.text  sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(altwidth-20, 700.0f) lineBreakMode:NSLineBreakByCharWrapping ];
    [altcontent setFrame:CGRectMake(10, 30,altwidth-20, (int)size.height+5)];
      altcontent.numberOfLines = (int)size.height/20+1;
    altheight=35+altcontent.frame.size.height;
    [view addSubview:altcontent];
    
  if(altbtnArray==nil)
  {
      
      UIButton *altbut=[UIButton buttonWithType:UIButtonTypeCustom];
      [altbut setTitle:@"取消" forState:UIControlStateNormal];
      [altbut setTitle:@"取消" forState:UIControlStateHighlighted];
      [altbut setTitleColor:color forState:UIControlStateNormal];
      [altbut setTitleColor:selectcolor forState:UIControlStateHighlighted];
      [altbut setBackgroundColor:[UIColor blueColor]];
      [altbut setTag:0];
      [altbut setFrame:CGRectMake((altwidth/2-50)/2, altheight, 50, 35)];
      [altbut addTarget:self action:@selector(checkbtn:) forControlEvents:UIControlEventTouchUpInside];
      
      
      
      UIButton *altbut1=[UIButton buttonWithType:UIButtonTypeCustom];
      [altbut1 setTitle:@"确认" forState:UIControlStateNormal];
       [altbut1 setTitle:@"确认" forState:UIControlStateHighlighted];
      [altbut1 setTitleColor:color forState:UIControlStateNormal];
      [altbut1 setTitleColor:selectcolor forState:UIControlStateHighlighted];
      [altbut1 setBackgroundColor:[UIColor blueColor]];
      [altbut1 setTag:1];
      [altbut1 setFrame:CGRectMake(altwidth/2+(altwidth/2-50)/2, altheight, 50, 35)];
      [altbut1 addTarget:self action:@selector(checkbtn:) forControlEvents:UIControlEventTouchUpInside];
      
      [view addSubview:altbut];
      [view addSubview:altbut1];
      altheight+=50;
  }
  else
  {
    
      
  }
    
   [view setFrame:CGRectMake((320-altwidth)/2, ([UIScreen mainScreen].bounds.size.height-altheight)/2-64, altwidth , altheight)];
    if(backimg==nil)
    {
        [view setBackgroundColor:[UIColor grayColor]];
    }
    else
    {
      [view setBackgroundColor:[UIColor colorWithPatternImage:backimg]];
    }
 
 
}

 
-(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index
{
    NSLog(@"abcderfv");
        [deleta alertview:self clickbuttonIndex:index];
}
-(void)showmessage
{
    NSLog(@"asd");
}
-(void)handleSingleTap:(UITapGestureRecognizer *)sender
{
    [self hide];
}
-(void)checkbtn:(UIButton *)sender
{
    [deleta showmessage];
    [deleta alertview:self clickbuttonIndex:sender.tag];
//    [self hide];
}

-(void)show
{
    if(view==nil)
    {
        view=[[UIView alloc] init];
    }
    [view setHidden:NO];
}
-(void)hide
{
    if(view==nil)
    {
        view=[[UIView alloc] init];
    }
    [view setHidden:YES];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
 接下来,就是调用了;

*.h 这里要适用协议Customaltviewdelete 引用类文件 定义类对象 alt

*.m 在需要调用的地方添加这段代码 初始化对象

    if(alt==nil)
    {
    alt=[[CustomAlertview alloc]init];
        alt.deleta=self;
        alt.altwidth=250.0f;
    
    [alt CreatAlt:nil altTitle:@"XXXXXXXX" Content:@"asd阿速度发送地方fa阿萨德发速度发送地方sd阿萨德发生的发速度发送地方f速度发送地方X阿速度发送地方" altButton:nil altbtnTcolor:[UIColor redColor] altselectbtnTcolor:[UIColor whiteColor] ];
 
    [self.view addSubview:alt.view];
    [alt show];
    }
    else
    {
        [alt show];
    }
    
    return;

在实现委托事件
-(void)showmessage
{
    NSLog(@"bbb");
}
-(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index
{
    NSLog(@"asdfasdf %d",index);
    [alt hide];
}

 小demo 大功告成。适合新手!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值