【问题标题】:How do I continually display the current time on an iOS app?如何在 iOS 应用上持续显示当前时间?
【发布时间】:2023-03-15 08:30:02
【问题描述】:

我正在Xcode 中创建一个单一视图应用程序,它只是在标签中每秒更新时间。我知道如何获取当前时间(使用NSDate),但是我要刷新它的方法在哪里?我在ViewController 中看到的只是以下方法:

- (void)viewDidLoad
- (void)didReceiveMemoryWarning

我在 AppDelegate 中看到的只是以下方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions                            
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationWillTerminate:(UIApplication *)application

据我所知,这些方法似乎都无法不断更新时间。我希望它显示时间有点像时钟(它不断更新)。我该怎么做?我期待有某种循环方法可以做到这一点,但看起来好像没有。

【问题讨论】:

标签: ios objective-c


【解决方案1】:

试试这个:)

 - (void)viewDidLoad
 {
    NSDateFormatter *timeformatter = [[NSDateFormatter alloc] init];
    [timeformatter setTimeStyle: NSDateFormatterShortStyle];
    [timeformatter setDateFormat:@"hh:mm:ss a"];

   [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(UpdateTime:) userInfo:nil repeats:YES];

 }

-(void)UpdateTime:(id)sender
{
   NSString *Time  = [timeformatter stringFromDate:[NSDate date]];
   NSLog(@"Time == %@",Time);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多