0

I have a function like so: (and ps - I am new to ios development)

- (void)loadJSON
{

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];

            NSData *data = [NSData dataWithContentsOfURL:url options:0 error:nil];

            NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

            NSArray *firstItemArray = array[0];

            NSString *yesNoString = firstItemArray[0];
            NSString *dateString = firstItemArray[1];
            NSString *timeString = firstItemArray[2];
            NSString *homeString = firstItemArray[3];
            NSString *awayString = firstItemArray[4];
            NSString *lastUpdatedString = firstItemArray[5];
            NSString *previousIsOpen = firstItemArray[6];
            NSString *previousDate = firstItemArray[7];
            NSString *previousHome = firstItemArray[8];
            NSString *prviousHomeScore = firstItemArray[9];
            NSString *previousAway = firstItemArray[10];
            NSString *previousAwayScore = firstItemArray[11];

            dispatch_async(dispatch_get_main_queue(), ^{
                self.YesOrNo.text = yesNoString;
                self.date.text = [@"For " stringByAppendingString:dateString];
                self.time.text = timeString;
                self.home.text = homeString;
                self.away.text = awayString;
                self.lastUpdated.text = lastUpdatedString;
                self.lastUpdatedText.text = @"Last Updated";
                self.vs.text = @"vs";
            });
        });
}

and I have all of those previous strings defined and I need to call those variables in my button action alert:

- (IBAction)PreviousResults:(id)sender {
    UIAlertView *previousalert = [[UIAlertView alloc] initWithTitle: @"Previous Results" message: @previousIsOpen previousDate previousHome previousHomeScore previousAway previousAwayScore delegate: self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [previousalert show]; [previousalert release];
}

What would be the best way to do this?

4

3 回答 3

1

在更全局的范围内定义这些变量,例如在接口文件中。

通过在接口文件(.h 文件)中定义变量,您将可以在实现文件的任何方法中访问它。(.m 文件)

于 2013-08-26T04:38:57.620 回答
0

NSArray您可以在此类中使用实例。当你解析 JSON 数据时,你初始化它NSArray,然后你可以NSArray在这个类中使用这个实例。

于 2013-08-26T04:40:42.980 回答
0

在 .h 类中声明这些变量,然后在 PreviousResults 方法中使用这些变量。这些变量称为实例变量。

于 2013-08-26T04:37:16.330 回答