3

我正在使用情节提要,并且我有一个加载动态按钮的视图。单击此按钮时,我需要加载第二个视图控制器并传递数据。我不能使用 segue 作为它的动态按钮。

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

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


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

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

4

7 回答 7

1

尝试这样的事情:

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
于 2013-03-07T11:38:54.913 回答
1

您可以通过 ctrl+拖动从第一个控制器到第二个控制器创建一个不绑定到按钮的 Segue(不要忘记提供此 segue 和标识符)。

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

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

于 2013-03-07T12:13:06.233 回答
1

这就是在 Swift 2.0 中的做法。而不是使用prepareForSegue,

斯威夫特 3.0

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

斯威夫特 2.0

let someViewConroller = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewController") as! SomeViewController
someViewConroller.someObject = self.someObject!
self.navigationController?.pushViewController(someViewConroller, animated: true)
于 2015-11-29T13:21:20.260 回答
1

斯威夫特 3

let destinationView = self.storyboard?.instantiateViewController(withIdentifier: "ID") as! ViewController2
destinationView.Value2 = Value1
self.present(destinationView, animated: true, completion: nil)
于 2017-03-28T11:08:57.493 回答
1

你可以简单地使用

NSUserDefaults

存储数据并在您需要的任何页面中检索

在视图控制器 A

[[NSUserDefaults standardUserDefaults] setObject:aData forKey:@"DATA"];
[[NSUserDefaults standardUserDefaults] synchronize];

在目标控制器中

NSData * aData = [[NSUserDefaults standardUserDefaults] valueForKey:@"DATA"];
于 2017-07-03T09:57:10.950 回答
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

于 2013-03-07T11:42:13.623 回答
0

你可以定义一个协议:PassValueDelegate,在第一个viewController中你可以实现这个协议。

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];
于 2014-03-05T04:02:02.297 回答