2013-02-25 76 views
1

我跟着教程创建2个镜场景Tutorial,我知道如何从场景1的信息传递到现场2使用代码波纹管:如何使用segues在故事板场景之间传递信息?

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    Scene2ViewController *destination = 
        [segue destinationViewController]; 

    destination.labelText = @"Arrived from Scene 1"; 
} 

不过我是如何从现场2返回信息还不清楚(发布尔值)作为教程使用平仓,以避免产生SCENE1的新实例,我不能SCENE1使用方法,下面从SCENE2分配什么:

-(IBAction)returned:(UIStoryboardSegue *)segue { 

Scene2ViewController *destination = 
         [segue destinationViewController]; 
     if (destination.thisIsBooleanFromScene2){ 
     //do something 

    } 
    } 
+0

指定场景1的viewController作为场景2的代表这是处理这种典型的方式。 – 2013-02-25 14:16:59

回答

0

Scene2ViewController头,你可以把一个@property当前视图控制器所以哟u必须查看在其公共变量

所以Scene2ViewController.h

#import "YourPreviousViewController.h" 

//add this to your @interface 
@property (strong) YourPreviousViewController *parent; 

然后在当前viewcontroller.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
Scene2ViewController *destination = [segue destinationViewController]; 
destination.parent = self; 
} 

那么你可以从Scene2ViewController运行returned方法或者你可以操纵您当前视图控制器上的公共变量,即parent

编辑:我有n见过像现在这样,一个返回的方法,你可以只让,将设置你在当前视图控制器需要的变量,那么在Scene2ViewController的方法可以实现- (void) viewWillDisappear { }当您按下背部或Segue公司另一种观点认为,这将运行

//Scene2ViewController.m 
- (void) viewWillDisappear { 
[self.parent returnAndSetBool:someBool]; 

//or if myBoolean in the previous view controller is a public variable you can just go: 
//self.parent.myBoolen = someBool; 
} 

//CurrentViewController.m 
- (void) returnAndSetBool: (bool) passbackVariable { 
self.myBoolean = passbackVariable; 
} 
+0

嗨,谢谢你的回答我确实收到了错误信息: destination.parent = self; 从Scene1ViewController *指定为'UISplitViewController'的不兼容指针类型const_strong – 2013-02-25 16:53:29

+0

不介意如何访问场景1上的属性,我将其错误地声明为私有属性。 – 2013-02-25 17:11:10