ios在segue期间将值传递给另一个视图 [英] ios pass values during a segue to another view

查看:87
本文介绍了ios在segue期间将值传递给另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试做一些我见过很多地方记录的事情,但似乎无法管理。我试图在segue期间将数据从一个视图传递到另一个视图。



这是源视图中的prepareForSegue方法:

   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{

//传递值
DetailViewController * dest =(DetailViewController *)[segue destinationViewController];

dest.locTitle = [value valueForKeyPath:@title];
dest.locInfo = [value valueForKeyPath:@info];



// NSLog(@标题是:%@,信息是:%@。,dest.locTitle,dest.locInfo);

}

如果我启用最后一个 NSLog ,我看到了正确的值...



这是目的地视图上的界面:

  #import< UIKit / UIKit.h> 

@interface DetailViewController:UIViewController {

}

@property(强,非原子)NSString * locTitle;
@property(强,非原子)NSString * locInfo;
@property(弱,非原子)IBOutlet UILabel * detailMainText;
@property(弱,非原子)IBOutlet UILabel * detailTitle;

@end

...这里是viewDidLoad和相关的@synthesize在目标视图上:

  @synthesize detailMainText,detailTitle,locTitle,locInfo; 

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@在viewDidLoad中 - 标题是:%@,信息是:%@,self.locTitle,self.locInfo);
detailTitle.text = locTitle;
detailMainText.text = locInfo;
}

NSLog 报告 null 每个变量的值。



我很感激你能给我的任何帮助。我确定我做的事情很愚蠢,很明显。



预先感谢您的帮助。






很久以后...






好的......我想我正在缩小范围。



我发现(违反直觉)

   - (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

被称为之后

   - (void)prepareForSegue:(UIStoryboardSegue *)segue发件人:(id)发件人

这令人困惑,并且还给我的小计划弄清了我的 NSDictionary datasource应该显示在我的详细信息视图中。目前我这样做:

  //根据选择的行发送信息到详细信息视图
- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//获取所选对象以填写详细视图
myValue = [myLocations objectForKey:[locationsIndex] objectAtIndex:indexPath.row]];

}

但是因为这发生在之后塞古,这有点无用。如何在prepareForSegue方法中访问行号?



再次感谢。






...最后......






以下是问题的答案:

如何在prepareForSegue方法中访问选定的行号?



希望有人会发现它很有用。



首先,在listView控制器的界面中为tableview设置 IBOutlet

  @property(弱,非原子)IBOutlet UITableView * tableView; 

然后你的prepareForSegue可以这样做:

   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
if([[segue identifier] isEqualToString:@showDetail] ){
//传递值
NSLog(@发件人是%@,发件人);

NSIndexPath * indexPath = [self.tableView indexPathForCell:sender];
//获取所选对象以填写详细信息视图
id myValue = [myLocations objectForKey:[locationsIndex objectAtIndex:indexPath.row]];

DetailViewController * dest = [segue destinationViewController];
dest.locTitle = [myValue valueForKeyPath:@title];
dest.locInfo = [myValue valueForKeyPath:@info];

// NSLog(@标题是:%@,信息是:%@。,dest.locTitle,dest.locInfo);
}
}


解决方案

你可以通过以下方式实现:首先将要发送的值传递到目标控制器:

   - (void)tableView :(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@segueToLocationsender:您要传递的对象];
}

然后在此处获取对象

   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
if([[segue identifier] isEqualToString:@ segueToLocation]){
DetailViewController * dest =(DetailViewController *)[segue destinationViewController];
//发件人是您传递给上一个方法的内容
dest.target = sender
}
}


Trying to do something I've seen documented in a lot of places, but can't seem to manage. I'm trying to pass data from one view to another during a segue.

Here's the prepareForSegue method in the source view:

- (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);

}

If I enable that last NSLog, I see the correct values...

Here's the interface on the destination view:

#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

... and here's the viewDidLoad and relevant @synthesize on the destination view:

@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;
}

That NSLog reports null values for each of those variables.

I'd appreciate any help you could give me. I'm sure I'm doing something stupid, and obvious.

Thanks in advance for your help.


Much later...


OK... I think I'm narrowing it down.

I've discovered that (counterintuitively)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

gets called after

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

Which is confusing, and also hosed my little scheme to figure out what chunk of my NSDictionary datasource should be displayed in my detail view. Currently I'm doing this:

// 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]];

}

But since this happens after the segue, it's kinda useless. How can I access the row number in the prepareForSegue method?

Thanks again.


... and finally...


Here's the answer to the question:

How do I access a selected row number while in the prepareForSegue method?

Hopefully someone will find it useful.

First, set up a IBOutlet for the tableview in the listView controller's interface:

@property (weak, nonatomic) IBOutlet UITableView *tableView;

Then your prepareForSegue can do this:

- (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);
}
}

解决方案

you can achieve it by doing this: first pass the value you want to send to the destination controller here:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [self performSegueWithIdentifier:@"segueToLocation" sender:the object you want to pass];
}

then get the object here

- (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
    }
}

这篇关于ios在segue期间将值传递给另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