编程语言
首页 > 编程语言> > javascript – 将NSDate传递给iOS中的本地UIWebView – Objective C

javascript – 将NSDate传递给iOS中的本地UIWebView – Objective C

作者:互联网

我有一个带有菜单的应用程序,可以加载从本地HTML文件加载的各种UIWebView. (此代码位于WebViewController中)

//Code for the WebView
vesperswebview.scrollView.bounces = NO;
[[vesperswebview scrollView] setBounces:NO];
[(UIScrollView*)[vesperswebview.subviews objectAtIndex:0] setShowsVerticalScrollIndicator:NO];

NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"data/html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSString *theAbsoluteURLString = [url absoluteString];
NSString *queryString = @"#?manifest=Psalmody-Morning";
NSString *absoluteURLwithQueryString = [theAbsoluteURLString stringByAppendingString:queryString];
NSURL *finalURL = [NSURL URLWithString:absoluteURLwithQueryString];

NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0];
[vesperswebview loadRequest:request];

我还有一个名为FlatDate Picker的自定义日期选择器:(此代码在我的主ViewController中),它链接到我从GitHub获得的导入的FlatDatePicker开源项目.

- (void)flatDatePicker:(FlatDatePicker*)datePicker dateDidChange:(NSDate*)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *coptic = [[NSCalendar alloc] initWithCalendarIdentifier:@"coptic"];
    [dateFormatter setCalendar:coptic];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];

    if (datePicker.datePickerMode == FlatDatePickerModeDate) {
        [dateFormatter setDateFormat:@"EEEE, MMMM, dd, yyyy"];
    } else if (datePicker.datePickerMode == FlatDatePickerModeDate) {
        [dateFormatter setDateFormat:@"HH:mm:ss"];
    } else {
        [dateFormatter setDateFormat:@"EEEE, MMMM, dd, yyyy HH:mm:ss"];
    }
    NSString *value = [dateFormatter stringFromDate:date];
    self.labelDateSelected.text = value;
}

我的问题是,当我在主视图控制器中选择日期时,如何保存该日期并将其传递到下一个视图以加载在我的数据库中对日期敏感的相应Javascript?

解决方法:

鉴于FlatDatePicker的外观,从中获取的日期可以转换为以下JavaScript DateFormats

ISO Date “2015-03-25” (The International Standard)

Short Date “03/25/2015” or “2015/03/25”

Long Date “Mar 25 2015” or “25 Mar 2015”

将NSDate转换为这些格式.可以完成以下操作

NSDateFormatter *ISODateFormatter = [[NSDateFormatter alloc] init];
[ISODateFormatter setDateFormat:@"yyyy-mm-dd"];

NSDateFormatter *ShortDateFormatter = [[NSDateFormatter alloc] init];
[ShortDateFormatter setDateFormat:@"yyyy/mm/dd"];

NSDateFormatter *LongDateFormatter = [[NSDateFormatter alloc] init];
[LongDateFormatter setDateFormat:@"dd MMM yyyy"];

NSString *ISODate = [ISODateFormatter stringFromDate:date];
NSString *ShortDate = [ShortDateFormatter stringFromDate:date];
NSString *LongDate = [LongDateFormatter stringFromDate:date];

您可以将此日期保存在selectedDate变量中.然后,您可以像这样在UIViewController中拥有一个init属性

@interface JSViewController ()

@property (nonatomic, strong) NSString *currentDate;

@end

-(instancetype)initWithDate:(NSString *)jsDate {
     self = [super initWithNibName:NULL bundle:NULL];
     if (self) {
       _currentDate = jsDate;
     }
     return self;
 }

您可以通过以下方式从另一个初始化并启动此ViewController.

JSViewController *jsVC = [[JSViewController alloc] initWithDate:ISODate];
[self presentViewController:jsVC animated:YES completion:nil];

试一试.如果它不起作用,请随意ping我.随意建议编辑,使其更好.

标签:javascript,ios,objective-c,nsdate,uiwebview
来源: https://codeday.me/bug/20190702/1352562.html