问题描述
我有一个 FirstViewController
和一个 SecondViewController
。我在 FirstViewController
中创建了一个按钮,以便以 SecondViewController
方式执行segue。
I have a FirstViewController
and a SecondViewController
. I created a button in FirstViewController
in order to perform a segue modally to SecondViewController
.
在 SecondViewController
中,我有一个tableView显示8个项目的列表,因为我从该列表中选择一个项目,我 dismissViewControllerAnimated:
,回到 FirstViewController
。
In SecondViewController
I have a tableView showing a list of 8 items, as I select an item from that list, I dismissViewControllerAnimated:
, going back to FirstViewController
.
我想做的是将一个字符串传回 FirstViewController
。
What I would like to do is pass a string back to the FirstViewController
.
我用这个帖子作为参考对于我的代码:
I used this post as a reference for my code: dismissModalViewController AND pass data back
所以这是我所拥有的:
在FirstViewController.h
in FirstViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SecondViewController.h"
@interface FirstViewController : UIViewController <SecondDelegate>
@end
在FirstViewController.m中
in 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
in 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
in SecondViewController.m
in 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
就好了。问题是字符串这里有一些没有被传回。
When I run the app I can go from FirstViewController
to SecondViewController
, and when I select a row from the tableView I go back to FirstViewController
just fine. The problem is that the string "SOME STRING HERE" was not passed back.
我缺少什么?
顺便说一下,我不知道这是否相关:我正在使用ARC和Storyboards。
By the way, I'm not sure if this is relevant: I am using ARC and Storyboards.
推荐答案
你必须当您出现第二个视图控制器时,设置代理,即在您的FirstViewController中:
You have to set the delegate when you present the second view controller, ie, in your FirstViewController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"present_secondviewcontroller]) {
SecondViewController *svc = (SecondViewController *)segue.destinationViewController;
svc.delegate = self;
}
}
这篇关于使用dismissViewControllerAnimated传回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!