views:

60

answers:

2

I am wanting to show a simple loading dialog when certain things are happening in my app. I figured I would just create a new view, add a label to that, and then set that view to a subView of my current view.

When doing this, I don't see anything!

Here is how I am writing my method:

- (void)showLoading {
    UIView *loading = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    loading.backgroundColor = [UIColor blackColor];
    UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(198, 9, 94, 27)];
    txt.text = @"Loading...";
    txt.textColor = [UIColor whiteColor];
    [loading addSubview:txt];
    [super.view addSubview:loading];
    [super.view bringSubviewToFront:loading];
    [loading release];
    [txt release];
}

Am I doing this completely wrong?

EDIT: I added it to the viewDidLoad method, and it works how I want:

loading = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    loading.backgroundColor = [UIColor blackColor];
    UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 94, 27)];
    txt.text = @"Loading...";
    txt.textColor = [UIColor whiteColor];
    [loading addSubview:txt];
    [txt release];
    [self.view addSubview:loading];
    [self.view bringSubviewToFront:loading];

But when loading it from a method, it seems to lag, and not show up for a bit.

+1  A: 

First, I don't think UIView has method called init. You may just call the super of it. The appropriate method you should call is - (id)initWithFrame:(CGRect)aRect . The frame is the position, the size of the View you want to display. More here

Another thing is why you call [super.view addSubView:], I think it should be self.view, isn't it?

vodkhang
yeah, [self.view addSubView:loading]You might want to consider assigning it (loading view) to an instance variable so you can keep track of it so you can remove it from the view later.
chilitechno.com
I think that's ok if he doesn't want to assign it. Because if the purpose is just showing one time and release when the whole view is released. But if he wants to remove before that, he should have an instance variable
vodkhang
+1  A: 

Although this doesn't directly answer your question, I'd recommend grabbing MBProgressHUD from GitHub and using that in place of a static label. Looks better, less code for you to directly maintain, etc. You can find it at http://github.com/matej/MBProgressHUD

The way I use it is by creating a subclass of UITableViewController and defining a handful of methods to show and hide the HUD view. From there, I call each relevant method when I'm loading or done loading.

Specifically, I have four methods: -hudView, -showLoadingUI, -showLoadingUIWithText:, and -hideLoadingUI.

-hudView creates a new MBProgressHUD object if one doesn't already exist, and adds it to the current view ([self.view addSubview:hudView]).

-showLoadingUI calls -showLoadingUIWithText: with a default title, -showLoadingUIWithText: just unhides the MBProgressHUD and sets a label value for it (self.hudView.labelText = @"foo";).

-hideLoadingUI hides the hudView ([self.hudView hide:YES]).

Aaron Brethorst
I want to show the HUD when I am doing a loop, but for some reason it waits until the loop is done to show the HUD. Is this normal?
Nic Hubbard
Is this a normal error: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...?
Nic Hubbard
Fixed the error by running my UI code in the main thread.
Nic Hubbard
Yeah, UIKit code must run on the main thread. Glad you got it sorted out! :)
Aaron Brethorst