【问题标题】:Alert view does not disappear [iOS] [duplicate]警报视图不会消失 [iOS] [重复]
【发布时间】:2014-03-11 22:37:28
【问题描述】:

我创建了一个自定义警报视图,我在其中创建了自定义按钮并删除了默认按钮。当我单击自定义按钮时,警报视图不会消失。请帮我解决这个问题。我正在使用以下代码,

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alertView show];
UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)];
[alertView addSubview:lbladdblockname];

【问题讨论】:

  • 在此处分享您的代码。
  • UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; [警报视图显示]; UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)]; [alertView addSubview:lbladdblockname];
  • 点击按钮时显示代码。
  • 自定义按钮在哪里声明和使用?
  • -(void)gotoWelcomeScreen{ // profileInfoViewController *profile= [self.storyboard instantiateViewControllerWithIdentifier:@"profileInfo"]; scrollingViewController *profile= [self.storyboard instantiateViewControllerWithIdentifier:@"scroller"]; profile.userImagePath=regUserPhoto; profile.phonenum=regPhNumber; profile.selfregistrationId=[NSString stringWithFormat:@"%d",registeredid ]; [self presentModalViewController:profile Animation:YES]; }

标签: ios iphone objective-c


【解决方案1】:

如果你的类是 UIAlertView 的子类,那么你可以通过调用这个方法来关闭它。

[alert dismissWithClickedButtonIndex:0 animated:TRUE];

如果您在课堂内使用此方法,则可以使用 self 代替 alert

【讨论】:

    【解决方案2】:

    1.包含您的 .h 文件:UIAlertViewDelegate

    2.请按照下面的实现...

    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [alertView show];
    UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)];
    [alertView addSubview:lbladdblockname];    
    [self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.0];
    

    dismiss 方法将是...

    -(void)dismiss:(UIAlertView*)alert
    {
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
    

    或者你想自定义视图而不是使用

     UIAlertView*testAlert=[[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
    
    
    UIButton*testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [testButton setFrame:CGRectMake(20, 20, 40, 40)];
    
    [testButton setTitle:@"Hello" forState:UIControlStateNormal];
    [testButton addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
    [testAlert addSubview:testButton];
     [testAlert show];
    
    -(void)someAction
    
    {
    [testAlert removeFromSuperView];
    }
    

    【讨论】:

    • 我正在使用此代码,但延迟后会显示警报视图。它不会等到我点击标签
    • 嘿我编辑我的答案检查希望它会有所帮助
    • 谢谢木人。你的第一种方法对我有用:)
    【解决方案3】:

    要解决此问题,您可以这样做:

    在标题中定义alertView,然后:

    - (void)viewDidLoad 
    {
        alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    
        UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(0,0,20,30)];
        [lbladdblock setText:@"custom Message"];
    
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTitle:@"Dismiss!" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(dismissMe) forControlEvents:UIControlEventTouchUpInside];
    
        [alertView addSubview:lbladdblock];
        [alertView addSubview:btn];
    
        [alertView show];
    }
    
     -(void)dismissMe
    {
        [alertview dismissWithClickedButtonIndex:0 animated:YES];
    }
    

    此外,AFAIK addSubViewalertView 在 ios7 中是不可能的,因此您的代码仅适用于以前的版本。看到这个thread

    由于您没有使用 UIAlertView 的内置功能(在所有参数中传递 nil),您应该更喜欢一些自定义警报视图。请参阅链接中的示例警报视图。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2016-01-02
      • 2015-01-18
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2014-12-08
      相关资源
      最近更新 更多