3

I want to display UIProgressView on UIAlertView for displaying the processing of uploading of the file. But I have searched too much and also find on that link but sill unable to do that. I don't get idea from this

If anyone know the easiest way to do that then please let me know.

peterh
  • 11,875
  • 18
  • 85
  • 108
user1487562
  • 139
  • 1
  • 1
  • 9
  • 1
    It's hard to beat an explanation from a book - it has pretty much all the code that you need, just follow the chapter patiently, and you should get there. – Sergey Kalinichenko Jul 26 '12 at 12:51
  • 1
    if you know how to use UIProgressView then it is simple.Add progessview as a subview into alert. – Iducool Jul 26 '12 at 12:51

6 Answers6

6

I've had problems doing this, and ended up with this:

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];
Pear Johan
  • 419
  • 6
  • 2
5

Try this code...

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];
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • can i also display the percentage of uploading with progress view on alert view. – user1487562 Jul 30 '12 at 06:41
  • Yes just like you have added progress view add A `UILabel` and change its value as percentage – Inder Kumar Rathore Jul 30 '12 at 07:08
  • @InderKumarRathore: I have a query on this, as Apple discourage adding UITextField on an alert view, will they allow adding a progress view? I have done the same thing with the difference that I have used YLProgressBar but wondering will it be accepted by Apple or not. – Yogi Mar 28 '13 at 13:32
  • @Yogi yeah they don't, apple has also used `UITextField` in alert view – Inder Kumar Rathore Apr 01 '13 at 10:51
  • Though Apple has used text field in an alert view they don't allow developers to follow the same as one of my apps was rejected because of the same reason. – Yogi Apr 01 '13 at 11:36
  • @Yogi I don't know how apple works, sometime it accepts and some time it don't so you have to just try and if this happens then look for alternative – Inder Kumar Rathore Apr 01 '13 at 11:44
  • Yes, you are correct. Apple is very unreliable in terms of conditions for approval of an app. For now I have used [MBProgressHUD](https://github.com/jdg/MBProgressHUD) rather than taking the risk of rejection of the app. Thanks for being so kind to reply to my query. – Yogi Apr 01 '13 at 12:56
  • @Yogi, [This](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UIAlertView.html) official article seems to state the contrary. – Iulian Onofrei Feb 24 '15 at 12:30
  • @IulianOnofrei: Hmm. Actually the article explains about the properties added to UIAlertView later by apple. So now we can use an alert view with text field by setting the type of alert view. Still apple don't allow us to add any subview to alert view. – Yogi Feb 24 '15 at 13:13
3

While this doesn't quite answer your question, try MBProgressHud, a third-party control that has this feature built-in. The examples supplied on Github should get you up to speed pretty quickly.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • Damn it ! I was posting the same thing :) I personally use MBProgressHUD, as I had issues with UIAlertView. I use mostly the mode 'MBProgressHUDModeIndeterminate' but I also like 'MBProgressHUDModeCustomView' as you can display a HUD for a couple of seconds with infos for the user. The modes you may be looking for are 'MBProgressHUDModeDeterminate' or 'MBProgressHUDModeAnnularDeterminate'. You should definitely try MBProgressHUD, I can't stop using it now. :) – rdurand Jul 26 '12 at 12:57
0

Try this code. Put YES for activity indicator and NO for 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];
}
Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107
0

Why not make use of the alerviewdelegate method

- (void)willPresentAlertView:(UIAlertView *)alertView

The advantage of this is we can see what size the alertview will actually be on screen, as iOS has precomputed this at this point, so no need for magic numbers - or overriding the class which Apple warn against !

And as of iOS7 I remember reading some document from Apple saying not to hard code any frame sizes but to always compute them from the app, or something along those lines ?

- (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
}

To pull reference to the loading bar in

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

Then use

UIProgressView *loadingBar = objc_getAssociatedObject(alertView, &myKey);

Remember to have defined

#import <objc/runtime.h>
static char myKey;

At the top of your class declaration

Aardvark
  • 608
  • 5
  • 15
0

This is create a alert view

UIAlertController* alert=[UIAlertController alertControllerWithTitle:@"Message" message:@"This is test" preferredStyle:UIAlertControllerStyleAlert];

now add textfield

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
 {
     textField.placeholder=@"Enter Text label";
     [textField setBorderStyle:UITextBorderStyleRoundedRect];
     textField.backgroundColor=[UIColor whiteColor];
 }];

and added it on view

[self presentViewController:alert animated:YES completion:nil];

DURGESH
  • 2,373
  • 1
  • 15
  • 13