【问题标题】:adding a custom view to a alert view将自定义视图添加到警报视图
【发布时间】:2014-06-10 17:35:33
【问题描述】:

我有这样的问题:

我想在警报视图中显示自定义视图。所以我创建了一个单独的 xib 文件并设计了我的界面。并为它实现了类。但是当我应用下面的代码时,它给了我一个错误。

这是代码:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirm your Action"
                                                    message:@"Click OK to confirm"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:@"Cancel",nil];


    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"customDialogViewController"  owner:self options:nil];
    customDialogViewController *myView = (customDialogViewController*) [subviewArray objectAtIndex:0];

    [alert setValue:myView forKey:@"accessoryView"];
    //alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = KAlertViewthree;
    [alert show];

这是我的错误:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<ScheduleView: 0x8adf1a0> should have parent view controller:<_UIModalItemAccessoryViewControllerForLegacyAlert: 0xa888b70> but actual parent is:<UINavigationController: 0x8add8c0>'

我对 iOS 开发真的很陌生。我做错了吗?错误在哪里?我不知道这里所说的 Nib 文件是什么“loadNibNamed:@bla boa”我只是给了我的 xib 名称。谁能给我一个更好的方法来做到这一点,或者你能告诉我我必须改变哪里来解决这个问题吗?

请指导我一些.. 谢谢。

【问题讨论】:

  • ,昨天我已经在回答中告诉过你,这不是 UIAlertView 的问题。在你的控制台日志中,它清楚地提到你的 viewcontroller 有问题。所以改变你的问题。它不是UIAlertView 的问题。
  • 先生,我已经按照您昨天所说的进行了操作。但没有奏效。所以我关注这个github.com/wimagguc/ios-custom-alertview。不知何故,我能够在警报自定义视图中添加星标。但不能添加其他文本字段和标签你能建议我一个方法吗
  • 我已经在 github 上创建了它。下载这个并让我知道您在 UIAlertView 上的问题。 github.com/jayaprada-behera/CustomAlertView 。但是您提到的上述问题与 UIAlertView 无关
  • 一种方法是在视图控制器中设计自己的视图,然后以模态方式加载它。 See this answer.

标签: ios uialertview xib addsubview sdcalertview


【解决方案1】:

在 ios7 中的原生 Alert-view 中,无法将自定义视图添加为 alert-view 的子视图。您可以使用ios-custom-alertview 来执行此类任务,您可以随心所欲。希望这对您有所帮助。

我正在使用这个 ios-custom-alertview 并将两个 Bellow 文件添加到我的项目中。

CustomIOS7AlertView.h
CustomIOS7AlertView.m

还有如何使用有quick-start-guide

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      对于所有遇到此问题的人, 我关注了this链接..对于星标,我使用了this..

      您所要做的就是将以下文件添加到项目中

      • CustomIOS7AlertView.h
      • CustomIOS7AlertView.m
      • JSFavStarControl.h
      • JSFavStarControl.m

      并把下面的代码放在你想弹出警报视图的地方

      // Here we need to pass a full frame
      CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
      
      // Add some custom content to the alert view
      [alertView setContainerView:[self createDemoView]];
      
      // Modify the parameters
      [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Close1", @"Close2", @"Close3", nil]];
      [alertView setDelegate:self];
      
      // You may use a Block, rather than a delegate.
      [alertView setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
          NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
          [alertView close];
      }];
      
      [alertView setUseMotionEffects:true];
      
      // And launch the dialog
      [alertView show];
      

      在createDemoView的方法里面,你必须实现你自定义的view。在我的例子中是这样的

      UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
      
      //alertView.tag=2;
      
      UILabel *rateLbl=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 45)];
      rateLbl.backgroundColor=[UIColor clearColor];
      rateLbl.font=[UIFont boldSystemFontOfSize:15];
      rateLbl.text=@"Rate";
      
      UILabel *cmntLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 290, 45)];
      cmntLable.backgroundColor=[UIColor clearColor];
      cmntLable.font=[UIFont boldSystemFontOfSize:15];
      cmntLable.text=@"Add Comment";
      
      UIImage *dot, *star;
      dot = [UIImage imageNamed:@"dot.png"];
      star = [UIImage imageNamed:@"star.png"];
      JSFavStarControl *rating = [[JSFavStarControl alloc] initWithLocation:CGPointMake(150, 20) dotImage:dot starImage:star];
      [rating addTarget:self action:@selector(updateRating:) forControlEvents:UIControlEventValueChanged];
      
      
      UILabel *lblAlertTItle=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 290, 45)];
      lblAlertTItle.backgroundColor=[UIColor clearColor];
      lblAlertTItle.textAlignment=UITextAlignmentCenter;
      lblAlertTItle.font=[UIFont boldSystemFontOfSize:18];
      lblAlertTItle.text=@"Choose your sharing option";
      
      
      UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(150, 57, 100, 25)];
      text.backgroundColor=[UIColor whiteColor];
      //[demoView addSubview:lblAlertTItle];
      [demoView addSubview:text];
      [demoView addSubview:rating];
      [demoView addSubview:rateLbl];
      [demoView addSubview:cmntLable];
      
      
      return demoView;
      

      所以我的输出是这样的。 使用它并玩得开心:)

      感谢所有帮助过我的人。

      【讨论】:

        猜你喜欢
        • 2015-04-05
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多