IOS- custom AlertView (convenience constructor)

Preface


Traversing a constructor is also known as a factory method, which can be used to encapsulate one or more controls into a class

What I’m talking about is the encapsulation of a class that automatically creates a prompt box based on the input array

Design sketch:

IOS- custom AlertView (convenience constructor)
here to write picture description
IOS- custom AlertView (convenience constructor)
here to write picture description

Upper code

First create a CustomAlertView class that inherits from NSobject

IOS- custom AlertView (convenience constructor)
here to write picture description

And then write a method declaration in CustomAlertView.h, because it is inherited from the NSobject so to manually import the UIKit framework

#import < Foundation/Foundation.h> #import; < UIKit/UIKit.h> / / @protocol CustomAlertViewDelegate < NSObject> agent method;; / / @optional / / optional execution method parameter click button subscript - (void) didSelectAlertButton: (NSString * title); @end @interface CustomAlertView: NSObject / * * * / singleton + (CustomAlertView * singleClass); / * * * to quickly create a prompt box - (UIView) quickAlertViewWithArray: (NSArray * array); / / @property agent (assign, nonatomic) properties of id< CustomAlertViewDelegate> delegate; @end

Then start the implementation method in CustomAlertView.h.m

#import "CustomAlertView.h" RGB / #define / * * binary code to UIColorFromRGBValue (rgbValue) [UIColor (colorWithRed: (float) ((rgbValue & 0xFF0000 >); > 16) (/255.0) (float) (green: (rgbValue & > > 0xFF00) /255.0 blue: (8)) (float (rgbValue) & 0xFF; /255.0 alpha:1.0])) @implementation CustomAlertView / * * * / singleton + (CustomAlertView *) singleClass{static CustomAlertView *manager static = nil; dispatch_once_t onceToken; dispatch_once (& onceToken, manager = [[CustomAlertView ^{alloc] init]; return manager;}}); / * * * / - view (UIView *) quickAlertViewWithArray: (* NSArray) array{CGFloat buttonH CGFloat = 61; buttonW = 250; / / create a view by the length of the array of high UIView *alert = " UIView alloc] initWithFrame:CGRectMake (0, 0, buttonW, array.count * buttonH)]; for (int i = 0; I < array.count; i++) {/ / because of a split line so the following is a layer of view UIView *view [[UIView alloc] = initWithFrame:CGRectMake (0, i*buttonH, buttonW, buttonH) = view.backgroundColor]; [UIColor whiteColor] button UIButton *button; / / create = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake (0, 0, buttonW, buttonH); [button setTitle: array[i] forState: (UIControlStateNormal)]; / / All button are associated with a click, through the button on the title [button addTarget:self (alertAction: action:@selector of forControlEvents: (UIControlEventTouchU) PInside)]; [view addSubview:button]; / / here according to the value change state of if ([array[i] isEqualToString:@ cancel [UIColor]) {button.tintColor = whiteColor]; view.backgroundColor = UIColorFromRGBValue / green background (0x82DFB0;}else) {button.tintColor = UIColorFromRGBValue (0x333333); / / line / / if not the last line of UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake (0, 60, buttonW, 1); lineView.backgroundColor = UIColorFromRGBValue (0xefefef); view addSubview:lineView] [alert addSubview:view] ";}}; return alert;} / * * button click event notification agent execution The method of - (void) alertAction: * (UIButton *) button{[_delegate didSelectAlertButton:button.titleLabel.text] @end;}

Call in ViewController

#import "ViewController.h" #import "CustomAlertView.h" @interface ViewController (<); CustomAlertViewDelegate> / * * * @property prompt box (strong, nonatomic) UIView *alertView; @end @implementation ViewController (void) viewDidLoad {[super viewDidLoad]; / / self.view.backgroundColor = [UIColor lightGrayColor]; / / will be prompted to join the view [self.view addSubview:self.alertView] on the page; / / this is stroyBoard} the creation of button - (IBAction) alertAction: (UIButton *) sender [UIView animateWithDuration:0.1 animations:^{/ / UIView animation {self.alertView.alpha = 1; self.alertView.hidden = NO;}];} / * * * / - prompt lazy loading (UIView *) alertView{if (! _alertView) {/ / this can be The alerView to create a mask, direct operation mask transparency hidden to display animation, can also avoid the other control click on the outside of the box, here is not the fine writing assignment / / singleClass] / / _alertView = [[CustomAlertView to quickAlertViewWithArray:@[@ array "OK", "A", @ @ "test B" "cancel", @]]; / / set the center, if necessary please adaptation layoutIfNeed _alertView.center = self.view.center; / / cut fillet _alertView.layer.masksToBounds = YES; _alertView.layer.cornerRadius = 10; / / the initial state is hidden, transparency is 0 _alertView.hidden = YES; _alertView.alpha = 0; / / set the proxy [Cu StomAlertView singleClass].delegate = self;} return _alertView;} / / value transfer agent (void) - didSelectAlertButton: (NSString *) title{[UIView animateWithDuration:0.1 animations:^{self.alertView.alpha = 0;} completion:^ (BOOL finished) {/ / if hidden does not appear directly in the animation animation, so to hide the self.alertView.hidden = YES at the end of the animation after}];; NSLog (@ "% @", title);} @end

Effect drawing

IOS- custom AlertView (convenience constructor)
here to write picture description

The above is the function of all the code

GitHub:https://github.com/Lafree317/customAlertView