ios ios在segue期间将值传递到另一个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9907684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:34:58  来源:igfitidea点击:

ios pass values during a segue to another view

iossegueuistoryboard

提问by james Burns

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.

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

Here's the prepareForSegue method in the source view:

这是源视图中的 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);

}

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

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

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:

...这是目标视图上的 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;
}

That NSLogreports nullvalues for each of those variables.

这会NSLog报告null每个变量的值。

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 NSDictionarydatasource should be displayed in my detail view. Currently I'm doing this:

这很令人困惑,也使我的小计划弄清楚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]];

}

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

但是由于这是segue之后发生的,所以它有点没用。如何访问 prepareForSegue 方法中的行号?

Thanks again.

再次感谢。



... and finally...

……最后……



Here's the answer to the question:

以下是问题的答案:

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

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

Hopefully someone will find it useful.

希望有人会发现它有用。

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

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

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

Then your prepareForSegue can do this:

然后你的 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);
}
}

回答by ltebean

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

回答by Unsure1

You don't need to create a local instance variable of the destination view controller. This will do the same:

您不需要创建目标视图控制器的本地实例变量。这将执行相同的操作:

[segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"];
[segue.destinationViewController setLocInfo: [myValue valueForKeyPath:@"info"];

回答by resedasue

Another option - could use the following method:

另一种选择 - 可以使用以下方法:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

which fires before prepareForSegue.

在 prepareForSegue 之前触发。

回答by Sunil_Vaishnav

You can try this:

你可以试试这个:

[[NSString alloc] initWithFormat:@"%@",
  [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"]];
[[NSString alloc] initWithFormat:@"%@",
  [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"info"]];

回答by Jonauz

I think I had absolutely the same problem, so hope my solution will help you too. It should answer first part of your question, or at least will help some one else.

我想我遇到了完全相同的问题,所以希望我的解决方案也能帮助你。它应该回答您问题的第一部分,或者至少会帮助其他人。

First of all you can't assign passed values to properties with viewDidLoadmethod in the SecondViewController. Doing so, gives nil, as viewDidLoad is implemented before Segue.

首先,您不能使用SecondViewController 中的viewDidLoad方法将传递的值分配给属性。这样做会得到 nil,因为 viewDidLoad 是在 Segue 之前实现的。

And I wouldn't recommend to use viewDidAppear, as as the values will be changed after the view is loaded, witch makes the change process be visually visible.

而且我不建议使用viewDidAppear,因为在加载视图后值会发生变化,巫婆使更改过程在视觉上可见。

So, the best option is to assign the values directly to the IBOutlet's. If you want to pass an array of strings, you can assign one array value witch you want to load first, and also pass all NSArray. This will allow your view to be loaded with the value already, and also you will have other values to work with.

因此,最好的选择是将值直接分配给IBOutlet's. 如果你想传递一个字符串数组,你可以分配一个你想首先加载的数组值,然后传递所有NSArray. 这将允许您的视图已经加载了该值,并且您还将有其他值可以使用。

You could also call method and pass data.

您还可以调用方法并传递数据。