[英]Adding the multiple stopwatch label by clicking the button in Xcode
I am new to Xcode with Objective-C programming. 我是使用Objective-C编程的Xcode的新手。 I have the stopwatch program.
我有秒表程序。 I need to add the multiple stopwatch by clicking the button.
我需要通过单击按钮添加多个秒表。 Is there any way to do that?
有什么办法吗?
My existing code is here: 我现有的代码在这里:
stopwatchviewcontroller.h 秒表视图控制器
#import <UIKit/UIKit.h>
@interface StopWatchViewController : UIViewController {
UILabel *stopWatchLabel;
IBOutlet UIButton *btnStartStop;
NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
NSDate *startDate; // Stores the date of the click on the start button
}
@property (nonatomic, retain) UIButton *btnStartStop;
@property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
- (IBAction)AddStopwatch:(id)sender;
@end
stopwatchviewcontroller.m 秒表视图控制器
#import "StopWatchViewController.h"
@implementation StopWatchViewController
@synthesize stopWatchLabel;
- (void)dealloc
{
[stopWatchLabel release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[self setStopWatchLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
[dateFormatter release];
}
- (IBAction)onStartPressed:(id)sender {
if ([[btnStartStop titleForState:UIControlStateNormal]
isEqualToString:@"Start Clock"])
{
startDate = [[NSDate date]retain];
// Create the stop watch timer that fires every 10 ms
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
[btnStartStop setTitle:@"Stop Clock" forState:UIControlStateNormal];
}
else
{
//---stop the timer---
[timer invalidate];
//---change the caption back to "Start Clock"---
[btnStartStop setTitle:@"Start Clock" forState:UIControlStateNormal];
}
}
- (IBAction)AddStopwatch:(id)sender {
}
@end
I don't know what will do in Addstopwatch method, plz anyone provide me the solution for that. 我不知道Addstopwatch方法会做什么,请问有人为我提供了解决方案。 thanks in advance...
提前致谢...
If i understod your question, i think this should do it: 如果我理解您的问题,我认为应该这样做:
first of all you have to define a counter
as int
and give it a value of 0
in the ViewDidLoad
Method, the edit your code to this: 首先,您必须将一个
counter
定义为int
并在ViewDidLoad
方法ViewDidLoad
其赋值为0
,然后将代码编辑为:
stopwatchviewcontroller.h 秒表视图控制器
#import <UIKit/UIKit.h>
@interface StopWatchViewController : UIViewController {
UILabel *stopWatchLabel;
IBOutlet UIButton *btnStartStop;
IBOutlet UIButton *btnAddStopWatch;
NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
NSDate *startDate; // Stores the date of the click on the start button
int counter;
}
@property (nonatomic, retain) UIButton *btnStartStop;
@property (nonatomic, retain) UIButton *btnAddStopWatch;
@property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
- (IBAction)AddStopwatch:(id)sender;
@end
stopwatchviewcontroller.m #import "StopWatchViewController.h" stopwatchviewcontroller.m #import“ StopWatchViewController.h”
@implementation StopWatchViewController
@synthesize stopWatchLabel;
- (void)dealloc
{
[stopWatchLabel release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[self setStopWatchLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
counter = 0;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
[dateFormatter release];
}
- (IBAction)onStartPressed:(id)sender {
if ([[btnStartStop titleForState:UIControlStateNormal]
isEqualToString:@"Start Clock"])
{
startDate = [[NSDate date]retain];
// Create the stop watch timer that fires every 10 ms
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
[btnStartStop setTitle:@"Stop Clock" forState:UIControlStateNormal];
}
else
{
//---stop the timer---
[timer invalidate];
//---change the caption back to "Start Clock"---
[btnStartStop setTitle:@"Start Clock" forState:UIControlStateNormal];
}
}
- (IBAction)AddStopwatch:(id)sender {
counter+= 50;
UILabel *sL = [[UILabel alloc]initWithFrame:CGRectMake(0, stopWatchLabel.frame.origin.y+counter, 320, 40)];
sL.backgroundColor = [UIColor clearColor];
sL.textAlignment = UITextAlignmentCenter;
sL.text = stopWatchLabel.text;
sL.textColor = [uiColor blackColor];
[self.view addSubview:sL];
}
@end
Edit 编辑
You have to add another UIButton
for the AddStopwatch:
Method 您必须为
AddStopwatch:
添加另一个UIButton
AddStopwatch:
方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.