Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 操作停止并在故事板之间重置_Ios_Objective C_Iphone_Xcode6_Xcode Storyboard - Fatal编程技术网

Ios 操作停止并在故事板之间重置

Ios 操作停止并在故事板之间重置,ios,objective-c,iphone,xcode6,xcode-storyboard,Ios,Objective C,Iphone,Xcode6,Xcode Storyboard,我的主故事板有一个秒表。我可以启动手表,然后导航到另一个故事板。当我返回到主秒表情节串连板时,它已停止并重置为零。请帮我弄清楚如何让秒表一直走到我按下停止键为止。谢谢 所有故事板都有相同的控制器 here is the .h #import <UIKit/UIKit.h> @interface MainViewController : UIViewController{ UILabel *lbl; NSTimer *stopTimer; NSDate *startDate; BO

我的主故事板有一个秒表。我可以启动手表,然后导航到另一个故事板。当我返回到主秒表情节串连板时,它已停止并重置为零。请帮我弄清楚如何让秒表一直走到我按下停止键为止。谢谢

所有故事板都有相同的控制器

here is the .h

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController{
UILabel *lbl;
NSTimer *stopTimer;
NSDate *startDate;
BOOL running;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (strong,nonatomic) IBOutlet UILabel *lbl;

-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;

-(void)updateTimer;

@end



here is the .m

#import "MainViewController.h"
#import "SWRevealViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController
@synthesize lbl;

- (void)viewDidLoad

//stopwatch open
{
[super viewDidLoad];
lbl.text = @"00.00.00.000";
running = FALSE;
startDate = [NSDate date];
//stopwatch close

[super viewDidLoad];

self.title = @"therapyTools";

// Change button color
_sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.9f];

// Set the side bar button action. When it's tapped, it'll show up the sidebar.
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);

// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

//stopwatch open
-(IBAction)startPressed:(id)sender{
if(!running){
    running = TRUE;
    [sender setTitle:@"STOP" forState:UIControlStateNormal];
    if (stopTimer == nil) {
        stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                     target:self
                                                   selector:@selector(updateTimer)
                                                   userInfo:nil
                                                    repeats:YES];
    }
}else{
    running = FALSE;
    [sender setTitle:@"START" forState:UIControlStateNormal];
    [stopTimer invalidate];
    stopTimer = nil;
}

}
-(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];
lbl.text = timeString;
}

-(IBAction)resetPressed:(id)sender{
[stopTimer invalidate];
stopTimer = nil;
startDate = [NSDate date];
lbl.text = @"00.00.00.000";
running = FALSE;
}
//stopwatch close

@end

您的意思是在一个情节提要中的控制器场景之间导航,还是确实有多个情节提要文件?你是如何来回导航的?对不起,一个故事板。我尝试过在多个场景中使用一个viewcontroller。我现在有一个单独的秒表.h和.m视图控制器。在场景之间切换时仍会关闭。我现在明白了,当我离开视图时,视图将被释放。我还知道我需要将计时器放在appdeligate或其他未连接到视图的文件中。我不知道怎么做。我已经尝试过将所有的秒表代码和一部分代码移到appdelegate中,但在编程中我还没有做到这一点。我读到一些开发人员覆盖了特定控制器的卸载。我希望它做得正确,而不仅仅是一个解决办法。但是这没关系,那么我想知道当我离开一个viewcontroller时,如何阻止它卸载。如果您使用模式或推送序列移动到第二个控制器,则不应释放第一个控制器,因此您的理解是错误的。当你离开时,它真的被释放了吗?你可以把一个日志放在dealloc中进行测试吗?你打算怎么回去?我正在使用SWRevealViewController使用滑出菜单。