码迷,mamicode.com
首页 > 移动开发 > 详细

IOS自定义弹出提示框

时间:2016-04-28 23:52:06      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

采用代理的设计思想。

1,创建一个视图类.h

#import <UIKit/UIKit.h>

@protocol CustomaltviewDelegate;

@protocol CustomaltviewDelegate <NSObject>
- (void)alertview:(id)altview clickbuttonIndex:(NSInteger)index;
@end

@interface Customaltview : UIView<CustomaltviewDelegate>

@property(nonatomic,strong)UIView *view;
@property(nonatomic,assign)float altHeight;
@property(nonatomic,assign)float altwidth;
@property(nonatomic,weak)id<CustomaltviewDelegate>delegate;

- (void)creatAltWithAltTile:(NSString*)title content:(NSString*)content;
- (void)show;
- (void)hide;

@end

2,实现自定义的的视图.m

#import "Customaltview.h"

@implementation Customaltview


- (void)creatAltWithAltTile:(NSString *)title content:(NSString *)content{
    _view = [[UIView alloc] init];
    UILabel *altTitleLabel = [[UILabel alloc] init];
    altTitleLabel.text = title;
    [altTitleLabel setTextAlignment:NSTextAlignmentCenter];
    [altTitleLabel setFont:[UIFont systemFontOfSize:16]];
    [altTitleLabel setTextColor:[UIColor redColor]];
    [altTitleLabel setFrame:CGRectMake(0, 0, _altwidth, 30)];
    [_view addSubview:altTitleLabel];
    
    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.0]];
    [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];
    
        UIButton *altbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [altbtn setTitle:@"取消" forState:UIControlStateNormal];
        [altbtn setBackgroundColor:[UIColor grayColor]];
        [altbtn setTag:0];
        [altbtn setFrame:CGRectMake((_altwidth/2-50)/2, _altHeight, 50, 35)];
        [altbtn addTarget:self action:@selector(checkbtn:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *altbut1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [altbut1 setTitle:@"确认" forState:UIControlStateNormal];
        [altbut1 setBackgroundColor:[UIColor grayColor]];
        [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:altbtn];
        [_view addSubview:altbut1];
        _altHeight+=50;
    
    [_view setFrame:CGRectMake((320-_altwidth)/2, ([UIScreen mainScreen].bounds.size.height-_altHeight)/2-64, _altwidth , _altHeight)];
    [_view setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0]];
}

#pragma Delegate
-(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index
{
    [_delegate alertview:self clickbuttonIndex:index];
}
#pragma SELECTOR
-(void)handleSingleTap:(UITapGestureRecognizer *)sender
{
    [self hide];
}
-(void)checkbtn:(UIButton *)sender
{
    [_delegate alertview:self clickbuttonIndex:sender.tag];
}
#pragma Instance method
-(void)show
{
    if(_view==nil)
    {
        _view=[[UIView alloc] init];
    }
    [_view setHidden:NO];
}
-(void)hide
{
    if(_view==nil)
    {
        _view=[[UIView alloc] init];
    }
    [_view setHidden:YES];
}

@end

3,引用这个类,需要引用类进来,遵守协议,执行协议方法

#import "ViewController.h"
#import "Customaltview.h"
#import "BViewController.h"
#import "CViewController.h"

@interface ViewController ()<CustomaltviewDelegate>
@property(nonatomic,strong)Customaltview *alt;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        _alt=[[Customaltview alloc]init];
        _alt.delegate=self;
        _alt.altwidth=250.0f;
    
        [_alt creatAltWithAltTile:@"警告" content:@"我是一个警告框,快来看!!!"];
        
        [self.view addSubview:_alt.view];
        [_alt show];
}

-(void)alertview:(id)altview clickbuttonIndex:(NSInteger)index
{
    if (index == 0) {
        [self presentViewController:[BViewController new] animated:YES completion:^{
    }];
        NSLog(@"cancel");
    }else{
        [self presentViewController:[CViewController new] animated:YES completion:^{
        }];
        NSLog(@"yes");
    }
    [_alt hide];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

 

IOS自定义弹出提示框

标签:

原文地址:http://www.cnblogs.com/garywong1949/p/5444611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!