2012-10-10 4 views
0

Objective-C 프로그래밍을 사용하는 Xcode를 처음 사용했습니다. 나는 스톱워치 프로그램을 가지고있다. 버튼을 클릭하여 여러 개의 초시계를 추가해야합니다. 그것을 할 방법이 있습니까?Xcode의 버튼을 클릭하여 다중 스톱워치 레이블 추가

내 기존 코드는 여기에 있습니다 :

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 내가 Addstopwatch 방법에 무엇을 할 것인지 모르는

 #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 

, PLZ 누군가가 나에게 제공 그 해결책. 사전에 덕분에 ...

+0

또 다른 UIButton을 추가해야 할 방법을 무엇을 Addstopwatch해야 할 ??? – Mutawe

+0

addstopwatch 버튼을 클릭하면 여러 스톱워치 라벨을 추가하고 싶습니다. –

+0

Ive가 답변을 추가했습니다. 도움이 되었으면 – Mutawe

답변

0

내가 질문을 understod 경우, 나는이 그것을 할해야한다고 생각 : 모든

첫 번째는 당신이 intcounter을 정의하고 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"

@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 

편집

당신은 AddStopwatch: 방법

+0

안녕 Jossef 님, "undefined identifier counter"같은 오류가있는 것 같습니다. 그게 뭘 할 수 있습니까 –

+0

죄송합니다, jossef 내가 ViewDidLoad 메서드에서 카운터를 정의하지 않을 수 있습니다 –

+0

편집 내 대답 – Mutawe

관련 문제