lre*_*ddy 5 iphone objective-c uialertview ios7
UIAlertView在ios 6中使用以下代码正常工作.但是当涉及ios 7时,子视图(我的代码中的"是"和"否"按钮)没有显示当调用alertview时只显示文本消息.任何人都可以告诉我如何解决这个问题?
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];
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
当呈现 UIAlertView 时,我们可以通过将子视图添加到presentedViewController 的视图来将子视图添加到UIAlerView。我已经通过以下方式访问了 UIAlertView :
NSArray *subviews = [UIApplication共享应用程序].keyWindow.rootViewController.presentedViewController.view.subviews;
我创建了 UIAlerView 的子类:
头文件:
@interface MLKLoadingAlertView : UIAlertView
- (id)initWithTitle:(NSString *)title;
@end
Run Code Online (Sandbox Code Playgroud)
实施文件:
#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
Run Code Online (Sandbox Code Playgroud)
在 - (void)didPresentAlertView:(UIAlertView *)alertView 方法中,我通过访问呈现视图控制器的视图将子视图添加到 UIAlertView 中。
您可以在此处找到相关说明和代码示例