如何在iPhone的应用程序开头显示活动指示器

时间:2012-09-06 05:36:57

标签: iphone uiactivityindicatorview

我有应用程序,如果本地数据库中有数据,那么它将首先将其上传到服务器,然后启动应用程序我已经在应用程序中编写了上传代码删除我希望在上传时如何显示活动指示器有警报。 通常很容易显示startAnimating和Stop,但在这种情况下如何做?

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


     [self copyDatabaseIfNeeded];


     NSMutableArray *tempArray = [[NSMutableArray alloc] init];
     self.coffeeArray = tempArray;
     [tempArray release];

     [Coffee checkData:[self getDBPath]];


  int mytestcount=rowCount;
        NSLog(@"My Test ROw Count IS %d",mytestcount);


    if (mytestcount=0) {


    NSLog("No Data To Upload");

    }


   else {


    [Coffee getInitialDataToDisplay:[self getDBPath]];

    [self uploadData];
   }


   [self.window addSubview:[navigationController view]];

       [self.window makeKeyAndVisible];

       return YES;
      }

1 个答案:

答案 0 :(得分:0)

为此操作显示UIActivityIndicator需要将其放在 appDelegate didFinishLaunchingWithOptions 中。在不同的线程中执行上载,以便只要执行操作就显示指示符。变化如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        [self copyDatabaseIfNeeded];
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        self.coffeeArray = tempArray;
        [tempArray release];
        [Coffee checkData:[self getDBPath]];
        int mytestcount=rowCount;
        NSLog(@"My Test ROw Count IS %d",mytestcount);
        if (mytestcount=0) {
            NSLog("No Data To Upload");
        }
       else {
            [Coffee getInitialDataToDisplay:[self getDBPath]];
            //Set activity indicator here and perform uploading in different thread so that indicator is displayed as long as operation is carried out.  

           [self performSelector:@selector(uploadData) withObject:nil afterDelay:0];
       }
       [self.window addSubview:[navigationController view]];
       [self.window makeKeyAndVisible];
       return YES;
      }

在警报中创建UIActivityIndicator并非困难。你可以这样做:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" " message:@" " delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview:progress];
[progress startAnimating];   
[alert show];  

您可以根据需要自定义提醒 希望这会有所帮助。