在tweetSheets initialText(iOS7)中显示格式化日期

时间:2013-11-12 15:45:12

标签: ios json slcomposeviewcontroller

我想给我的用户选择"分享"我的应用详细视图页面。

一旦用户点击"推文这个"我希望initialText包含一个用JSON解析的日期字符串(' release_date')。我无法以可读格式显示日期字符串。

这是我格式化日期的方式(在NSObject中)

- (NSString *) formattedDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
    NSDate *readableDate = [dateFormatter dateFromString:self.release_date];

    [dateFormatter setDateFormat:@"MMMM dd"];
    return [dateFormatter stringFromDate:readableDate];
}

我的release_date是NSString(这是JSON的外观)

release_date: "2013-11-16T00:00:00.000Z"

这就是我在我的detailedViewController

中显示formattedDate的方式
- (void)viewDidLoad
{
    [super viewDidLoad];
    if([_singleRelease objectForKey:@"release_date"] != NULL)
    {
        NSString *readableDate = [_singleRelease objectForKey:@"release_date"];
        UpcomingRelease *singleRelease = [[UpcomingRelease alloc] init];
        singleRelease.release_date = readableDate;

        self.release_date.text = [NSString stringWithFormat:@"%@", singleRelease.formattedDate];
    }
}

我的SLComposeViewController

- (IBAction)shareOnTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:[NSString stringWithFormat:@"%@ is releasing on %@, via @soleresource", [_singleRelease objectForKey:@"release_name"], [_singleRelease objectForKey:@"release_date"]]];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
}

由于

1 个答案:

答案 0 :(得分:0)

您的代码有点混乱。一种选择是这样的:

- (NSString *) formattedDate:(NSString *)jsonDateString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
    NSDate *readableDate = [dateFormatter dateFromString:jsonDateString];

    [dateFormatter setDateFormat:@"MMMM dd"];

    return [dateFormatter stringFromDate:readableDate];
}

- (IBAction)shareOnTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        NSString *formattedDate = [self formattedDate:[_singleRelease objectForKey:@"release_date"]];
        [tweetSheet setInitialText:[NSString stringWithFormat:@"%@ is releasing on %@, via @soleresource", [_singleRelease objectForKey:@"release_name"], formattedDate]];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
}