2012-10-10 67 views
0

我是Objective-C編程的Xcode的新手。我有秒錶程序。我需要通過點擊按鈕添加多個秒錶。有沒有辦法做到這一點?通過單擊Xco​​de中的按鈕添加多個秒錶標籤

我現有的代碼是在這裏:

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 

我不知道會在Addstopwatch方法做,PLZ有人提供給我爲此的解決方案。 在此先感謝...

+0

又該Addstopwatch方法嗎??? – Mutawe

+0

我想添加多秒錶標籤,當我點擊addstopwatch按鈕。 –

+0

我添加了一個答案,希望它可以幫助你 – Mutawe

回答

0

如果我understod你的問題,我認爲這應該這樣做:

首先你必須定義一個counterint,並給它的0值在ViewDidLoad方法,編輯您的代碼如下:

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 #進口 「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 

編輯

你得再添UIButtonAddStopwatch:方法

+0

嗨Jossef,這似乎有一些錯誤,如「未定義的標識符計數器」。我能做些什麼 –

+0

對不起,jossef我可能沒有在ViewDidLoad方法中定義計數器 –

+0

編輯我的回答 – Mutawe