iOS问题中的自定义警报视图

时间:2014-10-03 14:51:09

标签: ios objective-c uialertview

在我的应用中,我要创建一个自定义警报视图,如下所示:

enter image description here

因此,我按照此tutorial创建了自定义提醒视图。我完成了它,但我在以下方法中遇到问题:

- (void)addOrRemoveButtonWithTag:(int)tag andActionToPerform:(BOOL)shouldRemove {
    NSMutableArray *items = [[NSMutableArray alloc]init];

    [items addObject:self.buttonOk];
    [items addObject:self.buttonClose];

    int buttonIndex = (tag == 1);

    if (shouldRemove) {
        [items removeObjectAtIndex:buttonIndex];
    } else {
        if (tag == 1) {
            [items insertObject:self.buttonOk atIndex:buttonIndex];
        } else {
            [items insertObject:self.buttonClose atIndex:buttonIndex];
        }
    }
}

我编辑它比教程,因为我不需要UIToolBar按钮。当我运行应用程序时,它告诉我,我无法在NSMutableArray中插入nil对象,但我不明白什么是错的,我希望你能帮我解决这个问题。

更新 这是我开发的所有类代码:

#import "CustomAlertViewController.h"

#define ANIMATION_DURATION  0.25

@interface CustomAlertViewController ()
- (IBAction)buttonOk:(UIButton *)sender;
- (IBAction)buttonCancel:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *buttonClose;
@property (weak, nonatomic) IBOutlet UIButton *buttonOk;

@property (strong, nonatomic) IBOutlet UIView *viewAlert;

-(void)addOrRemoveButtonWithTag:(int)tag andActionToPerform:(BOOL)shouldRemove;

@end

@implementation CustomAlertViewController

- (id)init
{
    self = [super init];
    if (self) {
        [self.viewAlert setFrame:CGRectMake(self.labelAlertView.frame.origin.x,
                                         self.labelAlertView.frame.origin.y,
                                         self.labelAlertView.frame.size.width,
                                         self.viewAlert.frame.size.height)];
        [self.buttonOk setTag:1];
        [self.buttonClose setTag:0];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showCustomAlertInView:(UIView *)targetView withMessage:(NSString *)message {
    CGFloat statusBarOffset;

    if (![[UIApplication sharedApplication] isStatusBarHidden]) {
        CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
        if (statusBarSize.width < statusBarSize.height) {
            statusBarOffset = statusBarSize.width;
        } else {
            statusBarOffset = statusBarSize.height;
        }
    } else {
        statusBarOffset = 0.0;
    }
    CGFloat width, height, offsetX, offsetY;

    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
        [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
        width = targetView.frame.size.width;
        height = targetView.frame.size.height;

        offsetX = 0.0;
        offsetY = -statusBarOffset;
    }

    [self.view setFrame:CGRectMake(targetView.frame.origin.x, targetView.frame.origin.y, width, height)];
    [self.view setFrame:CGRectOffset(self.view.frame, offsetX, offsetY)];
    [targetView addSubview:self.view];

    [self.viewAlert setFrame:CGRectMake(0.0, -self.viewAlert.frame.size.height, self.viewAlert.frame.size.width, self.viewAlert.frame.size.height)];

    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:ANIMATION_DURATION];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [self.viewAlert setFrame:CGRectMake(0.0, 0.0, self.viewAlert.frame.size.width, self.viewAlert.frame.size.height)];
    [UIView commitAnimations];

    [self.labelAlertView setText:@"CIAO"];
}

- (void)removeCustomAlertFromView {
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:ANIMATION_DURATION];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [self.viewAlert setFrame:CGRectMake(0.0, -self.viewAlert.frame.size.height, self.viewAlert.frame.size.width, self.viewAlert.frame.size.height)];
    [UIView commitAnimations];

    [self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:ANIMATION_DURATION];
}

- (void)removeCustomAlertFromViewInstantly {
    [self.view removeFromSuperview];
}

