开发者

UIActivityIndicator question

The apple documentation made this seem like it was pretty easy but it's not working. My code is:

 UIActivityIndicatorView *activity = [[UIActivityIndicatorVie开发者_StackOverflow中文版w alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

 [activity startAnimating];

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.myserver.com"]]; 

[request setPostValue:name forKey:@"key"];


[request startSynchronous]; 
    NSLog(@"%@",[request responseString]);

 [activity stopAnimating];
 [activity release];

I didn't set anything up in UIBuilder because frankly, I don't understand exactly how it works with the UIActivityIndicator. Building and Running the app with the code I have above doesnt send off any warning indicators and runs just fine but I am not seeing the activity indicator.


@Eiko's right that your UI is going to block waiting on your synchronous web request to finish, but there's another problem. You're not even adding your activity view to your main view!

You need to give UIActivityIndicatorView *activity a .frame value, and then add it to the view heirarchy. Thusly:

activity.frame = CGRectMake(0,0,50,50); //that'll be top-left corner
[self.view addSubview:activity];
[activity startAnimating];

Now, it'll sit there NOT spinning while you do your web request, because you're doing that request on the main thread. I'm VERY glad you're using ASI, but you're going about it the ugly way.

Make your UIViewController conform to the ASIHttpRequestDelegate protocol.

Set request.delegate = self;.

Implement -requestFinished(ASIHttpRequest *)request to handle the response you get, and in THAT method, hide your activity view. You will PROBABLY wish at that point that you'd made it a named property, or at least an iVar, so you have a handle on it later.


The UI won't update before finishing the method call (well or until the next iteration in the run loop to be more precisely), so this is effectively adding and removing it in the same step.

You should time your request asynchronously to see something happen, or at least schedule the different tasks (adding activity, the request itself and removing it) independently on the main thread. Blocking the main thread is a very bad idea though, and your app will get killed completely if your request takes too long to respond (for whatever reason - easy to think of with web services).


Try putting UIActivityIndicatorView creation in a different thread.


       UIView *acty_view=[[UIView alloc]initWithFrame:CGRectMake(130,220, 60, 60)];
        acty_view.backgroundColor=[UIColor blackColor];
        acty_view.alpha = 0.6;
        acty_view.layer.cornerRadius = 10;
        acty_view.layer.masksToBounds = YES;
        activitidicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(15, 15, 30, 30)];
        [activitidicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
        [activitidicator setColor:[UIColor whiteColor]];

        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(8, 42, 100, 15)];
        lbl.text=@"Loading";
        [lbl setFont:[UIFont systemFontOfSize:10]];
        lbl.textColor = [UIColor whiteColor];
        // [lbl setFont:[UIFont fontWithName:@"Helvetica" size:13]];
        [acty_view addSubview:lbl];
        [acty_view addSubview:activitidicator];
        [self.view addSubview:acty_view];
        [activitidicator startAnimating];
        [self performSelector:@selector(CallWebserviceMethod) withObject:activitidicator afterDelay:0];`

-(void)CallWebserviceMethod
{
[self JsonResults];
[activitidicator stopAnimating];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