簡體   English   中英

如何重新加載UIAlertView消息

[英]How can I reload UIAlertView message

我從github上獲得了這個項目。

它是自定義的AlertView。 我了解它是如何工作的。 為我的項目修改它,像這樣

在此處輸入圖片說明

Course1-某種產品的名稱,而'1'-是其數量。 當我輸入正負號時,金額會增加/減少。 但這僅適用於我的變量(在顯示該變量時加載到此AlertView上,它為1)。 如何通過更改var(金額)來重新加載此警報視圖的消息。 我聽不懂 這是我的代碼。

在我的課堂上,我通過此代碼調用警報視圖

BlockAlertView *alert = [BlockAlertView alertWithTitle: title message:ac.acCount];

[alert setCancelButtonWithTitle:@"-" block:nil];
[alert setDestructiveButtonWithTitle:@"+" block:^{
            int u = [ac.acCount intValue];
            u++;
            ac.acCount = [NSString stringWithFormat:@"%d", u];
            NSLog(@"%d", u);
        }];
        [alert addButtonWithTitle:@"Ok" block:^{
            NSMutableArray *container = [[NSMutableArray alloc] init];
            [container addObject:title];
            [container addObject:price];
            [container addObject:bId];
            [container addObject:ac.acCount];
            [container addObject:depid];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:container];
        }];
        [alert show];

Show方法僅使用來自alertWithTitle:title message:ac.acCount的參數繪制警報視圖。 這是代碼

+ (BlockAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message
 {
    return [[[BlockAlertView alloc] initWithTitle:title message:message] autorelease];
 }

這是

- (id)initWithTitle:(NSString *)title message:(NSString *)message 
{
NSLog(@"title - %@ message - %@", title, message);

if ((self = [super init]))
{
    UIWindow *parentView = [BlockBackground sharedInstance];
    CGRect frame = parentView.bounds;
    frame.origin.x = floorf((frame.size.width - background.size.width) * 0.5);
    frame.size.width = background.size.width;

    _view = [[UIView alloc] initWithFrame:frame];
    _blocks = [[NSMutableArray alloc] init];
    _height = kAlertViewBorder + 6;

    if (title)
    {
        CGSize size = [title sizeWithFont:titleFont
                        constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000)
                            lineBreakMode:UILineBreakModeWordWrap];

        UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)];
        labelView.font = titleFont;
        labelView.numberOfLines = 0;
        labelView.lineBreakMode = UILineBreakModeWordWrap;
        labelView.textColor = kAlertViewTitleTextColor;
        labelView.backgroundColor = [UIColor clearColor];
        labelView.textAlignment = UITextAlignmentCenter;
        labelView.shadowColor = kAlertViewTitleShadowColor;
        labelView.shadowOffset = kAlertViewTitleShadowOffset;
        labelView.text = title;
        [_view addSubview:labelView];
        [labelView release];

        _height += size.height + kAlertViewBorder;
    }

    if (message)
    {
        CGSize size = [message sizeWithFont:messageFont
                          constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000)
                              lineBreakMode:UILineBreakModeWordWrap];

        UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)];
        labelView.font = messageFont;
        labelView.numberOfLines = 0;
        labelView.lineBreakMode = UILineBreakModeWordWrap;
        labelView.textColor = kAlertViewMessageTextColor;
        labelView.backgroundColor = [UIColor clearColor];
        labelView.textAlignment = UITextAlignmentCenter;
        labelView.shadowColor = kAlertViewMessageShadowColor;
        labelView.shadowOffset = kAlertViewMessageShadowOffset;
        labelView.text = message;
        [_view addSubview:labelView];
        [labelView release];

        _height += size.height + kAlertViewBorder;
    }

    _vignetteBackground = NO;
}

return self;

}

我試圖用這樣的東西

-(void)reloadAlertView: (NSString *) title: (NSString *) message{
[self initWithTitle:title message:message];
}

並從我的班級中調用它,在這里我顯示警報視圖。

[alert reloadAlertView: title: newMessage];

您需要在CustomAlertView.h中引用新標簽

@property (nonatomic, strong) IBOutlet UILabel *mylab;

那么您可以通過CustomAlertView.h類的對象獲取此標簽的屬性

BlockAlertView *alert ...;
alert.mylab.text = @"hello";
  1. 解決方案(簡單,不需要hack BlockAlertView.m ),只要單擊+按鈕,警報就會消失,然后再次出現。

ViewController.m

@implementation ViewController {


        BlockAlertView *alert ;
        NSInteger value;
    }

    -(void) viewDidLoad {
      value = 0;


        [self reloadAlert];


    }


    -(void) reloadAlert {
        value += 1;


        __block ViewController* _self = self;
        alert = [[BlockAlertView alloc] initWithTitle:@"Course1" message:[[[NSNumber alloc] initWithInteger:value ] stringValue]];


        [alert setDestructiveButtonWithTitle:@"PLUS" block:^{

            [_self reloadAlert];


        }];
        [alert show];

    }
   } 
  1. 解決方案,如果要保留AlertView而不彈出。

一種。 修改BlockAlertView.h以將messageLabelView帶到外部,以便可以從ViewController進行訪問,以便稍后重新繪制消息。

BlockAlertView.h

@interface BlockAlertView  { 
...
}
@property (nonatomic, retain) UILabel *messageLabelView;

您也可以帶來UIView *_view@protected進入@property相反,如果首選。 在這種情況下,可以通過_view的子視圖訪問messageLabelView

b。 修改BlockAlertView.m dismissWithClickedButtonIndex:animated:如果buttonIndex == PLUS標記button- buttonIndex硬編碼為0 ,則禁用removeView函數。

也許有更好的選擇來停止加號按鈕的塊obj內的執行線程,但我不知道該怎么做:)

C。 修改BlockAlert.m initWithTitle:message:以在呈現消息Label的過程中將messageLabelView引用添加到labelView中。

BlockAlertView.m

@synthesize messageLabelView;



     - (id)initWithTitle:(NSString *)title message:(NSString *)message 
    {
     ...
    // code to initialize messageLabelView
     if (message)
            {

               .... 
                // code to render labelView for message. add reference to messageLabelView
                 messageLabelView = labelView;
                  // [labelView release];


            }
    }

    - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 
    {
        if (buttonIndex >= 0 && buttonIndex < [_blocks count])
        {
            id obj = [[_blocks objectAtIndex: buttonIndex] objectAtIndex:0];
            if (![obj isEqual:[NSNull null]])
            {
                ((void (^)())obj)();
            }
        }

    // there may be a better option to stop the execution thread inside the block obj above
        if(buttonIndex == 0) {// or whatever index of Plus Button, dont removeView
            return;
        }

    // code to removeView below, keep as usual
        if (animated)
        {
           ...
        }
        else
        {
        ....
        }
    }
}

d。 ViewController.m更改reloadAlert

ViewController.m

-(void) reloadAlert {

    value += 1;

    alert.messageLabelView.text = [[[NSNumber alloc] initWithInteger:value ] stringValue];
    [alert.messageLabelView setNeedsDisplay];
} 

暫無
暫無

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

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