我有一个 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/