0

最初はメソッドを使用してサーバーにデータを送信するだけGETで、以下のような応答を受け取ります

2015-11-16 14:21:42.168 smartschool[1963:348015] Item actcode: ZQRTNN68
2015-11-16 14:21:42.169 smartschool[1963:348015] Item parentid: 8

次のラベルにアクティベーション コードを表示するにはどうすればよいですかviewController

これが私のコードです:

#import "RegistrationViewController.h"


@interface RegistrationViewController ()

@end

@implementation RegistrationViewController
{

    NSMutableData *mutableData;

#define URL @"http://192.168.1.166/bustracking/activation/requestActivationCode"
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(IBAction)sendDataUsingGet:(id)sender{

    [self sendDataToServer : @"GET"];


}

-(void) sendDataToServer : (NSString *) method
{

    NSString *parentName  = parent_name.text;
    NSString *contactNumber = contact_number.text;
    NSString *beaconid = @"145801000095";
    //NSString *beaconMacAdd = @"14:58:01:00:00:95";

    if(parentName.length > 0 && contactNumber.length > 0){

        NSLog (@"Getting response from server...");

        NSMutableURLRequest *request = nil;

        // Only Difference between POST and GET is only in the way they send parameters

        if([method isEqualToString:@"GET"]){

            NSString *getURL = [NSString stringWithFormat:@"%@?parent_name=%@&contact_number=%@&beacon_id=%@", URL, parentName, contactNumber, beaconid];
            //url = [NSURL URLWithString: getURL];
            getURL = [getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *url =  [NSURL URLWithString: getURL];
            request = [NSMutableURLRequest requestWithURL:url];

            NSLog(@"urlinfo: %@", url);
            NSLog(@"link: %@", getURL);
        }

        [request setHTTPMethod:method];
        [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        NSLog(@"connection: %@", connection);

        if( connection )
        {
            mutableData = [NSMutableData new];
        }
        else
        {
            NSLog (@"NO_CONNECTION");
            return;
        }
    }
    else
    {
        NSLog(@"NO_VALUES");
    }

}

#pragma mark NSURLConnection delegates

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    [mutableData setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutableData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog (@"NO_CONNECTION");
    return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error = nil;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:mutableData options:kNilOptions error:&error];
    NSArray *fetchedArr = [json objectForKey:@"response"];
    NSString *responseActCode;

    for (NSDictionary *user in fetchedArr)
    {
        responseActCode = [user objectForKey:@"activation_code"];
        NSLog(@"Item actcode: %@", responseActCode);
        NSLog(@"Item parentid: %@", [user objectForKey:@"parent_id"]);
        //NSLog(@"Item actcode: %@", [user objectForKey:@"activation_code"]);
    }

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:responseActCode forKey:@"HighScore"];
    [defaults synchronize];
    NSLog(@"from data: %@", [defaults objectForKey:@"HighScore"]);


    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Activation Code"
                          message:(@"%@", responseActCode)
                          delegate:nil //or self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];

    [alert show];
}

@end
4

2 に答える 2

1

返されるデータが辞書形式の場合、データを転送したい header.h クラスで nsdictionary を宣言します。同様に、配列と文字列を使用できます。ストーリーボードを使用している場合は、以下の方法です。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line

    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller

        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.dictionary  = self.dictionary.

    }
}

セルタップ時にデータを転送したい場合、これがあなたがしなければならないことです。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"YOUR_SEGUE_NAME_HERE"" sender:self];

}

「performSegueWithIdentifier」が呼び出されると、このメソッドは自動的に「prepareForSegue」メソッドにリダイレクトされます。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{


    NSIndexPath *indexPath= [self.tableView indexPathForSelectedRow];


    // Make sure your segue name in storyboard is the same as this line

    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller

        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
now this time its array.
        vc.array= [array[indexpath.row] valueforkey@"jsonkey"];

    }
}
于 2015-11-16T07:13:32.917 に答える