代码之家  ›  专栏  ›  技术社区  ›  kAiN

自定义UIAlertView iOS8 xCode6

  •  0
  • kAiN  · 技术社区  · 10 年前

    大家好,我正在尝试使用简单的类UIView创建自定义的AlertView。

    使用以下代码调用实现所需文件中的警报

    UTAlertView * alert = [[UTAlertView alloc] initWithTitle: @ "Hello how are you" message: @ "test message"];
    
         alert.types = UTAlertViewTypeError;
         [alert presentAlert];
         [self.view addSubview: alert];
    

    如您所见,要为所有警报选择颜色类型,请使用

    alert.types = UTAlertViewType
    

    我认为我做的一切都正确,但我看不到“类型”为警报的UIView分配的不同颜色。。。

    你能告诉我我做错了什么吗?

    这是.h文件

    #import <UIKit/UIKit.h>
    #import "UIColor+UTAlertView_Color.h"
    
    
    typedef NS_ENUM (NSInteger, UTAlertViewType) {
    
    UTAlertViewTypeSuccess,
    UTAlertViewTypeError,
    UTAlertViewTypeWarning };
    
    
    @interface UTAlertView : UIView
    
    -(id)initWithTitle:(NSString*)title message:(NSString *)message;
    -(void)presentAlert;
    
    
    @property(nonatomic, assign) UTAlertViewType types;
    
    
    @end
    

    这是文件.m

    @interface UTAlertView ()
    @property (nonatomic, strong)UILabel *titleLabel;
    @property (nonatomic, strong)UILabel *messageLabel;
    @property (nonatomic, strong)UIImageView *alertIcon;
    @property (nonatomic, strong)UIView *alertView;
    
    
    
    @end
    
    @implementation UTAlertView
    @synthesize alertIcon;
    @synthesize titleLabel, messageLabel;
    @synthesize alertView;
    @synthesize types;
    
    
    -(id)initWithTitle:(NSString*)title message:(NSString *)message  {
    
        alertView                         = [self initWithFrame:CGRectMake(kPositionX, 0, kSizeW, 55)];
    
        titleLabel                        = [[UILabel alloc] init];
        titleLabel.textColor              = [UIColor titleColor:1];
        titleLabel.font                   = [UIFont systemFontOfSize:14];
        titleLabel.text                   = title;
        titleLabel.frame                  = CGRectMake(2, 3, 50, 10);
        [self addSubview:titleLabel];
    
        messageLabel.textColor            = [UIColor messageColor:1];
        messageLabel.font                 = [UIFont systemFontOfSize:14];
        messageLabel.text                 =  message;
    
    
    
        return self;
    }
    
    
    -(UTAlertViewType)types {
    
    switch (types) {
        case UTAlertViewTypeSuccess:
        alertView.backgroundColor    = [UIColor successColor:1];
            break;
            case UTAlertViewTypeError:
            alertView.backgroundColor    = [UIColor errorColor:1];
            break;
            case UTAlertViewTypeWarning:
            alertView.backgroundColor    = [UIColor warningColor:1];
            break;
    
        default:
            break;
    }
    
    
    return types;
    

    } 我使用的是正确的类型枚举?我做错了什么。。非常感谢你的帮助

    UiView的背景是透明的

    1 回复  |  直到 10 年前
        1
  •  1
  •   Phillip Mills    10 年前

    当你这样做的时候 alert.types = UTAlertViewTypeError; ,与 [alert setTypes: UTAlertViewTypeError] .

    如果要使其更改背景,则需要在 -(void)setTypes:(UTAlertViewType)t { 方法而不是 -(UTAlertViewType)types { 方法