【发布时间】:2012-04-12 01:19:56
【问题描述】:
尝试做一些我在很多地方都看到过的事情,但似乎无法做到。我正在尝试在 segue 期间将数据从一个视图传递到另一个视图。
这是源视图中的 prepareForSegue 方法:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//pass values
DetailViewController *dest = (DetailViewController *)[segue destinationViewController];
dest.locTitle = [value valueForKeyPath:@"title"];
dest.locInfo = [value valueForKeyPath:@"info"];
// NSLog(@"The title is: %@, and the info is: %@.",dest.locTitle,dest.locInfo);
}
如果我启用最后一个NSLog,我会看到正确的值...
这是目标视图的界面:
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController{
}
@property (strong, nonatomic) NSString *locTitle;
@property (strong, nonatomic) NSString *locInfo;
@property (weak, nonatomic) IBOutlet UILabel *detailMainText;
@property (weak, nonatomic) IBOutlet UILabel *detailTitle;
@end
...这是目标视图上的 viewDidLoad 和相关的@synthesize:
@synthesize detailMainText,detailTitle,locTitle,locInfo;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"In viewDidLoad - the title is: %@ and the info is: %@",self.locTitle,self.locInfo);
detailTitle.text = locTitle;
detailMainText.text = locInfo;
}
NSLog 报告每个变量的 null 值。
如果您能给我任何帮助,我将不胜感激。我确定我在做一些愚蠢而明显的事情。
提前感谢您的帮助。
很久以后...
好的...我想我正在缩小范围。
我发现(违反直觉)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
在之后
被调用- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
这很令人困惑,也让我的小计划无法确定我的 NSDictionary 数据源的哪一部分应该显示在我的详细视图中。目前我正在这样做:
// send info to detail view according to which row selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the selected object in order to fill out the detail view
myValue = [myLocations objectForKey:[locationsIndex objectAtIndex:indexPath.row]];
}
但由于这发生在 转场之后,它有点没用。如何在 prepareForSegue 方法中访问行号?
再次感谢。
...最后...
这是问题的答案:
如何在 prepareForSegue 方法中访问选定的行号?
希望有人会觉得它有用。
首先,在listView控制器的界面中为tableview设置一个IBOutlet:
@property (weak, nonatomic) IBOutlet UITableView *tableView;
那么你的 prepareForSegue 可以这样做:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: @"showDetail"]) {
//pass values
NSLog(@"The sender is %@",sender);
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
//Get the selected object in order to fill out the detail view
id myValue = [myLocations objectForKey:[locationsIndex objectAtIndex:indexPath.row]];
DetailViewController *dest = [segue destinationViewController];
dest.locTitle = [myValue valueForKeyPath:@"title"];
dest.locInfo = [myValue valueForKeyPath:@"info"];
//NSLog(@"The title is: %@, and the info is: %@.",dest.locTitle,dest.locInfo);
}
}
【问题讨论】:
-
您在生命周期中是否有比 viewDidLoad 更早的东西 - loadView、initWithCoder 等?你的为 segue 准备的日志是否在 viewDidLoad 之前出现(例如,不是你的视图已经加载?)你在视图中看到的内容会出现在字符串属性中吗?
-
没有。我想我现在可能更接近了,但我正遭受“嵌套推送动画会导致导航栏损坏”的问题。
-
我在 xCode 4.2 (Snow Leopard) 中使用故事板。我有一个导航控制器,将 wo=ith 与我的列表控制器的 RootView 关系连接起来。我的列表控制器正在使用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 来调用 [self performSegueWithIdentifier:@"showDetail" sender:self];故事板有一个从原型表单元到详细视图的推送序列,带有一个名为“显示详细信息”的标识符 - 该链中是否有一些不稳定的东西?
-
是的,也许!如果您有来自单元格的 segue,则无需从 didSelectRow 中“手动”调用它。您可能会调用两个 segues,并且会感到困惑。从 didSelectRow 中删除您的代码 - segue 仍在执行?
-
如果转场从一个视图控制器对象转到下一个,您将手动执行转场。如果它来自 UI 元素,那么它会在您点击该元素时自动执行。
标签: ios segue uistoryboard