Iphone 我想在UIAlertView上显示UIProgressView
我想在UIAlertView上显示UIProgressView,以显示文件上载的处理过程。但我已经搜索了太多,也找到了那个链接,但无法做到这一点Iphone 我想在UIAlertView上显示UIProgressView,iphone,objective-c,ios,uialertview,uiprogressview,Iphone,Objective C,Ios,Uialertview,Uiprogressview,我想在UIAlertView上显示UIProgressView,以显示文件上载的处理过程。但我已经搜索了太多,也找到了那个链接,但无法做到这一点 如果有人知道最简单的方法,请告诉我。虽然这不能完全回答您的问题,但请尝试使用内置此功能的第三方控件。Github上提供的示例应该可以让您快速了解情况。试试这段代码。活动指示器为“是”,progressView为“否” - (void) createProgressionAlertWithMessage:(NSString *)message withA
如果有人知道最简单的方法,请告诉我。虽然这不能完全回答您的问题,但请尝试使用内置此功能的第三方控件。Github上提供的示例应该可以让您快速了解情况。试试这段代码。活动指示器为“是”,progressView为“否”
- (void) createProgressionAlertWithMessage:(NSString *)message withActivity:(BOOL)activity
{
progressAlert = [[UIAlertView alloc] initWithTitle: message
message: @"Please wait..."
delegate: self
cancelButtonTitle: nil
otherButtonTitles: nil];
// Create the progress bar and add it to the alert
if (activity) {
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f);
[progressAlert addSubview:activityView];
[activityView startAnimating];
} else {
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];
[progressAlert addSubview:progressView];
[progressView setProgressViewStyle: UIProgressViewStyleBar];
}
[progressAlert show];
[progressAlert release];
}
请尝试此代码
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIProgressView *pv = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
pv.frame = CGRectMake(20, 20, 200, 15);
pv.progress = 0.5;
[av addSubview:pv];
[av show];
为什么不使用alerviewdelegate方法呢
- (void)willPresentAlertView:(UIAlertView *)alertView
这样做的好处是,我们可以看到alertview在屏幕上的实际大小,因为iOS已经在这一点上进行了预计算,所以不需要使用幻数,也不需要覆盖苹果警告的类
从iOS7开始,我记得读过苹果的一些文件,上面说不要硬编码任何帧大小,而是总是从应用程序中计算它们,或者类似的东西
- (void)willPresentAlertView:(UIAlertView *)alertView
{
CGRect alertRect = alertview.bounds;
UIProgressView *loadingBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
loadingBar.bounds = CGRectMake(0, 0, alertRect.width, HEIGHT_YOU_WANT);
// Do what ever you want here to set up the alertview, as you have all the details you need
// Note the status Bar will always be there, haven't found a way of hiding it yet
// Suggest adding an objective C reference to the original loading bar if you want to manipulate it further on don't forget to add #import <objc/runtime.h>
objc_setAssociatedObject(alertView, &myKey, loadingBar, OBJC_ASSOCIATION_RETAIN); // Send the progressbar over to the alertview
}
然后使用
UIProgressView *loadingBar = objc_getAssociatedObject(alertView, &myKey);
记住要有定义
#import <objc/runtime.h>
static char myKey;
#导入
静态字符myKey;
在您的类声明顶部我在做这件事时遇到了问题,结果是:
av = [[UIAlertView alloc] initWithTitle:@"Running" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressView.frame = CGRectMake(0, 0, 200, 15);
progressView.bounds = CGRectMake(0, 0, 200, 15);
progressView.backgroundColor = [UIColor blackColor];
[progressView setUserInteractionEnabled:NO];
[progressView setTrackTintColor:[UIColor blueColor]];
[progressView setProgressTintColor:[UIColor redColor]];
[av setValue:progressView forKey:@"accessoryView"];
[av show];
这是创建警报视图的步骤 UIAlertController*alert=[UIAlertController alertControllerWithTitle:@“消息”消息:@“这是测试”首选样式:UIAlertControllerStyleAlert] 现在添加文本字段
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.placeholder=@"Enter Text label";
[textField setBorderStyle:UITextBorderStyleRoundedRect];
textField.backgroundColor=[UIColor whiteColor];
}];
并将其添加到视图中
[自我呈现视图控制器:警报动画:是完成:无] 这很难打败书中的解释-它几乎包含了您所需的所有代码,只要耐心地按照本章进行操作,您就应该到达那里。如果您知道如何使用UIProgressView,那么它很简单。将ProgressView作为子视图添加到alert中。该死!我发布了同样的东西:)我个人使用MBProgressHUD,因为我在UIAlertView上有问题。我主要使用“MBProgressHudModeUndeterminate”模式,但我也喜欢“MBProgressHUDModeCustomView”,因为您可以使用用户的信息显示HUD几秒钟。您可能要查找的模式为“MBProgressHUDModeDeterminate”或“MBProgressHudModeArdeterminate”。你绝对应该试试MBProgressHUD,我现在不能停止使用它。:)我还可以在警报视图上显示进度视图的上载百分比吗?是,就像您添加了进度视图一样添加
UILabel
,并将其值更改为percentage@InderKumarRathore:我对此有一个疑问,因为苹果不鼓励在警报视图中添加UITextField,他们会允许添加进度视图吗?我用YLProgressBar做了同样的事情,但我想知道苹果会不会接受它。@Yogi是的,他们不会,苹果也在警报视图中使用了UITextField
。尽管苹果在警报视图中使用了文本字段,但他们不允许开发人员遵循相同的方法,因为我的一个应用程序因同样的原因被拒绝。这是唯一对我有效的解决方案。使用iOS7
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.placeholder=@"Enter Text label";
[textField setBorderStyle:UITextBorderStyleRoundedRect];
textField.backgroundColor=[UIColor whiteColor];
}];