如何替换视图控制器之间的值?

时间:2014-09-13 16:25:13

标签: ios json uipickerview segue nsurl

我正在构建一个iOS应用程序,在第一个ViewController加载时解析JSON数据。在另一个ViewController中,我有一个UIPicker,它包含一个传递自己相应url的数组对象,以及一个将segue调回第一个ViewController的UIButton。但是,当我调用segue时,View Controller仍会从初始nsurl解析相同的数据,而不是调用替换它的数据。我如何解决这个问题,以便当我从UIPicker中选择一个对象并将数据传递给下一个控制器时,第一个视图控制器会相应地解析该数据?

这是我的代码:

MainVC.m

-(void)viewDidLoad{

[super viewDidLoad];

_bburl = [NSURL URLWithString:@"http://www.example.com/api/sites?count=75"];
    NSData *jsonData = [NSData dataWithContentsOfURL:_bburl];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:_bburl];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:_bburl completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error){
        NSLog(@"%@", response);

        NSData *data = [[NSData alloc]initWithContentsOfURL:location];

        NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];



. . . . .

}

MenuVC.m

-(void)viewDidLoad{

[super viewDidLoad];

 self->catArray = [[NSArray alloc] initWithObjects:@"All",@"4-Letter", @"Animal", @"Auction", @"Blog", @"Business", @"Celluar", @"Children",@"Community", @"Consulting", @"Dating", @"Design", @"Download", nil];


 self->URLArray = [[NSMutableArray alloc]init];

    [URLArray addObject: @"https://www.example.com/api/sites/?count=75"];

    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=4-letter"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=animal"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=auction"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=blog"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=business"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=cellular"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=children"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=community"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=consulting"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=dating"];
    [URLArray addObject: @"https://www.exapmle.com/api/sites/?count=75&category=design"];
    [URLArray addObject: @"https://www.example.com/api/sites/?count=75&category=download"];



 self->picker = catArray;

}



-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    // This method returns the number of components we want in our Picker.
    // The components are the colums.

    return 1;

}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    // This returns the number of rows in each component. We use the count of our array to determine the number of rows.

   return [self->catArray count];

    return 0;

}

#pragma mark Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return [self->catArray objectAtIndex:row];

    return 0;

}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
   {
       selectedRow = [pickerView selectedRowInComponent:0];

       NSString *catURLString = [URLArray objectAtIndex:selectedRow];
       catURL = [NSURL URLWithString:catURLString];
       MainViewController *mainVC = [[MainViewController alloc]init];
       mainVC.bburl = catURL;

       NSLog(@"%d, %@", selectedRow, catURL);
   }


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"backFromMenuSegue"])   {
    NSString *catURLString = [URLArray objectAtIndex:[self->picker selectedRowInComponent:0]];
    catURL = [NSURL URLWithString:catURLString];

    MainViewController *mainVC = segue.destinationViewController;
        mainVC.bburl = catURL;

        NSLog(@"Selected url: %@",catURL);

    }


}

1 个答案:

答案 0 :(得分:0)

当你将segue调回第一个ViewController时,第一个viewController已经加载了。因此不会调用方法viewDidLoad。解决此问题的最快捷方法是将viewDidLoad中的所有代码移至viewWillAppear