2016-02-29 47 views
-1

所以超NOOB當談到iPhone開發從viewControllers的iOS獲取數據

我有一個標籤欄控制器三個視圖控制器。 我在這裏想要的是當我按下一個按鈕從第一個視圖獲取並顯示TextFields中的數據。

在我firstViewController.h我已經宣佈了的TextField這樣的:

IBOutlet UITextField * driver; 
IBOutlet UITextField * phone; 
IBOutlet UITextField * poe; 
IBOutlet UITextField * doe; 
IBOutlet UITextField * coe; 
IBOutlet UITextField * dod; 
IBOutlet UITextField * customer; 
IBOutlet UITextField * customerContact; 
IBOutlet UITextField * customerTank; 
IBOutlet UITextField * trailer; 
IBOutlet UITextField * truck; 

然後在我的Main.storyboard我已經連接網點每個文本字段

現在我認爲它只是將firstViewController.h導入我的thirdViewController.h使用#import「」並以某種方式檢查觸發按鈕單擊的時間。但在NOOBines方面似乎並不那麼容易。

我認爲在thirdViewController.h做這樣的事情可以使魔:

#import <UIKit/UIKit.h> 
#import "FirstViewController.h" 
@interface ThirdViewController : UIViewController{ 
NSString *theDriver = [driver.text]; 
} 
@end 
+0

我前一陣發生了這個問題。你可以看看這個解決方案[這裏](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) –

+0

也許這是有幫助的http://stackoverflow.com/questions/32979924/passing -data-from-first-view-controller-to-the-lastviewcontroller/32980270#32980270 – vikingosegundo

回答

1

不幸的是它不是那麼容易。假設在第一個和第三個之間有第二個ViewController,那麼您將在使用此方法的segue期間傳遞變量。像司機,電話等你的第二個視圖控制器的.h文件中定義了一些變量,從你那裏有什麼分配這些值

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
     if ([segue.identifier isEqualToString:@"the name of your segue"]) { 
      secondViewController *secondVC = segue.destinationViewController; 
      secondVC.driver = self.driver.text; 
      secondVC.phone = self.phone.text; 
      //continue typing your values and use this to carry this same method in your secondViewController to carry them into your thirdViewController 
} 

編輯1:

下面是一個例子我在我的一個應用程序中使用,我在看一個遊戲列表,我想看看一個單獨的遊戲。這是MatchesViewController.m文件中的代碼。我有我的gameObject(在你的情況下,gameObject將是文本),我想將它發送到下一個視圖控制器。我也有這種進口的「MatchesViewController.m」

#import "EachMatchViewController.h" 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"showOneGame"]) { 
     EachMatchViewController *eachVC = segue.destinationViewController; 
     eachVC.gameObject = self.gameObject; 
    } 
} 

這就是在我的「EachMatchViewController.h」,這是視圖控制器我要定義。

@property (strong, nonatomic) PFObject *gameObject; 

一些在我的「EachMatchViewController.m」我可以使用它傳遞的數據。

+0

您在此處提供的代碼已添加到FirstViewController.m文件,現在的問題是它無法找到segue.SecondViewController。是否需要創造額外的東西或應該彈出嗎? – mogren3000

+0

不管你要去哪裏,它都假設是segue.destinationViewController。如果您正在使用帶segse的故事板,則必須給segue一個名稱,這是您檢查segue.identifier的位置。如果你不使用故事板,我會檢查你的問題下的評論。那裏有一堆很棒的東西。 – kygcoleman

+0

我看到了。我似乎無法訪問聲明的IBOutlet UITextField *驅動程序;在SecondViewController.h中,它似乎沒有找到它。說你在'SecondViewController *'類型的對象上找不到'Property'driver',是否意味着要訪問實例變量'driver'?「 – mogren3000