2013-03-07 17 views
3

我正在使用故事板,並且我有一個加載動態按鈕的視圖。點擊這個按鈕,我需要加載第二個視圖控制器並傳遞數據。我不能使用segue作爲它的動態按鈕。在視圖控制器之間傳遞數據而無需插播

我用下面的代碼來加載所述第二視圖控制器

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


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

在第二視圖控制器,我需要從第一視圖控制器訪問約5字段。我如何傳遞數據?

+0

爲什麼不在推送前在新的視圖控制器上設置所需的信息?你已經有了你的VC實例。公開屬性(在標題中聲明)或創建新屬性來填充數據。 – Pochi 2013-03-07 11:44:09

回答

1

您可以從第一控制器創建按ctrl +拖動無界的一個按鈕Segue公司的第二個(不要忘記給這個segue和標識符)。

Next在按鈕的IBAction爲可以調用[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:button];

可以將數據傳遞到所述第二控制器,像往常一樣(通過界面生成器或經由addTarget:self action:forControlEvents:設定),在- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

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 
0

首先你可以使用賽格瑞即使你有動態按鈕,只需添加目標的方法來UIButtons和推在該方法中使用segue的視圖控制器。在第二個視圖控制器中,在推送第二個視圖控制器之前,只取幾個屬性併爲這些屬性賦值。像:

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

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

在這之前,你需要分配財產給的NSString *名稱SecondViewController

+0

'vc2.setName:@「Your name」;'不起作用 – user2110495 2013-03-11 15:01:30

+0

我已添加更多評論請檢查 – Zaraki 2013-03-12 06:23:27

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]; 
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) 
+0

swift 3如何? – mythicalcoder 2016-11-09 07:00:27

+0

將添加Swift 3.0 @Maven;) – 2016-11-09 07:09:49

+0

在這裏你去@Maven – 2016-11-09 07:13:45

1

斯威夫特3

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

您可以簡單地使用NSUserDefaults的存儲數據,並在您需要

什麼都頁鑑於控制器獲取的 [NSUserDefaults的standardUserDefaults ] setObject:aData forKey:@「DATA」]; [[NSUserDefaults standardUserDefaults] synchronize];

在目的地控制器 NSData * aData = [[NSUserDefaults standardUserDefaults] valueForKey:@「DATA」];

相關問題