2013-03-08 19 views
5

Mam FirstViewController i SecondViewController. Stworzyłem przycisk w FirstViewController, aby wykonać przejście modalnie do SecondViewController.Prześlij dane ponownie za pomocą metody dismissViewControllerAnimated

W SecondViewController Mam tableView pokazujący listę 8 pozycji, jak wybrać pozycję z tej listy, I dismissViewControllerAnimated:, wracając do FirstViewController.

Co chciałbym zrobić, to przekazać ciąg z powrotem do FirstViewController.

użyłem tego posta jako punkt odniesienia dla mojego kodu: dismissModalViewController AND pass data back

Więc to jest to, co mam:

w FirstViewController.h

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 
#import "SecondViewController.h" 

@interface FirstViewController : UIViewController <SecondDelegate> 

@end 

w FirstViewController.m

#import "FirstViewController.h" 

@interface FirstViewController() 

@end 

@implementation FirstViewController 

... 

- (void)secondViewControllerDismissed:(NSString *)stringForFirst 
{  
    NSString *theString = stringForFirst; 
    NSLog(@"String received at FirstVC: %@",theString); 
} 

@end 

w SecondViewController.h

#import <UIKit/UIKit.h> 

@protocol SecondDelegate <NSObject> 
-(void) secondViewControllerDismissed:(NSString *)stringForFirst; 
@end 

@interface SecondViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> 
{ 
    __weak id myDelegate; 
} 

@property (nonatomic, weak) id<SecondDelegate> myDelegate; 
@property (weak, nonatomic) IBOutlet UITableView *myTableView; 

@end 

w SecondViewController.m

#import "SecondViewController.h" 

@interface SecondViewController() 

@end 

@implementation SecondViewController 

@synthesize myDelegate; 
@synthesize myTableView; 

... 

- (int)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 8; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [atributoTableView dequeueReusableCellWithIdentifier:@"MainCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = //strings from an array here;  
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)]) 
    { 
     [self.myDelegate secondViewControllerDismissed:@"SOME STRING HERE"]; 
     NSLog(@"string passed"); 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
    NSLog(@"SecondViewController dismissed"); 
} 

@end 

Kiedy uruchomić aplikację mogę iść z FirstViewController do SecondViewController, a kiedy wybrać wiersz z Tableview wrócę do FirstViewController dobrze. Problem polega na tym, że napis "SOME STRING HERE" nie został zwrócony.

Czego mi brakuje?

Nawiasem mówiąc, nie jestem pewien, czy jest to istotne: używam ARC i Storyboard.

+0

Czy ustawiasz delegata? Czy przechodzi do linii '[self.myDelegate secondViewControllerDismissed ...'? – iDev

+0

Bardzo ładnie sformatowane pytanie :) – Sid

Odpowiedz

4

Musisz ustawić delegata podczas przedstawienia drugi kontroler widoku, czyli w FirstViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"present_secondviewcontroller]) { 
      SecondViewController *svc = (SecondViewController *)segue.destinationViewController; 
      svc.delegate = self; 
    } 
} 
0

Według problemu i wyżej wymienionego roztworu, należy faktycznie dodać svc.myDelegate = self; zamiast svc.delegate = self;

Powiązane problemy