如何重新加载UIAlertView消息

时间:2013-02-25 12:09:24

标签: ios objective-c uialertview

我从github获得了这个项目。

这是自定义AlertView。我明白它是如何工作的。为我的项目修改了它,比如

enter image description here

课程1 - 某些产品的名称和“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方法只绘制带有params的警报视图,它取自来自alertWithTitle:标题消息: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];

2 个答案:

答案 0 :(得分:1)

你需要在CustomAlertView.h中引用一个新标签

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

然后您可以通过CustomAlertView.h类的对象获取此标签的属性

BlockAlertView *alert ...;
alert.mylab.text = @"hello";

答案 1 :(得分:0)

  1. 解决方案(简单,不需要破解BlockAlertView.m) 只要点击+按钮,警报就会消失,然后重新出现。
  2. <强> 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,请不要弹出。
    2. 一个。修改BlockAlertView.h以便从messageLabelView访问ViewController以外的内容,以便我们稍后重新制作邮件。

      <强> BlockAlertView.h

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

      如果愿意,您也可以将UIView *_view@protected中移出@property。在这种情况下,messageLabelView可以通过_view的子视图访问

      湾修改BlockAlertView.m dismissWithClickedButtonIndex:animated:,以便在removeView签名按钮 - buttonIndex == PLUS被硬编码为buttonIndex时停用0功能。

      可能有更好的选择来停止PLUS按钮的块obj中的执行线程,但我不知道该怎么做:)

      ℃。在呈现消息标签期间修改BlockAlert.m initWithTitle:message:以将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];
      }