试着做一些我见过很多地方记录的事情,但似乎无法管理 . 我想在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 {
被称为 after
- (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方法中的行号?
再次感谢 .
......最后......
这是问题的答案:
How do I access a selected row number while in the prepareForSegue method?
希望有人会发现它很有用 .
首先,在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);
}
}
5 回答
您可以通过以下方式实现:首先将要发送的值传递到目标控制器:
然后在这里得到对象
您不需要创建目标视图控制器的本地实例变量 . 这将做同样的事情:
另一种选择 - 可以使用以下方法:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
在准备之前发射 .
你可以试试这个:
我想我有完全相同的问题,所以希望我的解决方案对你也有帮助 . 它应该回答你问题的第一部分,或者至少会帮助其他人 .
首先,您无法在SecondViewController中使用
viewDidLoad
方法将传递的值分配给属性 . 这样做,给出nil,因为viewDidLoad是在Segue之前实现的 .并且我不建议使用
viewDidAppear
,因为在加载视图后将更改值,这会使更改过程在视觉上可见 .因此,最好的选择是将值直接分配给
IBOutlet's
. 如果要传递一个字符串数组,可以指定一个要首先加载的数组值,并传递所有NSArray
. 这将允许您的视图已加载值,并且您还将使用其他值 .您还可以调用方法并传递数据 .