1. 程式人生 > >iOS 自定義提示框

iOS 自定義提示框

在專案的開發中經常需要用到一些提示框,提示使用者是否確定進行某項操作。雖然系統提供了一個UIAlter的控制元件供開發人員使用,但是系統自帶往往有侷限性,不能滿足需求了,很多時候需要自定義提示框。我寫了一個比較簡單的自定義提示框,當然可以根據自己實際的需求,在我寫的基礎上面進行自定義,去新增一些更加豐富的功能。

1、新建一個類,命名為:PopAlterView,繼承於UIView,然後新增如下程式碼:

#import <UIKit/UIKit.h>

@interface PopAlterView : UIView

@property (strong, nonatomic) UIView
*transparentView; @property (strong, nonatomic) UIView *alterView; @property (strong, nonatomic) UILabel *labTitle; @property (strong, nonatomic) UIButton *btnCancle; @property (strong, nonatomic) UIButton *btnConfirm; /** * 自定義提示框 * * @param theView 提示框彈出的view * @param title 提示框的標題 */ - (void
)showInView:(UIView *)theView withTitle:(NSString *)title; //點選按鈕後的回撥方法 @property (nonatomic,strong) void (^btnAction)(NSInteger tag); @end
#import "PopAlterView.h"

@implementation PopAlterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _transparentView = [[UIView
alloc] initWithFrame:frame]; _transparentView.backgroundColor= [UIColor colorWithWhite:0 alpha:0.25]; [self addSubview:_transparentView]; _alterView = [[UIView alloc] initWithFrame:CGRectMake((frame.size.width-200)/2, (frame.size.height-100)/2, 200, 100)]; _alterView.backgroundColor = [UIColor whiteColor]; _alterView.layer.cornerRadius = 5.0; _alterView.layer.masksToBounds = YES; [_transparentView addSubview:_alterView]; _labTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 50)]; _labTitle.textAlignment = NSTextAlignmentCenter; _labTitle.numberOfLines = 0; [_alterView addSubview:_labTitle]; _btnCancle = [[UIButton alloc] initWithFrame:CGRectMake(10, 60, 80, 25)]; [_btnCancle setTitle:@"取消" forState:UIControlStateNormal]; _btnCancle.layer.cornerRadius = 12.0; _btnCancle.layer.masksToBounds = YES; _btnCancle.backgroundColor = [UIColor grayColor]; [_btnCancle setTag:100]; [_btnCancle addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_alterView addSubview:_btnCancle]; _btnConfirm = [[UIButton alloc] initWithFrame:CGRectMake (105, 60, 80, 25)]; [_btnConfirm setTitle:@"確定" forState:UIControlStateNormal]; _btnConfirm.layer.cornerRadius = 12.0; _btnConfirm.layer.masksToBounds = YES; _btnConfirm.backgroundColor = [UIColor blueColor]; [_btnConfirm addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_btnConfirm setTag:200]; [_alterView addSubview:_btnConfirm]; } return self; } - (void)showInView:(UIView *)theView withTitle:(NSString *)title { [theView addSubview:self]; _labTitle.text = title; } -(void)btnClick:(id)sender { UIButton *btn = (UIButton *)sender; _btnAction(btn.tag); [self removeFromSuperview]; } @end

自定義提示框的類就寫好了,然後就可以呼叫了。

2、在需要呼叫的地方,進行呼叫

- (void)btnClick:(id)sender
{
    PopAlterView *alterView = [[PopAlterView alloc] initWithFrame:self.view.frame];
    [alterView showInView:self.view withTitle:@"測試"];

    alterView.btnAction = ^(NSInteger tag){
        NSLog(@"%ld",(long)tag);
    };
}

對於點選提示框上的“取消”和“確定”按鈕需要進行的操作,可以在btnAction這個回撥方法中進行。

效果圖