2014-03-13 52 views
-3

我想從json傳遞一些值給UIAlertView的消息。我有一些代碼如何從JSON調用NSString到UIAlertView?

- (void)jsonParse{ 

    NSString* path = @"http://phdprototype.tk/getResultData.php"; 
    NSURL* url = [NSURL URLWithString:path]; 
    NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 
    NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]; 

    NSDictionary* resultDic = [dic objectForKey:@"maxid"]; 
    NSString* recData = [resultDic objectForKey:@"recommendData"]; 
    NSString* rData = [resultDic objectForKey:@"room"]; 
    NSString* lData = [resultDic objectForKey:@"level"]; 

} 

- (void)locationView 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
                message:[NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@."] 
                delegate: self 
              cancelButtonTitle: nil 
              otherButtonTitles: @"GO", nil]; 

    [alert show]; 
} 

據我所知,我必須做一些
message:[NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@."]。但是,我不知道該怎麼做。有人能告訴我如何將lData和rData的值傳遞給uialertview的消息嗎?

+0

你應該學習Objective-C第一次啓動編碼之前。這會幫助你。 – Amar

回答

2

,您可以撥打全球NSString變量定義你的消息

.h文件

NSString *message; 

jsonParse方法

- (void)jsonParse { 
    //Your Stuff 
    message = [NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@", lData, rData]; 
} 

,然後在UIAlertView

UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"Recommendation" 
                message:message 
                delegate:self 
              cancelButtonTitle:nil 
              otherButtonTitles:@"GO", nil]; 

[alert show]; 
+0

你如何使用其他函數的局部變量? –

+1

編輯了答案,你可以查詢 –

0

其實很簡單。你正在做的事情只是少數缺失的事情。

  1. 聲明rData & lData爲屬性在你的類,

    @interface <classname> 
    @propert (nonatomic,strong) NSString *lData; 
    @propert (nonatomic,strong) NSString *rData; 
    @end 
    
  2. 然後你的字符串可以使用形成

    [NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@.", lData, rData]; 
    

這應該解決您的問題。

+0

我已經嘗試過,但它仍然顯示NSString * rData = [resultDic objectForKey:@「room」]中未使用的變量lData和rData。這就是爲什麼我有點困惑 – Richard

+0

添加這個> @implementation > @synthesis lData,rData; > @end並使用[NSString stringWithFormat:@「OK!還有另一個集合位於%@的%@房間%@。」,lData,rData]; – Nilesh

1
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
               message:[NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@.", iData, rData] 
               delegate: self 
             cancelButtonTitle: nil 
             otherButtonTitles: @"GO", nil]; 

你寫的論據stringWithFormat後,只要寫的值。參數的數量應該與參數字符串中使用的%@s的數量相匹配,並且您的參數應該用逗號分隔,否則您將收到構建錯誤。

0

同時聲明變量(RDATA & LDATA)全球(手段,在你的界面),然後使用它像這樣

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
                message:[NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@.",lData,rData] 
                delegate: self 
              cancelButtonTitle: nil 
              otherButtonTitles: @"GO", nil]; 
2
- (void)jsonParse{ 

    //your code 
    [self locationViewWithRoom:rData level:lData]; 

} 

- (void)locationViewWithRoom:(NSString *)room level:(NSString *)level 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
                message:[NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@.", room, level] 
                delegate: self 
              cancelButtonTitle: nil 
              otherButtonTitles: @"GO", nil]; 

    [alert show]; 
} 
+0

但是如何調用這個方法? - (void)locationViewWithRoom:(NSString *)房間級別:(NSString *)級別。如果按鈕調用 - (void)jsonParse方法將在我的代碼標記後被我的字符串調用,那麼當按下按鈕 – Richard

+0

時,我需要調用此alertview。 – Crutched