0

I can't figure out why a UILabel only shows up when I create it from the viewDidAppear within my viewController. Here is my code so far:

Within AppDelegate:

CGRect viewBounds;
viewBounds.origin.x = 0;
viewBounds.origin.y = 0;
viewBounds.size.width = screenBounds.size.height;
viewBounds.size.height = screenBounds.size.width;
view = [[EAGLView alloc] initWithFrame: viewBounds];

overlayView = [[OverlayView alloc] initWithFrame: screenBounds];
overlayViewController = [[OverlayViewController alloc] init];
[overlayViewController setView:overlayView];

[window addSubview:view];
[window addSubview: overlayViewController.view];
[window makeKeyAndVisible];
[view start];

Within overlayViewController: (This function is successfully called, but the UILabel doesn't show up)

-(void)showText
{
    NSLog(@"showText()");
    textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 300.0f)];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textAlignment = UITextAlignmentCenter;
    textLabel.font = [UIFont fontWithName:@"Arial" size:30];
    textLabel.text = [NSString stringWithFormat:@"TESTING!"];
    [self.view addSubview:textLabel];
    [self.view bringSubviewToFront:textLabel];
}

Within overlayViewController: (Placing the above code into the viewDidAppear makes it show up from the beginning)

-(void)viewDidAppear:(BOOL)animated
{
   textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 300.0f)];
   textLabel.textColor = [UIColor whiteColor];
   textLabel.backgroundColor = [UIColor clearColor];
   textLabel.textAlignment = UITextAlignmentCenter;
   textLabel.font = [UIFont fontWithName:@"Arial" size:30];
   textLabel.text = [NSString stringWithFormat:@"TESTING!"];
   [self.view addSubview:textLabel];
   [self.view bringSubviewToFront:textLabel];
   [super viewDidAppear:animated];
}

Why would the UILabel not show up from within showText() function when it's called? I verified that the NSLog outputs to the console, yet the UILabel is not on the screen.

To give a little more context, this is an AR application. There is an EAGLView showing the feed of the camera on the screen. As I said, the UILabel, when placed in the viewDidLoad of overlayViewController, shows up the moment the app launches above the camera video feed. When placed inside the showText function, the UILabel doesn't show.

Any help would be appreciated! Thank you!

PS. To give more information, I have tried calling showText() in two ways:

Within my EAGLView.mm (which is where most of the AR functions are handled), I setup a notification as such:

[[NSNotificationCenter defaultCenter] postNotificationName:@"showTextOverlay" object:nil];

Then, within OverlayViewController.m, I placed an observer within ViewDidAppear (since ViewDidLoad doesn't seem to get called, but ViewDidAppear does...)

-(void)viewDidAppear:(BOOL)animated
{
    NSLog(@"viewDidAppear");


    [[NSNotificationCenter defaultCenter] 
         addObserver:self 
            selector:@selector(showText) 
                name:@"showTextOverlay" 
              object:nil];

    [super viewDidAppear:animated];
}

The selector of this observer calls showText(), which is also inside of OverlayViewController.

I next tried a second way:

Within EAGLView.mm, I got the application delegate and controllers directly as such:

ImageTargetsAppDelegate *delegate = (ImageTargetsAppDelegate *)[UIApplication sharedApplication].delegate;
OverlayViewController *controller = delegate.overlayViewController;

[controller showText];

But both ways still did not show any UILabel...

ANSWERED:

I figured this out. It turns out that the way the sample application is written, the updates to UIKit were not being called on the main thread. Therefore, I used the performSelectorOnMainThread when calling my showText...

Thank you everyone for your help!

kurisukun
  • 3,149
  • 5
  • 37
  • 69
  • Post the code where you are calling `-(void)showText` – Nekto Oct 26 '11 at 06:34
  • updated the post above with more info – kurisukun Oct 26 '11 at 06:50
  • kurisukun, could u please help me out in qcar sample app... I am fqacing loads of trouble.... i have also referred to forums post but unable to understand.. please refer to my stackoverflow question [Qualcomm SDK Customization](http://stackoverflow.com/questions/11387208/qualcomm-sdk-customization) – madLokesh Jul 11 '12 at 05:27

1 Answers1

1

One thing to think about is that showText ends with these lines:

[self.view addSubview:textLabel];
[self.view bringSubviewToFront:textLabel];

and then the next line is:

[overlayViewController setView:overlayView];

So, what would the value of self.view be in showText if the viewController does even set its view until the next line? You are likely adding a subview to a nil object.

Generally speaking, you should be doing things a little bit differently. The designated initializer for a view controller is initWithNibName:bundle rather than init so it is recommended that you use that.

More importantly, the OverlayViewController should either load its view from a .xib file or implement the loadView method to create its view. If you add the label inside of that method or in one of the methods called after it, like viewDidLoad, you will see the label.

Matthew Gillingham
  • 3,429
  • 1
  • 22
  • 26
  • Hi, thanks for the answer. However, I'm not sure how the next line would be [overlayViewController setView:overlayView]... I suspect that I am indeed adding a subview to a nil object, but I'm not sure yet. Somehow, viewDidLoad is not being called, so perhaps this is the reason? I want to have a function (not only at init time), where I can show/remove UILabels as I see fit. Specifically speaking, I am using Qualcomm's QCAR's free AR source sample code. I simply want to have the app detect a marker, then I will dynamically update the UILabel with the appropriate text based on the marker.. – kurisukun Oct 26 '11 at 07:00
  • The viewController doesn't create its view right away, but "lazily" when it needs to present it, so you can make a method to add more labels, just don't run it until after [window addSubview: overlayViewController.view]; I would get rid of the setView: method entirely. – Matthew Gillingham Oct 26 '11 at 07:11
  • Just to confirm, you would get rid of the setView: method and instead load the view from within the loadView: method of OverlayViewController that I should override? – kurisukun Oct 26 '11 at 07:19
  • Yes. The fact that you must either load from a nib file or implement loadView is also covered in Apple's documentation on UIViewController. – Matthew Gillingham Oct 26 '11 at 07:23
  • Thank you, I will try this now! The funny thing is that this is the source code provided by Qualcomm -- I haven't touched the way the view controllers are being initialized and set. I'll give it a go and see if it helps! – kurisukun Oct 26 '11 at 07:31
  • I've used initWithNib (with the nib and bundle set to nil), and I put my NSNotification observer in the init. I think also overrode loadView: but still the UILabels don't show up... – kurisukun Oct 26 '11 at 07:54