如何致电从JSON到UialertView的NSString



我想将一些值从json传递到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 ??

的消息

您可以制作一个全局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];
- (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];
}
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的数量匹配,并且您的参数应分开,否则您将获得构建错误。

在全球范围内声明两个变量(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];

实际上很容易。您正在做对了很少的事情。

  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];
    

这应该解决您的问题。

最新更新