iphone - UILabel > 显示文本(记录已保存)一秒钟然后清除 UILabel 不起作用

标签 iphone objective-c ios uilabel

我正在开发联系人数据库应用程序。用户可以在 UITextFields 中输入姓名、电话、电子邮件,并将数据保存到 SQLite 数据库中。一切都按预期工作,但我想在数据成功保存时向用户显示确认信息。当记录保存到数据库时,实现了一个显示“已保存”的状态标签,但我希望文本在一秒钟后消失。

希望您能提供帮助,我尝试在 self.statusLabel.text = @"Saved"; 之后立即执行 sleep() 函数,但没有成功。使用下面的代码 我看到状态标签显示“已保存”,但这应该会在一秒钟后自动消失。

- (IBAction)saveButtonTapped:(id)sender
{
    sqlite3_stmt *statement;

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &timings) == SQLITE_OK) 
    {
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO TIMINGS (time) VALUES (\"%@\")", self.timeLabel.text];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(timings, insert_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE) 
        {
            self.statusLabel.text = @"Saved";
            self.timeLabel.text = @"0:00:00";

        }
        else 
        {
            self.statusLabel.text = @"Failed";
        }
        sqlite3_finalize(statement);
        sqlite3_close(timings);
    }
}

最佳答案

尝试使用

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];

否则,您可以考虑实现一个 NSTimer,它会在一秒钟后触发并清理标签。

关于iphone - UILabel > 显示文本(记录已保存)一秒钟然后清除 UILabel 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956579/

相关文章:

ios - 关于在 linkedin 中获取用户数据的问题

ios - 如何使用 beginBackgrounTaskWithExpirationHandler 有选择地指定在后台执行的内容 :

ios - Expo iOS 构建不工作 - -"Could Not Receive Latest API Key from App Store Connect"

ios - UITapGestureRecognizer Singletap 崩溃

iphone - UIButton 的不同状态不起作用

iphone - 如何让智能手机识别电话号码?

iphone - 在iphone中的UINavigationBar上添加UIImageView

iphone - 是否可以在不使用itunes的情况下安装到iOS应用程序

objective-c - 如何编写具有多个参数的方法/消息?

iOS 创建一个仅包含数字的动态文本字段