我正在尝试设置倒数计时器。我有两个视图控制器:ViewController和EditViewController。 ViewController有两个标签小时和分钟。 EditViewController有一个UIDatePicker和一个保存按钮。当用户在UIDatePicker上选择时间并点击保存时,会将它们重定向回ViewController,并使用所选时间和当前时间之间的差异填充小时和分钟标签。这将自动更新,直到差异为0.我在尝试获取在ViewController标签上正确显示的小时和分钟时遇到问题。这是我的代码:
ViewController.m
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.hourLabel.text = self.hourText;
self.minuteLabel.text = self.minuteText;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
EditViewController.h
@interface EditViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet NSString *currentTimeString;
@property (weak, nonatomic) IBOutlet NSString *endTimeString;
- (IBAction)saveButton:(id)sender;
- (IBAction)cancelButton:(id)sender;
-(void)updateTime;
@end
EditViewController.m
#import "EditViewController.h"
#import "ViewController.h"
@interface EditViewController ()
@end
@implementation EditViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveButton:(id)sender
{
NSCalendar *now = [NSCalendar autoupdatingCurrentCalendar];
NSInteger currentTime = (NSHourCalendarUnit | NSSecondCalendarUnit);
self.datePicker.date = [now dateFromComponents:[now components:currentTime fromDate:self.datePicker.date]];
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)updateTime
{
NSInteger timeLeft = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger minutes = (timeLeft / 60) % 60;
NSInteger hours = (timeLeft / 3600) % 24;
ViewController *controller;
controller.hourText = [NSString stringWithFormat:@"%ldi",(long)hours];
controller.minuteText = [NSString stringWithFormat:@"%ldi",(long)minutes];
NSLog(@"The time is %ld",(long)timeLeft);
}
- (IBAction)cancelButton:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
感谢任何帮助。提前谢谢。
答案 0 :(得分:0)
几个问题:
ViewController.m
self.hourLabel.text = self.hourText;
self.minuteLabel.text = self.minuteText;
应该在viewWillAppear
而不是viewDidLoad
。发生了什么事情是你在应用程序加载时设置文本。对于该应用的实例,viewDidLoad
只会触发一次。你需要做的是从EditViewController回来时更新它,对吗?因此,如果您将其放入viewWillAppear
,当您回来并为您更新时,它也会被调用。
接下来的事情:
EditViewController.m
ViewController *controller;
controller.hourText = [NSString stringWithFormat:@"%ldi",(long)hours];
controller.minuteText = [NSString stringWithFormat:@"%ldi",(long)minutes];
NSLog(@"The time is %ld",(long)timeLeft);
此代码片段声明了一个新的ViewController变量(好吧,大多数情况下,你都缺少= [ViewController new]
部分)。这个问题是你没有对这个控制器做任何事情。 ViewController实际上已经在你的导航堆栈中了(我假设当用户回来时我就假设了NavigationController),所以你需要使用你的应用已经实例化的ViewController引用,而不是创建一个新的,因为你从来没有使用过这个人任何地方。
答案 1 :(得分:0)
在这里查看代码中已编辑的方法。这将完全满足您的要求。
#import "EditViewController.h"
#import "ViewController.h"
#import "AppDelegate.h"
@interface EditViewController ()
{
AppDelegate *appDelegate;
}
@end
@implementation EditViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
return self;
}
-(void)updateTime
{
NSInteger timeLeft = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger minutes = (timeLeft / 60) % 60;
NSInteger hours = (timeLeft / 3600) % 24;
NSString *hourText = [NSString stringWithFormat:@"%ldi",(long)hours];
NSString *minuteText = [NSString stringWithFormat:@"%ldi",(long)minutes];
[appDelegate updateTime:hourText andMinute:minuteText];
}
最后,在AppDelegate类中定义一个方法
- (void) updateTime:(NSString *)hourStr andMinute:(NSString *)minuteStr
{
for(id obj in [self.navigationController viewControllers])
{
if([obj isKindOfClass:ViewController])
{
ViewController *vcObj = (ViewController *)obj;
vcObj.hourLabel.text = hourStr;
vcObj.minuteLabel.text = minuteStr;
}
}
}