ios 在视图控制器之间传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11577484/
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 19:20:49  来源:igfitidea点击:

Passing variables between view controllers

iospropertiesuiviewcontrolleruistoryboarduistoryboardsegue

提问by Boban

I am using this function to switch between views in Xcode 4.3.

我正在使用此功能在 Xcode 4.3 中的视图之间切换。

[self performSegueWithIdentifier:@"NextView" sender:self];

I would like to pass some parameters between pages. How can I do that?

我想在页面之间传递一些参数。我怎样才能做到这一点?

回答by Pfitz

After performSegueWithIdentifier:sender:your view controler will call

performSegueWithIdentifier:sender:您的视图控制器将调用之后

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

assuming your new view controller has some propertys to set:

假设您的新视图控制器有一些要设置的属性:

if ([[segue identifier] isEqualToString:@"NextView"]) {
    MyViewController *myVC = [segue destinationViewController];
    myVC.propertyToSet = // set your properties here
}

回答by Kaan Dedeoglu

This question has a very nice and correct explanation of passing data between view controllers:

这个问题对在视图控制器之间传递数据有一个非常好的和正确的解释:

Passing Data between View Controllers

在视图控制器之间传递数据

回答by tmr

Here is the answer from Pfitz, but I made it a little easier to understand for folks who are new to Objective-C using an example:

这是 Pfitz 的答案,但我使用示例让不熟悉 Objective-C 的人更容易理解:

  1. In your destinationViewController.h file add property setting for variable to 'receive' value from your source controller.m file

    @property (nonatomic) int billIdReceivingVote;
    
  2. Import your destinationViewController.h to your sourceViewController.m file:

    // VOLViewController.m
    #import "VOLVoteViewController.h
    
  3. Add prepareForSeguemethod to your sourceViewController.m file and pass the variables

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        VOLVoteViewController *myDestinationViewController = [segue destinationViewController];
    
        // myDestinationViewController.variable = sourceViewController variabe
        myDestinationViewController.billIdReceivingVote = [self.idOfBillSelected intValue];
    }
    
  1. 在您的 destinationViewController.h 文件中,为变量添加属性设置以从源 controller.m 文件中“接收”值

    @property (nonatomic) int billIdReceivingVote;
    
  2. 将你的 destinationViewController.h 导入你的 sourceViewController.m 文件:

    // VOLViewController.m
    #import "VOLVoteViewController.h
    
  3. prepareForSegue方法添加到您的 sourceViewController.m 文件并传递变量

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        VOLVoteViewController *myDestinationViewController = [segue destinationViewController];
    
        // myDestinationViewController.variable = sourceViewController variabe
        myDestinationViewController.billIdReceivingVote = [self.idOfBillSelected intValue];
    }