Возврат данных NSDictionary из всплывающей таблицы - PullRequest
0 голосов
/ 15 марта 2011

У меня есть всплывающее табличное представление (ViewController2), появляющееся, когда я нажимаю кнопку на ViewController1. Затем, когда я выбираю строку в таблице, я хочу, чтобы эти значения были отправлены обратно ViewController1. Я настроил NSDictionary. Он отлично работает в обычном контроллере навигации, но пытается сделать это с помощью dismissModalViewControllerAnimated, поэтому представление таблицы возвращается вниз, и данные отображаются в первом представлении.

Это похоже на этот вопрос здесь, я думаю: http://www.iphonedevsdk.com/forum/iphone-sdk-development/39995-return-data-dismissmodalviewcontrolleranimated.html

Вот мой код:

ViewController1.h:

@protocol ViewController1Delegate;
@interface ViewController1 : UIViewController <ViewController2Delegate> {
    id <ViewController1Delegate> delegate;
}
@property (nonatomic, assign) id <ViewController1Delegate> delegate;
@end
@protocol ViewController1Delegate
- (void)viewController1DidFinish:(ViewController1 *)controller;
@end

ViewController1.m:

-(void)buttonGoToViewController2 {
    ViewController2 *controller = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

//  this controller.delegate = self causes it to crash if i have it uncommented for some reason...
//  controller.delegate = self;

    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

ViewController2.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(searching) {
        // Navigation logic may go here. Create and push another view controller.
        NSDictionary *selectedCountry = [self.copyListOfItems objectAtIndex:indexPath.row];
        ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil andDictionary: selectedCountry];
        NSLog(@"selected hit this %@",selectedCountry);
        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:dvController animated:YES];
        [dvController release];
        [self dismissModalViewControllerAnimated:YES];

    }
    else {
        // Navigation logic may go here. Create and push another view controller.
        NSDictionary *dictionary = [self.listOfItems objectAtIndex:indexPath.row];
        ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil andDictionary: dictionary];
        NSLog(@"normal hit this %@",dictionary);
        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:dvController animated:YES];
        [dvController release];     
    }
}

1 Ответ

0 голосов
/ 15 марта 2011

В вашем коде ViewController2 вы создаете новый объект ViewController1 и присваиваете ему свой NSDictionary.Когда вы закрываете ViewController2, вы возвращаетесь к оригинальному ViewController1, который никуда не делся и никогда не отправлял NSDictionary.Вам нужно предоставить ViewController2 доступ к объекту ViewController1, который его представил (я предлагаю сделать это с помощью шаблона делегата), чтобы установить там NSDictionary.

...