IOS development – easy to learn to package custom view view (custom bullet box package detail)

When we do development, we often customize some view, and if the view is more complex or is often called, we can put the view into a class individually, which is convenient for calling or modifying. Some time ago the author encapsulates a custom pop-up box, a simple function, to exchange with you and experience method encapsulates a custom view of the view, just to share learning, is not used directly in the project, if there are mistakes or shortcomings, welcome everyone to come to the criticism.

First effect diagram:

IOS development - easy to learn to package custom view view (custom bullet box package detail)
058C982B-D7A0-4F82-875D-71559F13A562.png

In ViewController.m, import the “SZKAlterView.h” header file
calling code:

SZKAlterView *lll=[SZKAlterView alterViewWithTitle:@ "in the book: iOS_ Kay" content:@ "thank you friends attention and encourage" cancel:@ "Cancel" sure:@ "confirm" cancelBtClcik:^{/ / Cancel button click event NSLog (@ "Cancel"); / / sureBtClcik:} ^{determine the button click event NSLog (@ "OK");}]; [self.view addSubview:lll];

Note:
SZKAlterView package into a class method, and can manually fill in the title, the content, the content of the Cancel button, the OK button, there is a need to pay attention to is the Cancel button and click OK button events using block transfer out, specific implementation methods mentioned below.

Implementation method:
SZKAlterView.h

#import < UIKit/UIKit.h> / / Cancel button click event typedef (^cancelBlock) (void); / / OK button click event typedef void (^sureBlock) (@interface); SZKAlterView: UIView @property (nonatomic, copy) cancelBlock cancel_block (nonatomic, copy); @property sureBlock sure_block; / * * * Jane Book Number: iOS_ * * http://www.jianshu.com/users/86b0ddc92021/latest_articles Kay @param Title * @param * @param title content content cancel content @param sure to determine the Cancel button * * @ param cancelBlock button contents cancel button click event * @param sureBlock to determine the button click event * * @return + SZKAlterView * (instancetype) alterViewWithTitle: (NSString * title) content: (NSString * content) Cancel: (NSString * cancel) sure: (NSString *) sure cancelBtClcik: (cancelBlock) cancelBlock sureBtClcik: (sureBlock) sureBlock; @end

In SZKAlterView.m

- (instancetype) initWithFrame: (CGRect) frame {self=[super initWithFrame:frame]; if (self _titleLb=[[UILabel alloc]initWithFrame:CGRectMake) {/ / Title (0, 0, self.bounds.size.width, 50)]; _titleLb.textAlignment=NSTextAlignmentCenter; _titleLb.textColor=[UIColor blackColor]; [self addSubview:_titleLb]; / / _contentLb=[[UILabel alloc]initWithFrame:CGRectMake content (0, CGRectGetMaxY (_titleLb.frame), self.bounds.size.width, 50)]; _contentLb.textAlignment= NSTextAlignmentCenter; _contentLb.textColor=[UIColor redColor] [self; addSubview:_contentLb]; / / Cancel button (0, CGRectGetMaxY (_cancelBt=[[UIButton alloc]initWithFrame:CGRectMake _contentLb.frame ), self.bounds.size.width/2, 50)]; _cancelBt.layer.borderColor=[UIColor grayColor].CGColor; _cancelBt.layer.borderWidth=0.5 [_cancelBt; setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_cancelBt addTarget:self action:@ selector (cancelBtClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_cancelBt] _sureBt=[[UIButton; / / OK button (self.bounds.size.width/2, alloc]initWithFrame:CGRectMake, self.bounds.size.width/2, CGRectGetMaxY (_contentLb.frame) 50)]; _sureBt.layer.borderColor=[UIColor grayColor].CGColor; _sureBt.layer.borderWidth=0.5 [_sureBt; setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_sure Bt addTarget:self action:@selector (sureBtClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_sureBt];} return self;} #pragma mark---- realization method + (instancetype) alterViewWithTitle: (NSString * title) content: (NSString * content) cancel: (NSString * cancel) sure: (NSString *) sure cancelBtClcik: (cancelBlock) cancelBlock sureBtClcik: (sureBlock sureBlock) {SZKAlterView *alterView=[[SZKAlterView alloc]initWithFrame:CGRectMake; (0, 0, 250, 150)]; alterView.backgroundColor=[UIColor whiteColor]; alterView.center=CGPointMake (WIDTH/2, HEIGHT/2); alterView.layer.cornerRadius=5; AlterView.layer.masksToBounds=YES; alterView.title=title; alterView.content=content; alterView.cancel=cancel; alterView.sure=sure; alterView.cancel_block=cancelBlock; alterView.sure_block=sureBlock; return alterView;} #pragma mark-- to genera of deassign - (void) setTitle: (NSString * title) {_titleLb.text=title}; - (void) setContent: (NSString * content) {_contentLb.text=content}; - (void) setSure: (NSString * sure) {[_sureBt setTitle:sure forState:UIControlStateNormal;} - (void) setCancel: (NSString * cancel) {[_cancelBt setTitle:cancel forState:UIControlStateNormal]}; #pragma mark---- cancel button click event - (void) cancelBtClick (self.cancel_block) {[self removeFromSuperview]; #pragma m;} Ark---- determines the button by clicking on the event - (void) sureBtClick {[self removeFromSuperview]; self.sure_block ();}

Note: because the
view package into a class method, so will call this class will first perform + (instancetype) alterViewWithTitle: (NSString * title) method, followed by the implementation of… – (instancetype) initWithFrame: (CGRect) frame method, and then will continue to execute alterView.title=title; method of attribute assignment, but this interface could not show the content of the need in the set method to give relevant content will show the last assignment, the two functions is the use of block will be transferred to the outside class in the click event.

Implementation method of general here, if you do not understand can contact the author, the author answer, if you want to download the full demo please click on the link: https://github.com/18811314750/SZKAlterView SZKAlterView:

if you think I can write good, star.

If you master the audience have not seen enough, there is a development package
iOS on infinite carousel figure two rapid code to achieve unlimited carousel figure (based on the ScrollView
http://www.jianshu.com/p/d240bd977689 package)

Other articles of the author:
, iOS, online music player, SZKAVPlayer (based on AVPlayer package),
, http://www.jianshu.com/p/4e0ac2898de0

IOS development – a code call to achieve network monitoring function (based on the AFNetworkReachabilityManager package)
http://www.jianshu.com/p/b901ad0c1d81

If there are deficiencies or errors, I hope you can criticize me and comment. I can comment on the message, and I will reply the first time after I receive it.
QQ/ WeChat: 790057066.
Jane Book Number: iOS_ http://www.jianshu.com/users/86b0ddc92021/latest_articles
GitHub Kai: personal homepage: https://github.com/18811314750
welcome to check, star, thank you for reading.