Skip to main content

create a custom alert box with multiple layout options in objective c



//

//  EGviewAlert.h--------------------------


#import <UIKit/UIKit.h>

@interface EGviewAlert : UIView
//alert view with heading message and ok button
typedef void (^closeAction)(id);
typedef void (^cancelAction)(id);
-(void)Alert:(NSString*)heading withMsg:(NSString*)message withOkBtn:(NSString*)okBtnMsg closeAction:(void (^)(id))completeBlock;
//alert view with heading message and ok button and cancel button
-(void)Alert:(NSString*)heading withMsg:(NSString*)message withOkBtn:(NSString*)okBtnMsg withCancelBtn:(NSString*)cancelBtnMsg closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction;

//for custom layout
-(void)customAlert:(NSDictionary *)messages  closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction;

//heading view
@property (nonatomic,strong) UIView *HeadingView;
//heading text
@property (nonatomic,strong) UILabel *headingText;
//heading message
@property (nonatomic,strong) UITextView *msgText;
@property (nonatomic,strong) UILabel *subHeadingText;
@property (nonatomic,strong) UITextField *answerTextField;
//clsoe btn
@property (nonatomic,strong) UIButton *closeBtn;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (copy, nonatomic) closeAction CloseAction;
@property (copy, nonatomic) cancelAction CancelAction;
@end



//


//  EGviewAlert.m-----------------------------------------------------


#import "EGviewAlert.h"
#define _width 480.0f
#define _height 320.0f

@implementation EGviewAlert
{
    CGSize  updateSize;
    CGPoint updatedFrame;
    CGFloat headingFont;
    CGFloat msgFont;
    CGFloat space;
    CGFloat button_width;
    CGFloat button_height;
    id data;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        space=20;
        headingFont=21;
        msgFont=17;
        button_width=125;
        button_height=50;
        self.frame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
        [self addHeadingView:CGSizeMake(frame.size.width, frame.size.height/5)];
    }
    return self;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        headingFont=21;
        msgFont=17;
          space=20;
           self.frame=CGRectMake(([UIScreen mainScreen].bounds.size.width/2-_width/2), ([UIScreen mainScreen].bounds.size.height/2-_height/2), _width, _height);
         [self addHeadingView:CGSizeMake(self.frame.size.width, self.frame.size.height/5)];
    }
    return self;
}

//@add heading view with size
-(void)addHeadingView:(CGSize)size
{
    _HeadingView=[[UIView alloc] init];
    _HeadingView.frame=CGRectMake(0, 0, size.width, size.height);
    _HeadingView.backgroundColor=[UIColor grayColor];
    [self addSubview:_HeadingView];
    //add uilabel to heading text
    _headingText=[[UILabel alloc] init];
    _headingText.frame=CGRectMake(0, 0, size.width, size.height);
    [_headingText setFont:[UIFont boldSystemFontOfSize:headingFont]];
    _headingText.textAlignment=NSTextAlignmentCenter;
    _headingText.text=@"";
    [_HeadingView addSubview:_headingText];
    ///add msgText
    _msgText=[[UITextView alloc] init];
    _msgText.frame=CGRectMake(0, ((_HeadingView.frame.origin.y+_headingText.frame.size.height)+space), size.width, _height/2);
    _msgText.backgroundColor=[UIColor clearColor];
    [_msgText setFont:[UIFont boldSystemFontOfSize:headingFont]];
    _msgText.textAlignment=NSTextAlignmentCenter;\
    _msgText.editable=false;
    _msgText.scrollEnabled=false;
    _msgText.text=@"";
    [self addSubview:_msgText];
    //add close btn

    
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
    self.layer.masksToBounds = NO;
    self.layer.cornerRadius=10;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    self.layer.shadowOpacity = 0.5f;
    self.layer.shadowPath = shadowPath.CGPath;
}
-(void)ClickEvent:(id)object
{
    self.CloseAction(data);
    [self removeFromSuperview];
}
-(void)CancelEvent:(id)object
{
    self.CancelAction(nil);
    [self removeFromSuperview];
}
//add shadow
-(void)addShadow
{
    
}

