iphone - 在一段时间内显示标签

标签 iphone cocoa-touch ios nstimer

例如,对于我的 iPhone 应用程序,我需要一个在指定时间内可见的标签。我该如何管理?
显示例如可见 10 秒但随后从 View 层次结构中删除的标签?

在此先感谢您的帮助! :)

最佳答案

显示标签,然后开始 NSTimer ,其超时回调方法隐藏标签。 (我隐藏标签而不是从 View 层次结构中删除它,这可能更合适也可能不合适。)

代码类似于我在 NSTimers and triggers in Obj-C 中的回答

MyViewController.h:

...
@interface MyViewController : UIViewController
{
    ...
    UILabel* label;
    NSTimer* timer;
    ...
}
...

MyViewController.m:
...
static const NSTimeInterval TIMER_INTERVAL = 10.0;
...
- (void)dealloc
{
    [self stopTimer];
    ...
    [super dealloc];
}
...
- (void)showLabelAndStartTimer
{
    label.hidden = NO;
    [self startTimer];
}
...
- (void)startTimer
{
    [self stopTimer];

    timer = [NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL
                                             target:self
                                             selector:@selector(timerCallback)
                                             userInfo:nil
                                             repeats:NO];
    [timer retain];
}
...
- (void)stopTimer
{
    if (timer)
    {
        [timer invalidate];
        [timer release];
        timer = nil;
    }
}
...
- (void)timerCallback
{
    label.hidden = YES;
}

关于iphone - 在一段时间内显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4364880/

相关文章:

objective-c - 从自定义 UITableViewCell 获取值

ios - 在 xCode 中指定不同的图像

ios - 如何在 Objective-C 中转换以下 Swift 代码?

javascript - Titanium - 在 iOS 上使用带数字键盘的 secure_text_input createAlertDialog?

iphone - 如何增加 NSString 类型变量的保留计数?

iphone - 调整图像大小 Objective C

iphone - 应用内购买项目的促销代码

iphone - 由于 DNS 查找导致 NSURLConnection 延迟?

ios - 使用 LAContext 和 touchIDAuthenticationAllowableReuseDuration 获取钥匙串(keychain)项目

iphone - copyWithZone 问题