zoukankan      html  css  js  c++  java
  • IOS自定义弹出提示框

    采用代理的设计思想。

    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

     

  • 相关阅读:
    Python3---内建函数---all()
    (dp)Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
    (状压dp)codevs2800 送外卖
    (dp)CF 813 Educational Codeforces Round 22 D. Two Melodies
    (线段树)CF813 Educational Codeforces Round 22 E
    (trie)HDU1251 统计难题
    (最大流)CodeForces
    (高斯消元)HDU2827 The Evaluation of Determinant
    (三分)HDU5531 Rebuild
    (并查集)Codeforces 325 D-Reclamation
  • 原文地址:https://www.cnblogs.com/garywong1949/p/5444611.html
Copyright © 2011-2022 走看看