jam*_*rns 27 ios uistoryboard segue
试着做一些我见过很多地方记录的事情,但似乎无法管理.我想在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);
}
Run Code Online (Sandbox Code Playgroud)
如果我最后启用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
Run Code Online (Sandbox Code Playgroud)
...这里是目标视图上的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;
}
Run Code Online (Sandbox Code Playgroud)
这NSLog报告null为每个变量的值.
我很感激你能给我的任何帮助.我确定我做的事情很愚蠢,很明显.
在此先感谢您的帮助.
好的......我想我正在缩小范围.
我发现了(违反直觉)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Run Code Online (Sandbox Code Playgroud)
被叫之后
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Run Code Online (Sandbox Code Playgroud)
这让人感到困惑,并且还在我的小方案中找出了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]];
}
Run Code Online (Sandbox Code Playgroud)
但是因为这发生在 segue之后,它有点无用.如何访问prepareForSegue方法中的行号?
再次感谢.
这是问题的答案:
如何在prepareForSegue方法中访问选定的行号?
希望有人会发现它很有用.
首先,IBOutlet在listView控制器的界面中为tableview 设置一个:
@property (weak, nonatomic) IBOutlet UITableView *tableView;
Run Code Online (Sandbox Code Playgroud)
然后你的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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 11
您可以通过以下方式实现:首先将要发送的值传递到目标控制器:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"segueToLocation" sender:the object you want to pass];
}
Run Code Online (Sandbox Code Playgroud)
然后在这里得到对象
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: @"segueToLocation"]) {
DetailViewController *dest = (DetailViewController *)[segue destinationViewController];
//the sender is what you pass into the previous method
dest.target=sender
}
}
Run Code Online (Sandbox Code Playgroud)
小智 4
您不需要创建目标视图控制器的本地实例变量。这将执行相同的操作:
[segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"];
[segue.destinationViewController setLocInfo: [myValue valueForKeyPath:@"info"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30312 次 |
| 最近记录: |