2013-03-08 23 views
5

Tôi có một số FirstViewControllerSecondViewController. Tôi đã tạo một nút trong số FirstViewController để thực hiện phân đoạn theo phương thức SecondViewController.Truyền dữ liệu trở lại với bỏ quaViewControllerAnimated

Trong SecondViewController Tôi có tableView hiển thị danh sách 8 mục, khi tôi chọn một mục từ danh sách đó, I dismissViewControllerAnimated:, quay lại FirstViewController.

Điều tôi muốn làm là chuyển một chuỗi trở lại số FirstViewController.

tôi đã sử dụng bài viết này như một tham khảo cho mã của tôi: dismissModalViewController AND pass data back

Vì vậy, đây là những gì tôi có:

trong FirstViewController.h

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

@interface FirstViewController : UIViewController <SecondDelegate> 

@end 

trong FirstViewController.m

#import "FirstViewController.h" 

@interface FirstViewController() 

@end 

@implementation FirstViewController 

... 

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

@end 

trong 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 

trong 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 

Khi tôi chạy ứng dụng Tôi có thể đi FirstViewController-SecondViewController, và khi tôi chọn một hàng từ tableView tôi quay trở lại FirstViewController tốt. Vấn đề là chuỗi "MỘT SỐ STRING TẠI ĐÂY" đã không được trả lại.

Tôi đang thiếu gì?

Nhân tiện, tôi không chắc liệu điều này có liên quan hay không: Tôi đang sử dụng ARC và Bảng phân cảnh.

+0

Bạn có đang đặt đại biểu không? Có phải nó đang đi đến dòng '[self.myDelegate secondViewControllerDismissed ...'? – iDev

+0

Câu hỏi được định dạng rất độc đáo :) – Sid

Trả lời

4

Bạn cần phải thiết lập các đại biểu khi bạn giới thiệu bộ điều khiển xem thứ hai, tức là, trong FirstViewController của bạn:

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

Theo vấn đề của bạn và các giải pháp nêu trên, bạn thực sự nên thêm svc.myDelegate = self; thay vì svc.delegate = self;

Các vấn đề liên quan