2013-10-01 63 views
0

我曾嘗試使用數組來推到一個UIViewController:如何使用NSDictionary從UITableViewController推送到UIViewController?

[email protected][@"New York", @"Chicago", @"Los Angeles", @"Miami"]; 

,然後在我的didSelectRowAtIndexPath

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

但現在所有的在陣推項目NewYorkViewController。我相信一個NSDictionary會工作

NSDictionary *[email protected][@{@"city":@"NewYork",@"viewController":NewYorkViewController}, @{@"city":@"Chicago",@"viewController":ChicagoViewController}, @{@"city":@"LosAngeles",@"viewController":LosAngelesViewController}, @{@"city":@"Miami",@"viewController":MiamiViewController}]; 

但是我是小白,從來沒有與NSDictionary工作。如果你會如此善意地解釋我將如何去做這件事。如果你知道使用數組的方法,那也可以。

ps。 我不使用storyboards,可能還解釋什麼,我會在我的cellForRowAtIndexPath做的事:和numberOfRowsInSection

感謝

+0

在NSDictionary .. {「viewController」:[NewYorkViewController class}} .. then [self.navigationController pushViewController:[[NSDictionary objectFor:「New York」] alloc] init] animated:YES]; – zt9788

+0

你介意在迴應中擴展嗎? – rivtracks

回答

0

我猜你想從的UITableViewController到NewYorkViewController傳輸數據。通常的做法是在你的NewYorkViewController中有一個屬性,聲明一個數組或你想要的東西。

@property (nonatomic,retain) NSMutableArray *itemsArray; 

,並在didSelectRowAtIndexPath方法,同時創造NewYorkViewController的對象

NewYorkViewController *newyorkObject = [NewYorkViewController alloc]init]; 
newyorkObject.itemsArray = self.cities; 
[self.navigationController pushViewController:newyorkObject animated:YES]; 

和NewYorkViewController的viewDidLoad中添加此:

NSLog(@"items array: %@",self.itemsArray); //to check whether you got what you passed from the previous class. 
+0

但我只將我的導航控制器發送到我的NewYorkViewController。我希望紐約推動NYViewController,芝加哥推到ChicagoViewController等只發送到newyorkObject。 – rivtracks

+0

老兄來了一個你不能發送navigationController到NewYorkViewController,我正在做的是創建一個NewYorkViewController的對象,並賦值其屬性的值,然後推self.navigationController中的NewYorkViewController的對象。你總是推送ViewController的對象而不是ViewController類本身。試試這個,讓我知道。 – satheeshwaran

+0

我明白了,但是我又在哪裏添加了我的其他「對象」 – rivtracks

0

我使用的是Windows系統了,所以下面的代碼可能包含錯誤。

使用反射來獲取IOS類 然後用這個來的Alloc對象和操作它

您可以使用[Object類]NSClassFromString(「類名」)獲得的類型類

這樣的:

NSString * ClassName = @ "yourClassName"; 
Class * tempClass = NSClassFromString (ClassName); 
//if your class is subclass of UIViewController 
UIViewController * tempObj = [[tempClass alloc] init]; 
//operating it 
[self.navigationController pushViewController: tempObj animated:YES]; 
[tempObj release]; 

爲了您的代碼,你可以這樣改:

//NSDictionary [email protected]{@"key",@"value",....} 
NSDictionary *[email protected]{@"NewYork",[NewYorkViewController Class]} 
NSDictionary *citietwo = @{@"Chicago",NSClassFromString(@"ChicagoViewController")}; 
//other citys 
NSArray* cities = @{citieone,citietwo,...}; 
//in UITableView 
NSString* cityName = [[[citis objectForIndex:index] allKeys] objectAtIndex:0]; 
Class* controllerClass = [[[citis objectForIndex:index] allValues] objectAtIndex:0]; 
// operating it 
UIViewController * controller= [[controllerClass alloc] init]; 
[self.navigationController pushViewController: controller animated:YES]; 
[controller release]; 
0

我做了類似於我的應用程序,但我使用NSArray而不是NSDictionary,但邏輯是類似的恕我直言。

我用我的對象創建了一個NSArray,還有一個方法可以返回它們的計數,也可以返回索引處的名稱。這是當我想從數組中獲取特定對象時,我可以快速訪問它。我已經改變了我的代碼來接受NSDictionary,以便您可以使用。

爲了創建UITableView中的行數,我創建了一個方法,該方法在調用時返回行數。

-(NSInteger) numberOfCities 
{ 
    NSArray * allKeys = [mutableDictionary allKeys]; 
    return [allKeys count]; 
} 

這將在

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section 
{ 
    return [self numberOfCities]; 
} 

所以被稱爲使UITableView中會有行作爲NSDictionary的

的確切人數爲了在特定的索引,使返回的名稱你可以在UITableViewCell中顯示那個名字,我創建了這個方法

-(NSString*) cityNameAtIndex: (NSInteger) index 
{ 
    return NSString* cityName = [[[mutableDictionary objectForIndex:index] allKeys] objectAtIndex:index]; ; 
} 

此方法可在

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath 

中調用以將不同的城市名稱分配給表格中的不同單元格。

在委託

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath 

,你可以調用此方法並推至不同viewControllers

UIViewController* selectedController = nil ; 
NSString* nameOfCity = [self cityNameAtIndex: indexPath.row] ; 

    if([nameOfCity isEqualToString:@"New York"]) 
     selectedController = [[NewYorkViewController alloc] init]; 
    else if([nameOfCity isEqualToString:@"Chicago"]) 
     selectedController = [[ChicagoViewController alloc] init]; 
    else if([nameOfCity isEqualToString:@"Miami"]) 
     selectedController = [[MiamiViewController alloc] init]; 
    else if([nameOfCity isEqualToString:@"Los Angeles"]) 
     selectedController = [[LosAngelesViewController alloc] init]; 

爲此

然後,你就必須創建不同viewControllers(芝加哥,紐約,洛杉磯,邁阿密),但如果你已經創建了它們並把它們作爲NSDictionary中的一個對象,你可以使用nameOfCity來獲取屬於該城市的NSDictionary中的viewController對象,然後將selectedController分配給Ť帽子viewController。

但是,如果您只使用NSDictionary來存儲城市的名稱,我認爲NSArray將是一個更好的選擇。我在appDelegate中創建了我的,NSArray中的名字是我的類中的#define,名爲global.h,因爲我需要NSArray和名稱在許多不同的viewControllers中,所以如果你也需要這樣做,你也可以這樣做。

希望這會有所幫助! :)

相關問題