ios - 使用 dismissViewControllerAnimated 传回数据

标签 ios objective-c delegates uistoryboard

我有一个 FirstViewController 和一个 SecondViewController。我在 FirstViewController 中创建了一个按钮,以便模态地执行到 SecondViewController 的 segue。

SecondViewController 中,我有一个显示 8 项列表的 tableView,当我从该列表中选择一个项目时,我 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 和 Storyboards。

最佳答案

您必须在呈现第二个 View Controller 时设置委托(delegate),即在您的 FirstViewController 中:

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

关于ios - 使用 dismissViewControllerAnimated 传回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15302219/

相关文章:

ios - 链接器警告 : 'WJLibrary has an install name beginning with "/", but it is not from the specified SDK'

iphone - 在 UIScrollView 中覆盖 drawRect 时的黑色背景

objective-c - 从 Xcode 项目中删除的文件,但仍可在游戏中使用

objective-c - 如何在 Objective-C 中自毁对象?

ios - 屏幕锁定时未收到iOS推送通知

javascript - 在滚动 React Native Animated 上做动画但闪烁

ios - 如何使我的类(class)符合外部协议(protocol)?它找不到它,但我的印象是我不应该在 .h 文件中导入

ios - Adcolony 委托(delegate)没有被调用

ios - 需要有关 AutoLayout 的帮助

ios - Cordova 插件开发工作流程