0

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];

}
DBoyer
  • 3,062
  • 4
  • 22
  • 32
  • 1) What class does your AlertView extend? Is it UIView, UIViewController? 2) What is the code you are calling to initialise it? Is it simply: yourAlertView = [[YourAlertView alloc] init];? 3) What is the error message shown? – Tcharni Aug 19 '14 at 01:22
  • It is a subclass of UIView, and I want to be able to go yourAlertView = [[YourAlertView alloc] init]. The problem is that the frame of the view is CGRectZero no matter what I do. I was able to get it to run though, but just nothing happens when I call showInView: – DBoyer Aug 19 '14 at 01:40
  • Just as an afterthought, have you set the File's Owner to self in Interface Builder, and in your loadNibNamed function call? Also, manually set the frame to your required size. – Tcharni Aug 19 '14 at 05:06
  • Yes I did set the files owner..as to setting the frame I attempted to do that in my code above? Did I do it wrong? – DBoyer Aug 19 '14 at 07:19

2 Answers2

0

Try disabling autolayout and run the code.

Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
0

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 ..."];
  • That is ideally what I want, but the frame of the view is always CGRectZero because I alloc/init the view in code, not from another nib. This is the problem I need solved (if its even possible) – DBoyer Aug 19 '14 at 02:52
  • Sadly the frame is still CGRectZero even if I do that. – DBoyer Aug 19 '14 at 02:54
  • Since this is a layout done in code I assume the above code would work totally fine, but loading a view from a nib throws some complications into the mix, thats what I am struggling with. – DBoyer Aug 19 '14 at 03:03