以下是我要完成的任务:用户触摸按钮,并可以与Facebook分享内容。设置用户触摸按钮的日期,并且用户不能再次与Facebook共享任何内容24秒。 24秒后,用户可以在Facebook上分享另一个帖子。
该错误与facebook无关,但它与elapsedTime有关。重置elapsedTime后,它不会倒数到24.它只会保持一个非常小的数字,如“0.0043”
这是我的代码:
-(void)postToFacebook
{
if (elapsedTime >= 24) {
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];
CCAppDelegate *app = (CCAppDelegate*) [[UIApplication sharedApplication] delegate];
SLComposeViewController *faceBookPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[faceBookPost setInitialText:@"test"];
[faceBookPost addURL:[NSURL URLWithString:@"http://www.google.com"]];
[[app navController] presentModalViewController:faceBookPost animated:YES];
faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
{
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
[[app navController] dismissModalViewControllerAnimated:YES];
dogeAmount.string = [NSString stringWithFormat:@"%.2LF", _doge];
[self setDate];
NSLog(@"Posted to Facebook");
};
} else if (elapsedTime < 24) {
NSLog(@"%f", elapsedTime);
}
}
以下是上面调用的SetDate:
-(void)setDate {
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
defaults = [NSUserDefaults standardUserDefaults];
startTime = [NSDate date];
calendar = [NSCalendar currentCalendar];
components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:startTime];
hour = [components hour];
minute = [components minute];
NSNumber *hourNumber = [NSNumber numberWithInteger:hour];
NSNumber *minuteNumber = [NSNumber numberWithInteger:minute];
[defaults setObject:startTime forKey:@"startTime"];
[defaults setObject:hourNumber forKey:@"hour"];
[defaults setObject:minuteNumber forKey:@"minute"];
[defaults setInteger:elapsedTime forKey:@"elapsedTime"];
NSLog(@"%@",dateString);
elapsedTime = fabs([startTime timeIntervalSinceNow]);
[defaults synchronize];
}
我无法弄清楚我在这里做错了什么。
答案 0 :(得分:1)
·H
@property (strong, nonatomic) NSDate *dateString;
的.m
- (void)share
{
double dateSinceNow = -[self.dateString timeIntervalSinceNow];
if (dateSinceNow > 24)
{
//share to facebook
}
self.dateString = [NSDate date];
}
答案 1 :(得分:0)
只需保存日期:
-(void)setStartTime {
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSDate date] forKey:@"startTime"];
}
稍后检索日期:
- (BOOL)isTimeExpired {
defaults = [NSUserDefaults standardUserDefaults];
NSDate *startedDate = [defaults objectForKey:@"startTime"];
NSTimeInterval elapsedTime = -[startedDate timeIntervalSinceNow];
return elapsedTime > 24;
}
注意:NSDate
只是自2001年1月1日格林威治标准时间第一瞬间以来的秒数。
答案 2 :(得分:0)
您不断地将[NSDate date]
与现在进行比较,这只是实例化与现在之间的时间。这就是它总是很小的原因。
另外,如果你想禁用一个按钮,为什么不禁用它并安排一个方法在24秒延迟后启用它?
答案 3 :(得分:0)
您的基本问题是,您在elapsedTime
方法中计算setDate
,但在发布Facebook后立即只调用setDate
,因此elapsedTime
永远不会在初始设定后改变。
我认为您还将发布日期存储在NSUserDefaults
中,以便您可以跟踪应用程序启动时的值?如果是这样,你在哪里读取价值?在viewDidLoad
?
我会删除elapsedTime
变量并创建postDate
@property
-
@property (strong,nonatomic) NSDate *postDate;
然后,在您的viewDidLoad
方法 -
self.postDate=[[NSUserdefaults standardUserDefaults] objectForKey:@"postDateKey"];
现在您的postToFaceBook
可以 -
-(void)postToFacebook
{
if (self.postDate == nil || [self.postDate timeIntervalSinceNow] > 24) {
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];
CCAppDelegate *app = (CCAppDelegate*) [[UIApplication sharedApplication] delegate];
SLComposeViewController *faceBookPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[faceBookPost setInitialText:@"test"];
[faceBookPost addURL:[NSURL URLWithString:@"http://www.google.com"]];
[[app navController] presentModalViewController:faceBookPost animated:YES];
faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
{
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
[[app navController] dismissModalViewControllerAnimated:YES];
dogeAmount.string = [NSString stringWithFormat:@"%.2LF", _doge];
self.postDate=[NSDate date];
[[NSUserDefaults standardDefaults] setObject:self.postDate forKey:@"postDateKey"];
NSLog(@"Posted to Facebook");
};
}
}
答案 4 :(得分:0)
这样做的简单方法是从performSelector获取一个空闲计时器:NSObject上的afterDelay:方法,
- (void)disablePosting {
// set a property that disallows posting, like a bool, or even better
// do something to the ui that disables, like disabling the button
self.postingEnabled = NO;
}
- (void)enablePosting {
self.postingEnabled = YES;
}
然后,要禁用24秒,请执行以下操作:
[self disablePosting];
[self performSelector:@selector(enablePosting) afterDelay:24];
你的FB代码如下:
if (self.postingEnabled) {
// do the fb request here, without any references to timeout other than
}
现在您可以将所有其他日期和elapsedTime相关代码输出。
一点小问题,记得取消对dealloc的执行请求
- (void)dealloc {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}