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

问题描述:

我正在使用此函数在Xcode 4.3中的视图之间切换。在视图控制器之间传递变量

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

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

+0

没有ü尝试使用导航控制器代替。它是非常简单的通过值 – WaaleedKhan 2012-07-20 10:30:01

+0

不,我没有尝试导航控制器。我在故事板中使用常规按钮并继续播放。 – bla0009 2012-07-20 11:12:09

performSegueWithIdentifier:sender:后您的观点。控制器会调用

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

假设你的新视图控制器具有一些propertys to set:

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

因此,我设置了像你说的属性,以及如何从该属性读取值。而且,如果我需要将多个变量传递给下一个视图呢? – bla0009 2012-07-20 10:51:17

+0

对于多个变量使用多个属性,并从属性中读取使用... = self.property(在您的视图控制器中,您定义该属性) – Pfitz 2012-07-20 11:04:54

+0

我明白,但我不知道如何获取“新思维”。 – bla0009 2012-07-20 11:11:13

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

Passing Data between View Controllers

+1

是的,这是很好的解释,但他们正在使用pushViewController视图更改,但我想使用performSegueWithIdentifier。 – bla0009 2012-07-20 10:47:21

这里是Pfitz答案,但我做了一点比较容易理解为乡亲使用例如谁是新的Objective-C:

  1. 在你destinationViewController.h文件中添加属性设置变量

    // VOLViewController.m 
    #import "VOLVoteViewController.h 
    
  2. :从您的源文件Controller.m或者

    @property (nonatomic) int billIdReceivingVote; 
    
  3. 导入你的destinationViewController.h您sourceViewController.m文件 '接收' 值

  4. 添加prepareForSegue方法您sourceViewController.m文件和传递变量

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