2012-01-26 28 views
4

即時通訊IOS和Objective-C以及整個MVC範例,我堅持以下。有多少種方式來通過/共享數據黑白視圖控制器

我正在處理(副本)聯繫應用程序,也可在iPhone中作爲應用程序內置。我想通過另一個視圖控制器傳遞數據和數據通(空):(。

我的問題是,如何從一個視圖到另一個傳輸數據?

回答

2

因爲大多數答案都是,在一個控制器和另一個控制器之間傳遞數據只是意味着將一個變量從一個控制器分配給另一個控制器。 如果您有一個控制器列出您的聯繫人,另一個控制器列出您的聯繫人,另一個控制器顯示聯繫人詳細信息,並且流程從列表開始並在選擇聯繫人後轉到詳細信息,您可以分配聯繫變量(可能是數組中的一個對象顯示在你的列表中),並在顯示這個之前將其分配給詳細視圖控制器。

- (void)goToDetailViewControllerForContact:(Contact *)c 
{ 
    ContactDetailViewController *detailVC = [[[ContactDetailViewController alloc] init] autorelease]; 
    detailVC.contact = c; 
    [self.navigationController pushViewController:c animated:YES]; 
    //[self presentModalViewController:detailVC animated:YES]; //in case you don't have a navigation controller 
} 

在另一方面,如果要插入從細節控制器列表控制器新的聯繫人,我想最好的辦法是指定列表控制器作爲代表到細節之一,所以當添加聯繫人時,委託人會收到通知並按預期行事(將聯繫人插入數組並重新加載表格視圖?)。

@protocol ContactDelegate <NSObject> 
- (void)contactWasCreated:(Contact *)c; 
// - (void)contactWasDeleted:(Contact *)c; //may be useful too... 
@end 

@interface ContactListViewController : UIViewController <ContactDelegate> 
@property (nonatomic, retain) NSArray *contacts; 
... 
@end 

@implementation ContactListViewController 
@synthesize contacts; 
... 

- (void)goToDetailViewControllerForContact:(Contact *)c 
{ 
    ContactDetailViewController *detailVC = [[[ContactDetailViewController alloc] init] autorelease]; 
    detailVC.contact = c; 
    detailVC.delegate = self; 
    [self.navigationController pushViewController:c animated:YES]; 
    //[self presentModalViewController:detailVC animated:YES]; //in case you don't have a navigation controller 
    } 

- (void)contactWasCreated:(Contact *)c 
{ 
    self.contacts = [self.contacts arrayByAddingObject:c]; //I'm not sure this is the correct method signature... 
    [self reloadContacts]; //may be [self.tableView reloadData]; 

} 

... 
@end 


@interface ContactDetailViewController : UIViewController 

@property (nonatomic, assign) id<ContactDelegate> delegate; 
... 
@end 


@implementation ContactDetailViewController 
@synthesize delegate; //remember to don't release it on dealloc as it is an assigned property 
... 

- (void)createContactAction 
{ 
    Contact *c = [[[Contact alloc] init] autorelease]; 
    [c configure]; 
    [self.delegate contactWasCreated:c]; 
} 

... 
@end 
+1

謝謝里卡德佩雷斯:) –

2

從技術上說,你不應該!
整個想法是不是「意見」來控制數據發生的事情。

你想做的是在控制器之間傳遞數據(我想這正是你打算做的東西)。

您可以共享模型(一個對象的實例,這兩個視圖控制器都會d訪問)保留您想要共享的數據,

您可以使用通知來傳遞數據(它最適合於某些情況)。

您可以將某些內容寫入磁盤並稍後再讀取。

您可以使用NSUserDefaults。

您可以使用KeyChain。

...

1

最好的辦法是:

  • 在第二視圖控制器
  • 當您創建它,只需設置屬性與

聲明適當@propertyviewController.property = valueYouWantToPass;

0

我是一個很大的fa代表和議定書。
並且在某些場合使用Singleton模式。

0

兩種方式來傳遞/視圖控制器

之間創建一個對象,併發送的數據這樣

QGraduteYr *tableverify=[[QGraduteYr alloc]initWithStyle:UITableViewStyleGrouped]; 
    tableverify.mystring=myString 
    [self.navigationController pushViewController:tableverify animated:YES]; 

另一方法共享數據是STOR它在代表和經由共享代表

使用它
MedicalAppDelegate *appdelegate=(MedicalAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    appdelegate.collnameStr=collStr; 

和烏斯本appdelegates值等。無論您需要

+1

你的答案中的第二種方法實際上是很差的做法。不要像全局變量那樣使用應用程序委託。如果你這樣做,你的代碼需要重新思考。 –

+0

thannks爲改善我的標準 – Marios

+0

樂意幫助:)。請參閱我的答案http://stackoverflow.com/a/8730326/123632瞭解更多詳情。 –

相關問題