1

我从 url 获取 json 数据:http ://api.kivaws.org/v1/loans/search.json?status=fundraising我在我的屏幕上得到它,但我必须将数据直接发送到我的secondviewcontrollerthroughsegue无需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];
          }   
      }
 }

我将绑定在 my 中的数据传递firstviewcontrollerseguesecondviewcontroller如下所示:

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

但是我必须将这些数据直接绑定到我的目标视图控制器中,而不是在我的第一个视图控制器中

  #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

感谢您将我与你们联系起来。如果我得到解决方案很有用。

4

0 回答 0