当我点击第一个视图中的按钮时,我想直接通过segue将json数据传递给我的第二个视图

时间:2012-12-08 07:22:42

标签: ios json uiviewcontroller segue

我从网址获取json数据:http://api.kivaws.org/v1/loans/search.json?status=fundraising我在屏幕上显示了这些数据,但是我必须通过secondviewcontroller将数据直接发送到我的segue而不绑定它们在我的firstviewcontroller中,点击firstviewcontroller

中的按钮
#import "ViewController.h"

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 
#define kLatestKivaLoansURL [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] 

@implementation ViewController
@synthesize textinput;

- (IBAction)loadData {
        dispatch_async(kBgQueue, ^{
           NSData* data = [NSData dataWithContentsOfURL:
                                kLatestKivaLoansURL];
           [self performSelectorOnMainThread:@selector(fetchedData:)
                                       withObject:data waitUntilDone:YES];
        });
 }

- (void)fetchedData:(NSData *)responseData {

    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //1
                          options:kNilOptions
                          error:&error];

    NSArray* latestLoans = [json objectForKey:@"loans"]; //2

    NSLog(@"loans: %@", latestLoans); //3
    [textinput resignFirstResponder];

   if ([textinput.text isEqualToString:@""])
   {
         display.text = @"Enter The Value";

   }else{
        if ([textinput.text isEqualToString:@"loans"])
          {

             NSDictionary* loans = [latestLoans objectAtIndex:0];


             NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                 lblWHO.text = [loans objectForKey:@"name" ],
                 lblActivtiy.text =  [loans objectForKey:@"activity"],nil];
                  lblLoan.text = [(NSDictionary*)[loans objectForKey:@"location"]
                   objectForKey:@"country"], nil;


             NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info
                                               options:NSJSONWritingPrettyPrinted error:&error];
          }   
      }
 }

我将firstviewcontroller中的数据通过segue传递给secondviewcontroller,如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"ShowCustDetail"])
    {
          self.showact = Activity.text;
          self.showname = Name1.text;
          self.showcount = Country.text;

        CustViewController *cv = [segue destinationViewController];
     cv.showact = self.showact;
     cv.showname = self.showname;
     cv.showcount = self.showcount;
    }
}  

但是我必须将这些数据直接绑定到我的destinationviewcontroller中,而不必将它放在我的firstviewcontroller中

  #import "CustViewController.h"

  @interface CustViewController ()
  @end

  @implementation CustViewController
  @synthesize Name1;
  @synthesize showname;
  @synthesize showact;
  @synthesize showcount;
  @synthesize Name2;
  @synthesize Name3;

  - (void)viewDidLoad
   {
     [super viewDidLoad];

      Name1.text = self.showact;
      Name2.text = self.showname;
      Name3.text = self.showcount;
      Name4.text = self.showstat;
      Name5.text = self.showsect;

   }



   @end

感谢您与我们联系。如果我得到解决方案,这很有用。

0 个答案:

没有答案