0

カスタムの「アラート ビュー」を作成しようとしていますが、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];

}
4

2 に答える 2

0

自動レイアウトを無効にしてコードを実行してみてください。

于 2014-08-19T02:41:58.367 に答える
0

以下と同じクラスを作成します

#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 ..."];
于 2014-08-19T02:49:33.930 に答える