2011-11-21 13 views
0

我已將自定義方法(-(void)loadXML {})定義到我的appDelegate中。 現在我想在幾個viewController中使用它;現在我使用本地NSDate對象。NSDate作爲customMethod中的參數

NSDate *todayDate = [NSDate date]; 
    NSString *XMLUrl = @"http://localhost/MyApp/GetXML?&aDate="; 
    NSString *urlString = [NSString stringWithFormat:@"%@%@", XMLUrl, todayDate]; 
    tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:urlString]]; 

而不是'todayDate'我想要'selectedDate';還有我如何在我的方法中添加一個bool,需要在我的方法中添加一些條件?

+0

如何存儲'selectedDate'?而關於'開機'...你是什麼意思?你想在應用程序啓動時調用你的方法嗎? – makaron

+1

' - (void)loadXML:(NSDate *)selectedDate {}'?? – beryllium

+0

@makaron:對不起,我輸入錯了,我的意思是bool不能啓動;現在我的應用程序調用了一個方法來解析todayDate中的xml,但我使用(Si-Calendar)[https://github.com/voidparadox/Si-Calendar]並想從日曆中選擇日期解析xml –

回答

1

這裏是你可以做什麼:

NSDate *selectedDate = ???????; // set this to whatever you want selectedDate to be 
BOOL myBoolean = YES; 
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; 

[dateFormat setDateFormat: @"yyyy-MM-dd"]; // or whatever format you wish 
NSString *urlString = 
    [NSString stringWithFormat:@"http://localhost/MyApp/GetXML?BOOL=%@&aDate=%@", 
    (myBoolean ? @"YES" : @"NO"), 
    [dateFormatter stringFromDate: selectedDate]]; 

tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:urlString]]; 
[dateFormatter release]; // don't forget to release, if not using ARC 

而且爲了你的利益,我向您展示如何使用NSDateFormattergeneric C ternary conditional

我希望這可以幫助你!

+0

在didFinishLunchingWithOptions [這裏是示例]調用我的loadXML方法(http://pastie.org/private/g5lpjuoobzdxgvbvbh8y4a)崩潰我的應用程序 –

+1

分配後襬脫'autorelease'和啓動你的日期格式化程序。 –

+0

哦,對不起;謝謝! –