【问题标题】:Displaying an alert view similar to UIAlertView显示类似于 UIAlertView 的警报视图
【发布时间】:2014-08-19 11:23:15
【问题描述】:

我正在尝试创建一个自定义的“警报视图”,没什么特别的,我只想在当前视图之上显示一个视图,就像 UIAlertView 一样。

现在问题来了...我在 nib 文件中创建了视图,现在我想在需要显示警报时分配/初始化视图。有人可以帮我吗?

这是我初始化视图的尝试...截至目前,当我分配/初始化一个新的警报视图时,这会使我的应用程序崩溃。

应用程序崩溃,告诉我视图不符合插座的键值对。我肯定插座已正确连接,除非我有一个“文件所有者”的事情搞砸了。

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

我做了这个方法来呈现视图,只是一个简单的 addSubview...

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

}

【问题讨论】:

  • 1) 你的 AlertView 扩展了什么类?是UIView,UIViewController吗? 2)您要调用什么代码来初始化它?是不是很简单:yourAlertView = [[YourAlertView alloc] init];? 3) 显示的错误信息是什么?
  • 它是UIView的子类,我希望能够去yourAlertView = [[YourAlertView alloc] init]。问题是无论我做什么,视图的框架都是 CGRectZero 。虽然我能够让它运行,但是当我调用 showInView 时什么也没发生:
  • 只是事后考虑,您是否在 Interface Builder 和 loadNibNamed 函数调用中将 File's Owner 设置为 self?此外,手动将框架设置为所需的尺寸。
  • 是的,我确实将文件所有者设置为设置框架,我试图在上面的代码中这样做?我做错了吗?

标签: ios objective-c uialertview awakefromnib


【解决方案1】:

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

【讨论】:

  • iOS 7 似乎禁用了 UIAlertView 上的 addSubview 方法
  • 我没有使用 UIAlertView,我正在制作一个模仿它的自定义视图。
【解决方案2】:

创建一个与下面相同的类

#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

然后在任何你想要的地方创建实例

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

【讨论】:

  • 理想情况下这是我想要的,但视图的框架始终是 CGRectZero,因为我在代码中分配/初始化视图,而不是来自另一个 nib。这是我需要解决的问题(如果可能的话)
  • 遗憾的是,即使我这样做,框架仍然是 CGRectZero。
  • 由于这是在代码中完成的布局,我假设上面的代码完全可以正常工作,但是从 nib 加载视图会带来一些复杂性,这就是我正在努力解决的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
相关资源
最近更新 更多