我正在使用故事板,我有一个加载动态按钮的视图。单击此按钮,我需要加载第二个视图控制器并传递数据。我不能使用segue作为其动态按钮。
我使用以下代码加载第二个视图控制器
UIStoryboard* sb1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
bundle:nil];
UIViewController* vc1 = [sb1 instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self.navigationController pushViewController:vc1 animated:YES];
在第二个视图控制器中,我需要从第一个视图控制器访问大约5个字段。我如何传递数据?
答案 0 :(得分: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
答案 1 :(得分:1)
你可以通过ctrl +从第一个控制器拖动到第二个控制器创建一个没有按钮的Segue(不要忘记给这个segue和标识符)。
下一步在按钮的IBAction中(通过Interface Builder或通过addTarget:self action:forControlEvents:
设置),您可以调用[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:button];
您可以像往常一样在- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
答案 2 :(得分: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)
答案 3 :(得分:1)
Swift 3
let destinationView = self.storyboard?.instantiateViewController(withIdentifier: "ID") as! ViewController2
destinationView.Value2 = Value1
self.present(destinationView, animated: true, completion: nil)
答案 4 :(得分:1)
您只需使用
即可NSUserDefaults的
存储数据并检索您需要的页面
在视图控制器A中
[[NSUserDefaults standardUserDefaults] setObject:aData forKey:@"DATA"];
[[NSUserDefaults standardUserDefaults] synchronize];
在目标控制器
中NSData * aData = [[NSUserDefaults standardUserDefaults] valueForKey:@"DATA"];
答案 5 :(得分: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 *名称答案 6 :(得分: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];