UILabel 仅在放置在 viewcontroller 的 viewDidAppear 中时显示
我不明白为什么 UILabel 仅在我从 viewController 中的 viewDidAppear 创建它时才显示。这是到目前为止我的代码:
在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];
在overlayViewController中:(此函数已成功调用,但UILabel没有显示)
-(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];
}
在overlayViewController中:(将上面的代码放入 viewDidAppear 中,使其从头开始显示)
-(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];
}
为什么 UILabel 在调用 showText() 函数时不会显示出来?我验证了 NSLog 输出到控制台,但 UILabel 不在屏幕上。
为了提供更多背景信息,这是一个 AR 应用程序。屏幕上有一个 EAGLView 显示摄像机的画面。正如我所说,当 UILabel 放置在 overrideViewController 的 viewDidLoad 中时,它会在应用程序启动时显示在相机视频源上方。当放置在 showText 函数内时,UILabel 不会显示。
任何帮助将不胜感激!谢谢你!
附言。为了提供更多信息,我尝试以两种方式调用 showText():
在我的 EAGLView.mm(这是处理大多数 AR 功能的地方)中,我设置了这样的通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"showTextOverlay" object:nil];
然后,在 OverlayViewController.m 中,我在 ViewDidAppear 中放置了一个观察者(因为 ViewDidLoad 似乎没有被调用,但 ViewDidAppear 确实被调用...)
-(void)viewDidAppear:(BOOL)animated
{
NSLog(@"viewDidAppear");
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(showText)
name:@"showTextOverlay"
object:nil];
[super viewDidAppear:animated];
}
该观察者的选择器调用 showText(),它也在 OverlayViewController 内部。
接下来我尝试了第二种方法:
在 EAGLView.mm 中,我直接获得了应用程序委托和控制器:
ImageTargetsAppDelegate *delegate = (ImageTargetsAppDelegate *)[UIApplication sharedApplication].delegate;
OverlayViewController *controller = delegate.overlayViewController;
[controller showText];
但是这两种方法仍然没有显示任何 UILabel...
回答:
我想通了。事实证明,按照示例应用程序的编写方式,UIKit 的更新并未在主线程上调用。因此,我在调用我的showText时使用了performSelectorOnMainThread...
谢谢大家的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要考虑的一件事是
showText
以这些行结尾:然后下一行是:
那么,
showText 中
如果 viewController 甚至将其视图设置到下一行?您可能会向self.view
的值是多少nil
对象添加子视图。一般来说,你应该采取一些不同的做法。视图控制器的指定初始化程序是
initWithNibName:bundle
而不是init
,因此建议您使用它。更重要的是,
OverlayViewController
应该从 .xib 文件加载其视图,或者实现loadView
方法来创建其视图。如果您在该方法内部或在该方法之后调用的方法之一(例如viewDidLoad
)中添加标签,您将看到该标签。One thing to think about is that
showText
ends with these lines:and then the next line is:
So, what would the value of
self.view
be inshowText
if the viewController does even set its view until the next line? You are likely adding a subview to anil
object.Generally speaking, you should be doing things a little bit differently. The designated initializer for a view controller is
initWithNibName:bundle
rather thaninit
so it is recommended that you use that.More importantly, the
OverlayViewController
should either load its view from a .xib file or implement theloadView
method to create its view. If you add the label inside of that method or in one of the methods called after it, likeviewDidLoad
, you will see the label.