2013-04-25 119 views
0

在我的項目中,我有3個控制器;在UIDatePicker和UITableView之間進行通信

  • NavigationController
  • ServiceTableViewController
  • DateTableViewController

ServiceTableViewController是初始視圖控制器。它有幾行提示用戶輸入數據,這些數據將通過電子郵件發送到特定的電子郵件地址。其中一行在點擊時將用戶發送到DateTableViewController,該提示用戶從UIDatePicker中選擇一個日期。

我面臨的問題是從DateTableViewController獲取數據,以便在ServiceTableViewController上顯示標籤以顯示用戶在DateTableViewController中選擇的日期。我知道如何從一個視圖控制器獲取信息到另一個視圖控制器,但是反過來說,我不知道該怎麼做。任何幫助表示讚賞。

回答

0

看看這個: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html

有幾種方法可以查看控制器之間傳遞數據來回。

但老實說代表是真的你真的需要這聽起來像在當前的情況下。 看到這一點 - >(Passing Data between View Controllers


說了這麼多,如果你使用的代表,這裏是如何--- 設置在頂部DateTableViewController.h協議,像這樣:

@protocol DateTableViewControllerDelegate <NSObject> 
- (void)userSelectedThisDate:(NSDate *)d; 
end 

放這與其他性質

@property (nonatomic, weak) id <DateTableViewControllerDelegate> delegate; 

DateTableViewController.m與日發回

[self.delegate userSelectedThisDate:withTheDateToSendBack]; 

ServiceTableViewController.h添加

#import "DateTableViewController.h" 
@interface ServiceTableViewController : UIViewController <DateTableViewControllerDelegate> 

因爲你在ServiceTableViewController.mUINavigationController,某處添加此當你即將推向DateTableViewController

DateTableViewController *vc = [[DateTableViewController alloc] init]; 
self.delegate = self; 
[self.navigationController pushViewController:vc animated:YES]; 

終於把委託方法ServiceTableViewController.m

- (void)userSelectedThisDate:(NSDate *)d { 
    NSLog(@"%@", d); // this should show the returned date 
} 
0

研究委託模式(here)(蘋果框架內的大量使用模式)。你想定義一個委託協議,允許將日期傳遞給委託。

您可以實現的模式與單一方法的@protocol並在DateTableViewController的屬性。在推動DateTableViewController之前,ServiceTableViewController將自己設置爲代表。

或者,你可以實現使用塊。再次,ServiceTableViewController在推動DateTableViewController之前設置該塊。

相關問題