使用dismissViewControllerAnimated传回数据

时间:2013-03-08 19:55:56

标签: ios objective-c delegates uistoryboard

我有FirstViewControllerSecondViewController。我在FirstViewController中创建了一个按钮,以便以SecondViewController模式执行segue。

SecondViewController我有一个tableView,显示了8个项目的列表,因为我从该列表中选择了一个项目dismissViewControllerAnimated:,然后返回FirstViewController

我想要做的是将字符串传递回FirstViewController

我使用这篇文章作为我的代码的参考:dismissModalViewController AND pass data back

所以这就是我所拥有的:

在FirstViewController.h中

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

@interface FirstViewController : UIViewController <SecondDelegate>

@end
在FirstViewController.m中

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

...

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

@end
在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
在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

当我运行应用程序时,我可以从FirstViewController转到SecondViewController,当我从tableView中选择一行时,我会回到FirstViewController就好了。问题是字符串“SOME STRING HERE”没有被传回。

我错过了什么?

顺便说一下,我不确定这是否相关:我正在使用ARC和故事板。

2 个答案:

答案 0 :(得分:4)

您必须在呈现第二个视图控制器时设置委托,即在FirstViewController中:

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

答案 1 :(得分:0)

根据您的问题和上述解决方案,您应该实际添加svc.myDelegate = self; 而不是svc.delegate = self;