2011-08-21 49 views
1

我正在構建一個基於實用程序的應用程序,數據存儲在MainViewController中,現在我知道如何將數據傳遞給FlipsideViewController(很多方面都是關於此線程的BTW,Sending data from Mainview to Flipside?)。但是我將數據添加到我已添加到翻轉視圖的子視圖(子類UIView)中。我怎樣才能把數據傳遞給這個子視圖?我看到FlipsideViewController.h已經有一個代表和協議,我對代表性的東西真的很陌生。任何幫助將不勝感激!從mainView傳遞數據到子視圖


更新:

在主視圖中,我有一個用戶一對夫婦的文本字段的輸入來創建一個對象。所有對象都存儲在一個數組中。即,我的數據被創建並存儲在MainViewController中。另一方面,我有一個自定義的UIView子類,它允許我根據該數組中的數據執行自己的繪圖。我需要做的是將存儲在MainViewController中的數據傳遞給子視圖。這裏是我的相關代碼:

MainViewController.m

- (IBAction)showInfo:(id)sender {  

    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; 
    controller.delegate = self; 

    controller.receiver = data;//this is what I've done. 

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 

    [controller release]; 
} 

FlipsideViewController.h

@protocol FlipsideViewControllerDelegate; 

@interface FlipsideViewController : UIViewController { 
    id <FlipsideViewControllerDelegate> delegate; 
    DataModel *receiver; //create a property to receive the data transferred from main view 
} 

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate; 
@property (nonatomic, retain) DataModel *receiver; 
- (IBAction)done:(id)sender; 
@end 


@protocol FlipsideViewControllerDelegate 
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller; 
@end 

在上面的代碼中, 「數據」 是MainViewController.h文件中聲明的DataModel的對象。

我想在drawing類(UIView的子類)中做我的自定義繪圖,我如何將FlipsideViewController的數據傳遞給這個子視圖?我需要使用FlipsideViewController.h文件中聲明的委託嗎?提前致謝!

+0

您能更具體嗎?你想傳遞什麼樣的數據? – matteodv

+0

已更新。 – Michael

回答

1

我已經快速瀏覽了模板,並認爲您對代理的用途感到困惑。

此模板中的代理不傳輸數據。當您點擊完成按鈕時,它會回到MainViewController並要求它調用dismissModalViewControllerAnimated方法,以便它可以刪除視圖控制器。這似乎有點superflous作爲文檔狀態

If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller. 

因此,你真的不需要調用父母來做到這一點。

在界面構建器中,您可以看到FlipsideView.xib已將它的File's Owner設置爲FlipsideViewController.xib

enter image description here

現在,如果你右擊File's Owner你會看到view連接到View這基本上意味着view是屬性的名稱FlipsideViewControllerView是在Interface Builder的元素。

enter image description here

因此,我們可以從FlipsideViewController利用網點訪問在廈門國際銀行文件中的元素。

如果說畫一個標籤,你需要做兩件事情

首先添加在.H屬性和合成它在.M像

// FlipsideViewController.h 
@interface FlipsideViewController : UIViewController 

@property (nonatomic, retain) IBOutlet UILabel *testLabel; // <----- Added this 
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate; 

- (IBAction)done:(id)sender; 

@end 

// FlipsideViewController.m 
@implementation FlipsideViewController 

@synthesize delegate = _delegate; 
@synthesize testLabel = _testLabel; // <----- Added this 

// More methods 

- (void)dealloc 
{ 
    [_testLabel release]; // Always do you memory management 
    [super dealloc]; 
} 

然後回到在Interface Builder

  1. 一個UILabel元素到您的視圖
  2. ctrl + dragFile's Owner噸添加○UILabel您添加
  3. 在我的例子中選擇標籤是testLabel

現在這些迷上了正常。要進行設置標籤的價值的地方是在viewDidLoad:,你現在可以像這樣

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.testLabel.text = @"It Works"; // You would use the data passed in from `MainViewController` 
} 
+0

嗨,感謝您的回覆,儘管我仍然有點困惑。我的自定義視圖是從筆尖加載時的翻轉視圖實例。我想我應該在initWithCoder()方法中傳遞數據,對吧?而不是在flipviewcontroller中調用addSubview方法? – Michael

+0

對不起,我沒有看太久的模板,我會更新我的答案。 –

+0

我可以聲明一個方法 - (void)flipsideViewControllerDidFinish:在FlipsideViewControllerDelegate協議的mainviewcontroller中使用嗎?然後在子視圖中調用這個新方法來獲取數據? – Michael

0

我覺得從一個視圖傳遞到另一個數據最簡單的方法是通過直接設置數據從原始視圖的下一個視圖。

例如;

在您的FlipsideViewController.h中,爲要傳遞的數據聲明一個「容器」。雙方必須是同一班才能正常工作,即。 NSArray到NSArray,NSMutableDictionary到NSMutableDictionary。

NSMutableArray *newData; 
... 
@property (nonatomic, retain) NSMutableArray *newData; // This allows you to access this object from outside this class. 

和FlipsideViewController.m

@synthesize newData; 
... 
[newData release]; 

現在,我們需要傳遞的數據,可以這麼說。假設我們要發送的數據存儲在名爲'results'的NSMutableArray中。

在我們的MainViewController.m中,當我們實例化我們的下一個視圖控制器(在這個例子中是FlipsideViewController)時,我們可以在初始化nib之後直接引用newData可變數組。

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; 
controller.newData = results; 
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:controller animated:YES]; 
[controller release]; 

確保您導入您的FlipsideViewController在MainViewController.h文件。

如果該屬性在.h文件中聲明,那麼您幾乎可以在視圖堆棧中的任何位置引用該對象的內容!

希望對您有所幫助:D

+0

我從主視圖發送數據到flipside視圖沒有問題,就像我在文章中提到的那樣。我想要做的是將數據發送到構建在flipsideview之上的自定義視圖。 – Michael