我试图做一件在许多地方都有记录的事情,但似乎无法控制。我正在尝试在 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]];
}
但是,由于这发生在segue之后,所以它有点无用。我如何在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);
}
}