简体   繁体   English

显示类似于UIAlertView的警报视图

[英]Displaying an alert view similar to UIAlertView

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. 我试图创建一个自定义的“警报视图”,没有什么太花哨的了,我只想像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? 现在这是问题所在...我在nib文件中创建了视图,现在我想在需要发出警报时分配/初始化视图。有人可以帮助我吗?

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.... NSObject 0x14e49800> setValue:forUndefinedKey:]:此类不符合该密钥的编码值。...

    - (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... 我做了这种方法来呈现视图,只是一个简单的addSubview ...

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

}

尝试禁用自动布局并运行代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM