【问题标题】:Adding the multiple stopwatch label by clicking the button in Xcode通过单击 Xcode 中的按钮添加多个秒表标签
【发布时间】:2012-10-10 05:56:28
【问题描述】:

我是 Xcode 的新手,使用 Objective-C 编程。我有秒表程序。我需要通过单击按钮添加多个秒表。有什么办法吗?

我现有的代码在这里:

秒表视图控制器.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

秒表视图控制器.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 方法中会做什么,请任何人为我提供解决方案。 提前谢谢...

【问题讨论】:

  • Addstopwatch 方法应该做什么???
  • 我想在单击添加秒表按钮时添加多个秒表标签。
  • 我已经添加了一个答案,希望对你有帮助

标签: objective-c xcode uibutton nstimer stopwatch


【解决方案1】:

如果我理解你的问题,我认为应该这样做:

首先,您必须将counter 定义为int,并在ViewDidLoad 方法中为其赋值0,将您的代码编辑为:

秒表视图控制器.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

秒表视图控制器.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: 方法添加另一个UIButton

【讨论】:

  • 嗨,Jossef,似乎有些错误,例如“未定义的标识符计数器”。我能做些什么呢
  • 抱歉,jossef 我可能没有在 ViewDidLoad 方法中定义计数器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多