简体   繁体   English

如何重新加载UIAlertView消息

[英]How can I reload UIAlertView message

I took this project from github . 我从github上获得了这个项目。

It is custom AlertView. 它是自定义的AlertView。 I understood how it works. 我了解它是如何工作的。 Modified it for my project, like this 为我的项目修改它,像这样

在此处输入图片说明

Course1 - name of some product, and '1' - is its amount. Course1-某种产品的名称,而'1'-是其数量。 When I'm typing on plus/minus amount increases/decreases. 当我输入正负号时,金额会增加/减少。 But it works only for my variable (which I load on this AlertView, when I show it, and it is 1). 但这仅适用于我的变量(在显示该变量时加载到此AlertView上,它为1)。 How can I reload message of this alert view with changing var (amount). 如何通过更改var(金额)来重新加载此警报视图的消息。 I can't understand. 我听不懂 Here my code. 这是我的代码。

In my class I call alert view by this code 在我的课堂上,我通过此代码调用警报视图

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 method just draws alert view with params which takes from alertWithTitle: title message:ac.acCount. Show方法仅使用来自alertWithTitle:title message:ac.acCount的参数绘制警报视图。 Here is the code 这是代码

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

here is 这是

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

} }

I tried to use something like this 我试图用这样的东西

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

and call this from my class, where i shows alert view. 并从我的班级中调用它,在这里我显示警报视图。

[alert reloadAlertView: title: newMessage];

YOu need to ref a new label in your CustomAlertView.h 您需要在CustomAlertView.h中引用新标签

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

then you can get the properties of this label by the object of the CustomAlertView.h class 那么您可以通过CustomAlertView.h类的对象获取此标签的属性

BlockAlertView *alert ...;
alert.mylab.text = @"hello";
  1. solution (simple , not required to hack the BlockAlertView.m ) whenever the + button is click, the alert will be disappeared, and reappeared again. 解决方案(简单,不需要hack BlockAlertView.m ),只要单击+按钮,警报就会消失,然后再次出现。

ViewController.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. solution , if you want to keep the AlertView remained, without popping up. 解决方案,如果要保留AlertView而不弹出。

a. 一种。 Modify BlockAlertView.h to bring messageLabelView outside to be accessed from ViewController , so we can redraw the message later. 修改BlockAlertView.h以将messageLabelView带到外部,以便可以从ViewController进行访问,以便稍后重新绘制消息。

BlockAlertView.h BlockAlertView.h

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

You can also bring the UIView *_view out of @protected and into @property instead, if preferred. 您也可以带来UIView *_view@protected进入@property相反,如果首选。 In that case, messageLabelView can be access via _view 's subviews 在这种情况下,可以通过_view的子视图访问messageLabelView

b. b。 Modify BlockAlertView.m dismissWithClickedButtonIndex:animated: to disable removeView function from being called if buttonIndex == PLUS sign button - buttonIndex to be hardcoded to 0 . 修改BlockAlertView.m dismissWithClickedButtonIndex:animated:如果buttonIndex == PLUS标记button- buttonIndex硬编码为0 ,则禁用removeView函数。

There may be a better option to stop the execution thread inside the block obj of PLUS button, but I dont know how to do :) 也许有更好的选择来停止加号按钮的块obj内的执行线程,但我不知道该怎么做:)

c. C。 Modify BlockAlert.m initWithTitle:message: to add reference of messageLabelView into labelView during the render of message Label. 修改BlockAlert.m initWithTitle:message:以在呈现消息Label的过程中将messageLabelView引用添加到labelView中。

BlockAlertView.m 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. d。 Change reloadAlert in ViewController.m ViewController.m更改reloadAlert

ViewController.m 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