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

Iphone 在视图控制器ios 7之间传递数据,iphone,ios,objective-c,uiviewcontroller,ios7,Iphone,Ios,Objective C,Uiviewcontroller,Ios7,我正在开发一个应用程序,需要在视图控制器之间传递数据。我知道这是一个常见的问题,但我找不到问题的答案:我能够将数据从FirstViewController(MasterViewController)传递到SecondViewController(SettingsViewController),但不能反过来。发生的情况是,我从SecondViewController.m文件中的FirstViewController调用了一个方法。这是可行的,它会记录数据。但是当我退出SecondViewContr

我正在开发一个应用程序,需要在视图控制器之间传递数据。我知道这是一个常见的问题,但我找不到问题的答案:我能够将数据从FirstViewController(
MasterViewController
)传递到
SecondViewController
SettingsViewController
),但不能反过来。发生的情况是,我从SecondViewController.m文件中的FirstViewController调用了一个方法。这是可行的,它会记录数据。但是当我退出SecondViewController(使用
[[self-presentingViewController]dismissViewControllerAnimated:YES completion:nil];
)时,数据被重置。 我尝试使用其他方法传递数据,但没有成功。我正在使用此代码传递数据:

MasterViewController *vc = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];

[vc setPorts:SelectedPorts];
我还尝试替换
[vc设置端口:SelectedPorts]
vc.selectedCellIndexes=SelectedPorts但同样的问题也会发生

setPorts
方法在FirstViewController.h文件中声明,而
SelectedPorts
是我在SecondViewController.m中声明的变量(我没有选中它)

以下是
设置端口:
FirstViewController.m中:

- (void) setPorts:(id)selectedPorts {
selectedCellIndexes = selectedPorts;
NSLog(@"selectedCellIndexes : %@", selectedCellIndexes);
}
这会记录好的值,但当我在
视图中登录时,它会出现在FirstViewController.m中,它会重置为在我从SecondViewController.m调用该方法之前的值

只是澄清一下,如果我不退出SecondViewController.m,数据不会重置

我确实读了你所有的评论,我真的非常感谢你的帮助。为了方便起见,我使用了一个全局变量


感谢您的帮助。

在secondViewController中,您可以创建协议

  #import <UIKit/UIKit.h>

@protocol sampleDelegate <NSObject>

- (void)passValue:(id)selectedPorts

@end

@interface SecondViewController : UIViewController

@property (nonatomic, strong) id <sampleDelegate> passDelegate;

@end
在FirstViewController.h中

导入代理

#import "SecondViewController.h"

@interface FirstViewController : UIViewController<SampleDelegate>

@end
并在第二个ViewController分配中设置self

SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
vc.passDelegate = self;

得到的结果没有什么不寻常的。通过做

MasterViewController *vc = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];   
[vc setPorts:SelectedPorts];
您正在从您的
SecondViewController
创建
MasterViewController
的新实例。这与导航到
SecondViewController
的位置不同。所以你不会得到预期的结果。因为您正在将端口(
[vc setPorts:SelectedPorts]
)设置为新创建的主机实例


不要创建新实例,只需在属性的
SecondViewController
中保留
MasterViewController
的引用,并在移动到第二个VC之前分配它。作为初学者,我建议这样做。但是使用委托是返回数据的首选方式。

可以使用委托方法从模式VC与主VC通信,或者如果您想从模式VC检索一些操纵对象,可以执行类似操作

将对象设置为模态视图控制器的.h文件中的属性(因此它们是公共的)

在主VC中,使用展开段,只需执行以下操作:

-(IBAction)exitModalVC:(UIStoryboardSegue*)segue
{
    SomeObject *obj = ((YourModalVC*)segue.sourceViewController).someObject;

    //Do what you want with obj
}
编辑:

这只有在您使用unwind segue时才有效(这是在使用story board时摒弃模态VC的一种简洁方式)

您使用的是这个,它不是展开分段:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
  • MasterViewController
    中有一个端口列表,您希望在
    设置ViewController
    中使用它
  • MasterViewController
    可以保存此列表,并且
    SettingsViewController
    应该可以访问此列表
SettingsViewController
中,使用
setSelectedPort
方法: 该方法将“选定端口”列表保存到属性中

masterview控制器
中,调用
设置视图控制器
并为其提供列表。
设置查看控制器中修改列表时,即使您离开
设置视图控制器
,您正在从第二视图控制器创建第一视图控制器的新实例,而没有访问原始调用方的相同实例,但
主视图控制器
中包含的端口列表也不会移动。这就是为什么当您返回到原始调用者(您的MasterViewController)时,虽然可以看到日志,但数据不在那里的原因


您需要使用委托方法。检查我的答案,这是与对象所有权相关的问题。 请按照以下步骤操作:

根据理解,您希望将值从“SecondViewController”反转为“FirstViewController”

不要在SecondViewController中创建FirstViewController的新对象,它将不起作用

  • 在“SecondViewController.h”文件中创建“FirstViewController”的对象。 @属性(非原子,强)FirstViewController*FirstViewController

  • 当您从FirstViewController导航到SecondViewController时,请传递“self”。 e、 g.SecondViewController*vc=[[SecondViewController alloc]initWithNibName:@“SecondViewController”捆绑包:nil]; vc.firstViewController=self

  • 如果要将反向值传递给FirstViewController,则在SecondViewController.m文件中传递。 [self.firstViewController设置端口:SelectedPorts]

  • 在FirstViewController.m中,使用最新值刷新控件


  • 尝试以上代码将挑战性地按照您的要求工作

    数据被重置为secondViewController,其IVAR被解除分配。您需要在主视图控制器上创建一个ivar,该ivar将始终保存您正在谈论的数据。请您就我应该如何执行该操作向我提供一些建议?@jrock007如何向我们展示您如何尝试声明和实现委托协议?有什么问题@jrock007?7人花时间写答案。可以随时更新我们吗?在FirstViewController.m中分配吗?我需要将数据从SecondViewController传递到SecondViewController,而不是SecondViewController。我忘了提到我正在使用故事板。您需要反转吗?第二视图到主视图?是的,我需要将数据传回。。。从SecondViewController
    -(IBAction)exitModalVC:(UIStoryboardSegue*)segue
    {
        SomeObject *obj = ((YourModalVC*)segue.sourceViewController).someObject;
    
        //Do what you want with obj
    }
    
    [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    
    @property (nonatomic, retain) id selectedPorts
    
    - (void) setPorts:(id)selectedPorts;
    
    SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    [vc setSelectedPorts:yourValue];