Displaying an alert view similar to UIAlertView

314 Views Asked by At

I am trying to create a custom "alert view", nothing too fancy I just want to display a view overtop of the current view just like a UIAlertView.

Now here is the problem...I made the view in a nib file and now I want to alloc/init the view whenever I need to present an alert.Can anyone help me?

This is my attempt to get the view initialized...as of right now this crashes my app when I alloc/init a new alert view.

The app is crashing by telling me the view is not key-value compliant for the outlets.. I definitely have the outlets hooked up properly, unless I have a "File's Owner" thing screwed up.

NSObject 0x14e49800> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key....

    - (id)initWithCoder:(NSCoder *)aDecoder {
            self = [super initWithCoder:aDecoder];
            if (self) {
                [self load];
            }
            return self;
        }

- (void)load {
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
    [self addSubview:[views firstObject]];
}

    - (void)awakeFromNib {
        [super awakeFromNib];

    }

    - (id)initWithFrame:(CGRect)frame
    {
        self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][0];
        if (self) {
            [self addSubview:self.contentView];
            self.frame = frame;
        }
        return self;
    }

    - (instancetype)initWithTitle:(NSString *)title
                          message:(NSString *)message
                         delegate:(id)delegate
                cancelButtonTitle:(NSString *)cancelButtonTitle
               confirmButtonTitle:(NSString *)confirmButtonTitle {
        self = [self initWithFrame:CGRectMake(0, 0, 320, 568)];
        if (self) {

        }
        return self;
    }

I made this method to present the view, just a simple addSubview...

- (void)showInView:(UIView *)view {
    [view addSubview:self];

}
2

There are 2 best solutions below

2
On

Try disabling autolayout and run the code.

3
On

Create a class same as below

#import "MyOverLay.h"

@interface MyOverLay (){
    // control declarations
    UIActivityIndicatorView *activitySpinner;
    UILabel *loadingLabel;
}

@end

@implementation AcuOverLay


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

- (void)show:(NSString *)message{
    // configurable bits
    self.backgroundColor = [UIColor blackColor];
    self.alpha = 0.75f;
    self.autoresizingMask =UIViewAutoresizingFlexibleHeight;

    float labelHeight = 22;
    float labelWidth = self.frame.size.width - 20;

    // derive the center x and y
    float centerX = self.frame.size.width / 2;
    float centerY = self.frame.size.height / 2;

    // create the activity spinner, center it horizontall and put it 5 points above center x
    activitySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activitySpinner.frame = CGRectMake(centerX - (activitySpinner.frame.size.width / 2) , centerY - activitySpinner.frame.size.height - 20, activitySpinner.frame.size.width, activitySpinner.frame.size.height);

    activitySpinner.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self addSubview:activitySpinner];

    [activitySpinner startAnimating];

    // create and configure the "Loading Data" label
    loadingLabel = [[UILabel alloc]initWithFrame:CGRectMake(centerX - (labelWidth / 2), centerY + 20 , labelWidth, labelHeight)];


    loadingLabel.backgroundColor = [UIColor clearColor];
    loadingLabel.textColor = [UIColor whiteColor];
    loadingLabel.text = message;
    loadingLabel.textAlignment = NSTextAlignmentCenter;
    loadingLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self addSubview:loadingLabel];

}

- (void)hide{
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        [self removeFromSuperview];
    }];
}


@end

then create instance wherever you want

loadingOverlay = [[MyOverLay alloc]initWithFrame:[UIScreen mainScreen].bounds];
        loadingOverlay.tag = 999;
        [[[UIApplication sharedApplication] keyWindow] addSubview:loadingOverlay];
        [loadingOverlay show:@"Saving ..."];