How can I display the current time on a shortcut?

How can I display the current time on the iphone shortcut?

+4
source share
4 answers

Ok Here is what you need.

Place the IBOutlet shortcut in your .h file of your view controller and connect to the .xib file.

Now just put the following code in your .m controller file.

- (void)viewDidLoad { [super viewDidLoad]; // your label text is set to current time lblTime.text=[[NSDate date] description]; // timer is set & will be triggered each second [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES]; } // following method will be called frequently. -(void)showTime{ lblTime.text=[[NSDate date] description]; } 
+7
source

Objective-c

 NSDate *currDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:currDate]; yourLabel.text=dateString 

Swift 3.0

 var currDate = Date() var dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd.MM.YY HH:mm:ss" var dateString = dateFormatter.string(fromDate: currDate) yourLabel.text! = dateString 
+7
source

Use NSTimer to run regular calls to a method that updates the label.

0
source

Source: https://habr.com/ru/post/1305749/


All Articles