簡體   English   中英

iOS問題中的自定義警報視圖

[英]Custom alert view in iOS issue

在我的應用中,我必須創建一個自定義警報視圖,如下所示:

在此處輸入圖片說明

因此,我按照本教程創建了一個自定義警報視圖。 我完成了,但是在以下方法中遇到了問題:

- (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

我使用CustomAlertView的UPDATE 2代碼:

#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

也許您是在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 並通過添加方法來修改.m:

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

確保修改情節提要(如果符合條件)以通過以下方式定義“ customAlertView”:

故事板屬性的屏幕截圖

也不要忘記將UIButton綁定到控制器,這在實現中也可能是一個錯誤:

將UIButton綁定到控制器

希望這個能對您有所幫助 :)

我在網上找到了一個使用代碼創建自定義警報視圖的教程,如果您有興趣,可以轉到本教程 我用它來解決問題,效果很好! 您必須修復一些問題,因為它使用了已棄用的命令,但修復起來很容易。 如果您有興趣,請看一下本教程。 我認為您可以將其集成到您的應用中,然后在必要時可以輕松地用於其他內容。 希望我的回答對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM