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



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

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

我也尝试用vc.selectedCellIndexes = SelectedPorts;替换[vc setPorts:SelectedPorts];,但出现了同样的问题。

setPorts 方法是在 FirstViewController.h 文件中声明的,SelectedPorts是我在 SecondViewController.m 中声明的变量(我检查的不是零)。

以下是FirstViewController.m中的setPorts:

- (void) setPorts:(id)selectedPorts {
selectedCellIndexes = selectedPorts;
NSLog(@"selectedCellIndexes : %@", selectedCellIndexes);
}

这将记录良好的值,但是当我在FirstViewController.m中viewWillAppear登录它时,它会重置为我从SecondViewController.m调用该方法之前的值。

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

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

感谢您的帮助。

  • 您在MasterViewController中有一个端口列表,并且希望在SettingsViewController中使用它。
  • MasterViewController可以保存此列表,SettingsViewController应该有权访问它。

SettingsViewController中,有一个setSelectedPort方法:

@property (nonatomic, retain) id selectedPorts
- (void) setPorts:(id)selectedPorts;

该方法将选定的端口列表保存到属性中。

MasterViewController中,调用SettingsViewController并给出列表。

SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
[vc setSelectedPorts:yourValue]; 

SettingsViewController内修改列表时,即使您离开SettingsViewControllerMasterViewController中包含的端口列表也不会移动。

在 secondViewController 中,您创建协议

  #import <UIKit/UIKit.h>
@protocol sampleDelegate <NSObject>
- (void)passValue:(id)selectedPorts
@end
@interface SecondViewController : UIViewController
@property (nonatomic, strong) id <sampleDelegate> passDelegate;
@end

在viewDidLoad或任何方法中,根据需要调用这样的方法,

[self.passDelegate passValue:selectedPorts];

在FirstViewController.h中,

导入委托<sampleDelegate>

#import "SecondViewController.h"
@interface FirstViewController : UIViewController<SampleDelegate>
@end

在FirstViewController.m中,

-(void)passValue:(id)selectedPorts
{
    id receivedValues = selectedPorts;
}

并在您的 SecondViewController 分配中设置 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 文件中的属性(以便它们是公共的)。

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

-(IBAction)exitModalVC:(UIStoryboardSegue*)segue
{
    SomeObject *obj = ((YourModalVC*)segue.sourceViewController).someObject;
    //Do what you want with obj
}

编辑:

这仅在使用展开 segue 时才有效(这是在使用故事板时关闭模式 VC 的一种巧妙方式)

你正在使用这个,这不是放松 segues:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];

您正在从第二个视图控制器创建第一个视图控制器的新实例,而不访问原始调用方的同一实例。 这就是为什么虽然您可以看到日志,但当您返回原始调用方 - 您的 MasterViewController 时数据不存在的原因。

您需要使用委托方法。 检查我的答案。

这是与对象所有权相关的问题。请按照以下步骤操作:

根据理解,您希望将值从"SecondViewController"反向到"FirstViewController"

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

  1. 在"SecondViewController.h"文件中创建"FirstViewController"的对象。@property(非原子,强) 第一视图控制器 *第一视图控制器;

  2. 当您从FirstViewController导航到SecondViewController时,请传递"self"。例如 SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];vc.firstViewController = self;

  3. 如果要将反向值传递给FirstViewController,则在SecondViewController.m文件中。[self.firstViewController setPorts:SelectedPorts];

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

尝试上面的代码将根据您的要求灵活地工作。

最新更新