2015-02-26 3 views
0

Привет всем, Я пытаюсь создать свой AlertView с помощью простого класса UIView.Пользовательский UIAlertView iOS8 xCode6

вызвать мое уведомление в реализации желаемого файла с помощью следующего кода

UTAlertView * alert = [[UTAlertView alloc] initWithTitle: @ "Hello how are you" message: @ "test message"]; 

     alert.types = UTAlertViewTypeError; 
     [alert presentAlert]; 
     [self.view addSubview: alert]; 

Как вы видите, чтобы выбрать тип цвета для all'alert используйте

alert.types = UTAlertViewType 

Я думал, сделал все правильно, но я не вижу разных цветов. «Типы» присваивает UIView of Alert ...

Можете ли вы сказать мне, что я сделал что-то не так?

это .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 

и это file.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 является прозрачной

+0

Вы внедрили свойство getter, похожее на то, что он хочет использовать текущее значение для установки цвета фона. Не похоже, чтобы вы называли геттер где угодно. (Я не уверен, что «типы» есть, когда вы говорите 'switch (types) {' или 'return types', потому что я ожидал бы, что переменная поддержки для свойства будет' _types'.) –

+0

извините, но новичок в xcode ... Можете ли вы дать мне пример, более понятный, возможно, через код? Я не мог понять тебя ... – kAiN

ответ

1

Когда вы сделаете это alert.types = UTAlertViewTypeError;, это то же самое, как [alert setTypes: UTAlertViewTypeError].

Если вы хотите заставить его изменить фон, вам нужно написать код в методе -(void)setTypes:(UTAlertViewType)t { вместо метода -(UTAlertViewType)types {.

+0

Спасибо .... приятно! – kAiN

Смежные вопросы