UIAlertView按钮(子视图)未在iOS 7中显示

时间:2013-09-23 16:13:15

标签: iphone objective-c uialertview ios7

UIAlertView在ios 6中使用以下代码正常工作。但是当涉及到ios 7时,子视图(我的代码中的“是”和“否”按钮)在调用alertview时只显示正在显示文本消息.Can有谁告诉我如何解决这个问题?

viewController.m文件

 [Utilities prCustomAlert:@"Textmessage" inTitle:@"Alert view title" delegate:self inTag:300];
 CustomAlertView *alertView    = [Utilities sharedUtility].customAlertView;
alertView.numberOfBtns  = 2;
UIButton *btn= (UIButton *)[alertView viewWithTag:10];
[btn setTitle:@"no" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(dontlogout) forControlEvents:UIControlEventTouchDown];

btn = (UIButton *)[alertView viewWithTag:11];
[btn setTitle:@"yes" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchDown];    
[Utilities displayCustomAlertForDelegate:self];

UIAlertView.m文件

  CGRect viewFrame    = self.frame;
  CGRect buttonFrame  = button.frame;
  if(self.numberOfBtns==2){

  CGRect labelFrame   = [self viewWithTag:15].frame;        
  button.frame    = CGRectMake(10, 0, 40, 30);
        button.hidden   = NO;

        //yes...
        btn          = (UIButton *)[self viewWithTag:11];       
        btn.frame    = CGRectMake(60, 0, 40, 30);
        btn.hidden   = NO;

        //no..    
        btn          = (UIButton *)[self viewWithTag:10];
        btn.hidden   = YES;


  }

3 个答案:

答案 0 :(得分:1)

我认为iOS7中问题的根源是Apple改变了 UIAlertView 外观机制。 从现在起,在启动两个私有视图控制器之后,会出现任何alertView节目

_UIModalItemAppViewController
_UIModalItemsPresentingViewController

换句话说,现在UIAlertView不是一个纯粹的视图 - 它是一些具有全视图控制器生命周期的复杂视图控制器集合的一部分。

但是一个好消息是您可以在标准警报视图中将 accessoryView 更改为 customContentView

[alertView setValue:customContentView forKey:@"accessoryView"];

请注意,您必须在 [alertView show] 之前调用此方法。

答案 1 :(得分:1)

你在做什么总是错的。您不能将自己的子视图添加到UIAlertView。好消息是 - 在iOS 7中,你没有必要!新的自定义过渡动画机制允许您创建自己的视图,其行为只是一个警报视图,但由于它是您的视图,您可以将任何您喜欢的内容放入其中,如下所示:

enter image description here

请注意假的“警报视图”如何浮动在右侧屏幕截图中的原始视图前面,并使屏幕后面的屏幕变暗,就像真正的警报视图一样。但它完全由自定义内容组成; “真正的”警报视图永远不会包含图像和开关!

对于创建此视图的代码,您可以轻松地根据自己的目的进行调整,请参阅我的github网站:https://github.com/mattneub/custom-alert-view-iOS7

答案 2 :(得分:1)

当呈现UIAlertView时,我们可以通过在presentViewController的视图中添加子视图来向UIAlerView添加子视图。我已经按照以下方式访问了UIAlertView:

NSArray * subviews = [UIApplication sharedApplication] .keyWindow.rootViewController.presentedViewController.view.subviews;

我创建了UIAlerView的子类:

标题文件:

@interface MLKLoadingAlertView : UIAlertView

- (id)initWithTitle:(NSString *)title;

@end

实施档案:

#import "MLKLoadingAlertView.h"

#define ACTIVITY_INDICATOR_CENTER   CGPointMake(130, 90)

@implementation MLKLoadingAlertView

- (id)initWithTitle:(NSString *)title
{
    if ( self = [super init] )
    {
        self.title = title;
        self.message = @"\n\n";

        [self setDelegate:self];
    }

    return self;
}

// You can Customise this based on your requirement by adding subviews.
- (void)didPresentAlertView:(UIAlertView *)alertView
{
    NSArray *subviews = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController.view.subviews;

    if( subviews.count > 1 )
    {
        // iOS while presenting an alertview uses a presening view controller. That controller's view has several subviews. I have picked one
        // subview from it which has frame similar to the alertview frame.
        UIView *presentedView = [subviews objectAtIndex:1];

         UIActivityIndicatorView *customActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [customActivityIndicator startAnimating];
        customActivityIndicator.center = ACTIVITY_INDICATOR_CENTER;

        [presentedView addSubview:customActivityIndicator];
    }
}

@end

In - (void)didPresentAlertView:(UIAlertView *)alertView方法我通过访问Presented View Controller的视图将子视图添加到UIAlertView。

您可以在Here

上找到相关说明和代码示例