I would like to pass some values from json to a message of UIAlertView. I have some codes

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

As I know, I have to do something in
message:[NSString stringWithFormat:@"OK! There is another collection which located at room %@ in level %@."]. However, I have no idea how to do it. Can someone tell me how to pass the values of lData and rData to the message of uialertview??

有帮助吗?

解决方案

You can make a global NSString variable which defines you message

In .h file

NSString *message;

In jsonParse method

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

and then in your 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];

Just write the values after you write the argument for stringWithFormat. The number of your arguments should match with the number of %@s used in argument string, and your arguments should be comma separated, otherwise you will get a build error.

Declare both the variables (rData & lData) globally (means, in your interface) and then use it like this

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

Its actually pretty easy. You are doing right just few things are missing.

  1. Declare rData & lData as properties in your class,

    @interface <classname>
    @propert (nonatomic,strong) NSString *lData;
    @propert (nonatomic,strong) NSString *rData;
    @end
    
  2. Then your string can be formed using,

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

This should resolve your issue.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top