IOS自訂彈出提示框

來源:互聯網
上載者:User

標籤:

採用代理的設計思想。

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自訂彈出提示框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.