- (BOOL)isOkayButtonRemoved {
    if (self.buttonOk == nil) {
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)isCancelButtonRemoved {
    if (self.buttonClose == nil) {
        return YES;
    } else {
        return NO;
    }
}

- (void)removeOkayButton:(BOOL)shouldRemove {
    if ([self isOkayButtonRemoved] != shouldRemove) {
        [self addOrRemoveButtonWithTag:1 andActionToPerform:shouldRemove];
    }
}

- (void)removeCancelButton:(BOOL)shouldRemove {
    if ([self isCancelButtonRemoved] != shouldRemove) {
        [self addOrRemoveButtonWithTag:0 andActionToPerform:shouldRemove];
    }
}

- (void)addOrRemoveButtonWithTag:(int)tag andActionToPerform:(BOOL)shouldRemove {
    NSMutableArray *items = [[NSMutableArray alloc]init];

    [items addObject:self.buttonOk];
    [items addObject:self.buttonClose];

    int buttonIndex = (tag == 1);

    if (shouldRemove) {
        [items removeObjectAtIndex:buttonIndex];
    } else {
        if (tag == 1) {
            [items insertObject:self.buttonOk atIndex:buttonIndex];
        } else {
            [items insertObject:self.buttonClose atIndex:buttonIndex];
        }
    }
}

- (IBAction)buttonOk:(UIButton *)sender {
    [self.delegate customAlertOk];
}

- (IBAction)buttonCancel:(UIButton *)sender {
    [self.delegate customAlertCancel];
}
@end

更新2 我使用CustomAlertView的代码:

#import "PromotionsViewController.h"
#import "CustomAlertViewController.h"

@interface PromotionsViewController () <CustomAlertViewControllerDelegate> {
    BOOL isDeletingItem;
}


@property(nonatomic,strong) CustomAlertViewController *customAlert;

- (IBAction)buttonBack:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *buttonAlert;
- (IBAction)buttonAlert:(UIButton *)sender;

@end

@implementation PromotionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.buttonAlert setTitle:self.promotionSelected forState:UIControlStateNormal];
    [self.customAlert setDelegate:self];
    isDeletingItem = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonBack:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)buttonAlert:(UIButton *)sender {
    self.customAlert = [[CustomAlertViewController alloc]init];
    [self.customAlert removeOkayButton:NO];
    [self.customAlert removeCancelButton:NO];
    NSString *message = [NSString stringWithFormat:@"La tua offerta %@ del 20%% è stata convertita in punti IoSi x10", self.promotionSelected];
    [self.customAlert showCustomAlertInView:self.view withMessage:message];
    isDeletingItem = YES;
}

- (void)customAlertOk {
    if (isDeletingItem) {
        [self.customAlert removeCustomAlertFromViewInstantly];
    } else {
        [self.customAlert removeCustomAlertFromView];
    }
}

- (void)customAlertCancel {
    [self.customAlert removeCustomAlertFromView];
    if (isDeletingItem) {
        isDeletingItem = NO;
    }
}

@end

2 个答案:

答案 0 :(得分:2)

也许您在未完全创建UI时调用addOrRemoveButtonWithTag:andActionToPerform:,因为UI元素是异步创建的。因此,如果您在自定义警报视图实例后立即调用此方法,则会因为视图中的按钮未创建而导致崩溃。

要解决此问题,只有在将自定义提醒添加到视图层次结构后,才需要调用addOrRemoveButtonWithTag:andActionToPerform:

编辑:

使用您在编辑2中提供的示例代码,您可以调用以下行:

- (IBAction)buttonAlert:(UIButton *)sender {
  self.customAlert = [[CustomAlertViewController alloc]init];
  [self.customAlert removeOkayButton:NO];
  [self.customAlert removeCancelButton:NO];
}

但是当你刚刚实例化CustomAlertViewController时,它的2个按钮尚未创建,所以我建议你添加2个属性hasOkButtonhasCancelButton以及一个新的构造函数到你的自定义类中这一个:

- (instancetype) initWithOk:(BOOL)OkButton AndCancel:(BOOL) CancelButton
{
    if(self = [super init])
    { 
       hasOkButton = OkButton;
       hasCancelButton = CancelButton;
    }
}

-(void)viewWillAppear:(BOOL)animated
{
      [super viewWillAppear:animated];
      // At this time, the custom UI buttons will be created in the UI view hierarchy
      [self removeOkayButton: hasOkButton];
      [self removeOkayButton: hasCancelButton];
}

在调用者中,您可以使用以下内容显示自定义警报视图:

- (IBAction)buttonAlert:(UIButton *)sender {
    self.customAlert = [[CustomAlertViewController alloc] initWithOk:NO AndCancel:NO];
    // ...
}

编辑#2

我在一个真实的项目中尝试过你的解决方案,我通过在调用者中使用这些行来实现它:

- (IBAction)buttonAlert:(UIButton *)sender {
    self.customAlert = [self.storyboard instantiateViewControllerWithIdentifier:@"customAlertView"];
    self.customAlert.hasOK = NO;
    self.customAlert.hasCancel = YES;
    NSString *message = [NSString stringWithFormat:@"La tua offerta %@ del 20%% è stata convertita in punti IoSi x10", self.promotionSelected];
    [self.customAlert showCustomAlertInView:self.view withMessage:message];
    isDeletingItem = YES;
}

CustomAlertViewController声明2个可见属性hasOKhasCancel in.h. 并通过添加方法修改.m:

-(void)viewWillAppear:(BOOL)animated
{
    [self removeOkayButton:self.hasOK];
    [self removeCancelButton:self.hasCancel];
}

请务必修改故事板(如果符合条件)以获得&#34; customAlertView&#34;这样定义:

Screen capture of storyboard properties

不要忘记将UIButton绑定到控制器,这在您的实现中也可能是错误的:

Bind UIButtons to the controller

希望这会对你有所帮助:)。

答案 1 :(得分:0)

我在网上找到了一个使用代码创建自定义提醒视图的教程,如果您有兴趣,可以转到此tutorial。我用它来解决我的问题而且效果很好!你必须修改一些东西,因为它使用了弃用的命令,但很容易修复它。 如果您有兴趣,请查看本教程。我认为您可以将其集成到您的应用中,并在您可以轻松使用其他内容(如果有必要)。我希望我的回答能帮助别人。