//update layout
-(void)updateLayout
{
    
}
//alert with ok btn
-(void)Alert:(NSString *)heading withMsg:(NSString *)message withOkBtn:(NSString *)okBtnMsg closeAction:(void (^)(id))completeBlock
{
    _headingText.text=heading;
    _msgText.text=message;
    
    _closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _closeBtn.frame=CGRectMake((_width/2-button_width/2), _height-75, button_width, button_height);
    _closeBtn.backgroundColor=[UIColor grayColor];
    [_closeBtn setTitle:okBtnMsg forState:UIControlStateNormal];
    [self addSubview:_closeBtn];

    [_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    self.CloseAction = completeBlock;
}
//alert with ok and cancel button
-(void)Alert:(NSString *)heading withMsg:(NSString *)message withOkBtn:(NSString *)okBtnMsg withCancelBtn:(NSString *)cancelBtnMsg closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction
{
    _closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _closeBtn.frame=CGRectMake(50, _height-75, button_width, button_height);
    _closeBtn.backgroundColor=[UIColor grayColor];
    [_closeBtn setTitle:okBtnMsg forState:UIControlStateNormal];
    [self addSubview:_closeBtn];
    [_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    _headingText.text=heading;
    _msgText.text=message;
    
    _cancelBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _cancelBtn.frame=CGRectMake(480-175, _height-75, button_width, button_height);
    _cancelBtn.backgroundColor=[UIColor grayColor];
    [_cancelBtn setTitle:cancelBtnMsg forState:UIControlStateNormal];
    [self addSubview:_cancelBtn];
    [_cancelBtn addTarget:self action:@selector(CancelEvent:) forControlEvents:UIControlEventTouchUpInside];
      self.CloseAction = completeBlock;
    self.CancelAction =cancelAction;
}
//custom alert add any object label textfield button segment etc
-(void)customAlert:(id)messages closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction
{
    //heading msg
    //other msgs
    _headingText.text=[messages objectForKey:@"heading"];
    _msgText.hidden=true;
    _closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _closeBtn.frame=CGRectMake(50, _height-75, button_width, button_height);
    _closeBtn.backgroundColor=[UIColor grayColor];
    [_closeBtn setTitle:[messages objectForKey:@"closeText"] forState:UIControlStateNormal];
    [self addSubview:_closeBtn];
    [_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    

    //sub heading text
    _subHeadingText=[[UILabel alloc] init];
    _subHeadingText.frame=CGRectMake(0, 100, self.frame.size.width, 50);
    [_subHeadingText setFont:[UIFont boldSystemFontOfSize:headingFont]];
    _subHeadingText.textAlignment=NSTextAlignmentCenter;
    _subHeadingText.text=[messages objectForKey:@"msg"];
    [self addSubview:_subHeadingText];
    //adding answer text filed
    _answerTextField=[[UITextField alloc] init];
    _answerTextField.textAlignment=NSTextAlignmentCenter;
    _answerTextField.keyboardType=UIKeyboardTypeASCIICapableNumberPad;
    _answerTextField.frame=CGRectMake(25, 150, 400, 50);
    _answerTextField.borderStyle=UITextBorderStyleLine;
    [self addSubview:_answerTextField];
    
    
    
    _cancelBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _cancelBtn.frame=CGRectMake(480-175, _height-75, button_width, button_height);
    _cancelBtn.backgroundColor=[UIColor grayColor];
    [_cancelBtn setTitle:[messages objectForKey:@"cancelText"]  forState:UIControlStateNormal];
    [self addSubview:_cancelBtn];
    [_cancelBtn addTarget:self action:@selector(CancelEvent:) forControlEvents:UIControlEventTouchUpInside];
    self.CloseAction = completeBlock;
    self.CancelAction =cancelAction;

}
-(void)dealloc
{
    NSLog(@"all relased");
}
@end


importing and added file to your view controller---------------------------------------------------------------------


#import "ViewController.h"
#import "EGviewAlert.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    EGviewAlert *alert=[[EGviewAlert alloc] init];
    alert.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:alert];
    [alert customAlert:nil closeAction:^(id data) {
        //
    } cancelAction:^(id data) {
        //
    }]; // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

Comments

Popular posts from this blog

Creating a word Scramble game where you can drag and drop the word and create a complete word. (drag and drop word Scramble game).

Creating a word game using  phaser game engine which is a javascript based gaming engine  where u can drag and drop world like Word Scramble Game and complete a given word. your index.html file look like below < html > < head > < script src = "src/phaser.min.js" ></ script > < script src = "src/wordGame.js" ></ script > < script src = "src/main.js" ></ script > </ head > < body > </ body > </ html > 1.    <script src="src/phaser.min.js"></script> required to run the  phaser game engine. 2. rest of the two files deals with game logics      <script src="src/wordGame.js"></script>       <script src="src/main.js"></script> you can download whole project from https://github.com/manishchauhan/wordgame/tree/master just copy and paste the project in your local server and run ...

starting with three.js and react with a very simple example

 1. three.js is undoubtedly the best library to create interactive content for the web in 3d. link https://threejs.org/ a working sample of three with react can be found below https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js why choose threejs 1. lightweight  2.fast 3.big community  https://discourse.threejs.org/ 4. even you can use unity or unreal to publish html5 content but i don't think that would be acceptable in many cases. 5. open-source project. i created a running sample with React  https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js

A simple binary search tree with generic approach tree can store any type of data. (DFS and BFS)

 1. a binary search tree in c++ using a generic approach with both BFS and DFS approach    working tree    https://www.onlinegdb.com/edit/HkuQLdA-w code=> some properties of binary search tree-> 1. binary search tree is not always a balanced tree.  2. Inorder, traversing gives you sorted data.  3.  best is o(log n) and worst is O(n).  4. left node data is always less than root data and right node data always be greater than root data.  5. red-black tree STL map is a balanced binary search tree. *******************************************************************************/ #include <stdio.h> #include<iostream> #include<queue> using namespace std; template < typename T > class Node {   //can store any type of data private:   //genric data or store any type of data   T data;   //reference of left pointer   Node *left = nullptr;   //reference of right pointer   Node *rig...