在不使用segue的情况下在视图控制器之间传递数据

3

我将使用Storyboard,其中有一个视图会加载动态按钮。当单击此按钮时,我需要加载第二个视图控制器并传递数据。由于它是动态按钮,我无法使用segue。

我将使用以下代码来加载第二个视图控制器:

UIStoryboard*  sb1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                                           bundle:nil];
UIViewController* vc1 = [sb1 instantiateViewControllerWithIdentifier:@"SecondViewController"];


[self.navigationController pushViewController:vc1 animated:YES];

在第二个视图控制器中,我需要访问第一个视图控制器中的大约5个字段。我该如何传递数据?


为什么不在推出新的视图控制器之前设置所需的信息呢?你已经有了VC的实例。将属性设置为公共(在头文件中声明)或创建新属性以填充数据。 - Pochi
7个回答

1
您可以简单地使用

NSUserDefaults

来存储数据,并在需要的任何页面中检索。
在视图控制器A中:
[[NSUserDefaults standardUserDefaults] setObject:aData forKey:@"DATA"];
[[NSUserDefaults standardUserDefaults] synchronize];

在目标控制器中。
NSData * aData = [[NSUserDefaults standardUserDefaults] valueForKey:@"DATA"];

1

您可以通过从第一个控制器到第二个控制器进行ctrl+拖动来创建未绑定到按钮的Segue(不要忘记为此Segue指定标识符)。

接下来,在按钮的IBAction中(通过Interface Builder或通过addTarget:self action:forControlEvents:设置),您可以调用[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:button];

您可以像往常一样在- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender中将数据传递给第二个控制器。


1

Swift 3

let destinationView = self.storyboard?.instantiateViewController(withIdentifier: "ID") as! ViewController2
destinationView.Value2 = Value1
self.present(destinationView, animated: true, completion: nil)

1
尝试像这样做一些事情:

Class A

// CLASS_A.h
#import <UIKit/UIKit.h>
@interface CLASS_A : UIViewController {
    ...
}
- (IBAction)Btn_PushPressed:(id)sender;
...
@end

// CLASS_A.m
#import "CLASS_A.h"
#import "CLASS_B.h"
@implementation CLASS_A

- (IBAction)Btn_PushPressed:(id)sender
{
    UIStoryboard*  sb1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                                       bundle:nil];
    CLASS_B* vc1 = [sb1 instantiateViewControllerWithIdentifier:@"SecondViewController"];
    vc1.delegate = self;
    [self.navigationController pushViewController:vc1 animated:TRUE];
}

....
@end

类B

// CLASS_B.h
#import <UIKit/UIKit.h>
@class CLASS_A;
@interface CLASS_B : UIViewController {
    ...
}
@property (weak, nonatomic) CLASS_A *delegate;
....
@end

// CLASS_B.m
#import "CLASS_B.h"
#import "CLASS_A.h"
@implementation CLASS_B
....
@end

1
这是在Swift 2.0中的做法。不要使用prepareForSegue

Swift 3.0

let someViewConroller = self.storyboard?.instantiateViewController(withIdentifier: "SomeViewController") as! SomeViewController
someViewConroller.someObject = self.someObject!
self.navigationController?.pushViewController(someViewConroller, animated: true)

Swift 2.0
let someViewConroller = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewController") as! SomeViewController
someViewConroller.someObject = self.someObject!
self.navigationController?.pushViewController(someViewConroller, animated: true)

Swift 3怎么样? - mythicalcoder
将会加入Swift 3.0 @Maven ;) - Tal Zion
这是给你的,@Maven。 - Tal Zion
“someObject”显示为“someViewController”的成员。 我并没有完全使用你的代码。 这是我正在使用的代码:var someViewController = mainStoryboard.instantiateViewController(withIdentifier: "listView") as! ListVC。有什么问题吗? - mythicalcoder
@Maven,someObject只是一个示例,用于向您展示如何在视图控制器之间传递对象而不使用转场。您想要传递哪个对象?只需将其替换为someObject即可。 - Tal Zion

0
你可以定义一个协议:PassValueDelegate,并在第一个视图控制器中实现这个协议。
UIStoryboard*  sb1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
UIViewController* vc1 = [sb1 instantiateViewControllerWithIdentifier:@"SecondViewController"];
//set the delegate for the second view controller,so you can access the values in the first controller
vc1.delegate = self;
[self.navigationController pushViewController:vc1 animated:YES];

0

首先,即使您使用动态按钮,也可以使用segue。只需将目标方法添加到UIButtons并在该方法中使用segue推送视图控制器。在第二个视图控制器中,在推送第二个视图控制器之前,获取一些属性并将值分配给这些属性。例如:

UIStoryboard*  sb1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                               bundle:nil];
UIViewController* vc2 = [sb1 instantiateViewControllerWithIdentifier:@"SecondViewController"];

[vc2 setName:@"name"];
[self.navigationController pushViewController:vc2 animated:YES];

在此之前,您需要在SecondViewController中为NSString *name分配属性。


我已经添加了更多的注释,请检查。 - Zaraki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接