2011-03-29 27 views
0

我想知道如何通過委託傳遞2個字符串?我想從viewcontroller2(SelectStationViewController)傳遞給viewcontroller1(SubGabViewController)。通過委託傳遞2個字符串

我有它目前的工作,只是傳遞1個字符串(NSString *測試)。

下面的代碼我已成立:

// in SelectStationViewController.h 
@protocol SelectStationViewControllerDelegate; 
... 
@interface ... { 
    id <SelectStationViewControllerDelegate> delegate; 
} 
@property (nonatomic, assign) id <SelectStationViewControllerDelegate> delegate; 
@end 

@protocol SelectStationViewControllerDelegate 
- (void)selectStationViewControllerDidFinish:(NSString *)selectedStationName; 
@end 

// in SelectStationViewController.m 
NSString *test = [[copyListOfItems objectAtIndex:indexPath.row] objectForKey:@"hash"]; 
[delegate selectStationViewControllerDidFinish:test]; 

// in SubGabViewController.h 
@interface...<SelectStationViewControllerDelegate> 

// in SubGabViewController.m 

// set selectedStationName as currentStationName 
- (void)selectStationViewControllerDidFinish:(NSString *)selectedStationName 
{ 
    NSLog(@"selectedStationName is = %@", selectedStationName); 
    [self setCurrentStationName:selectedStationName]; 
} 

爲了通過2串委託,我會做這樣的事?

然後有另一個功能設置爲這樣?

- (void)selectStationViewControllerDidFinish:((NSString *)selectedStationName; 
- (void)selectLineViewControllerDidFinish:((NSString *)selectedLineName; 

回答

2

在SubGabViewController即得到2串在其PARAMS而不是一個只寫一個委託方法,並從SelectStationViewController調用它:

- (void) functionName:(NSString*)str1 string2:(NSString*)str2; 

[delegate functionName:stringtogive1 string2:stringtogive2]; 

這是所有鄉親;-)

+0

完美。謝謝! – Aaron 2011-03-30 00:42:00

+0

@Aaron:不客氣:-) – Oliver 2011-03-30 10:04:46

0

作爲另一種選擇,您可以通過代理方法傳遞一組字符串,如下所示:

- (void)selectStationViewControllerDidFinish:(NSArray *)selectedStations; 

無論你有一個還是一百個值都可以通過,他們都可以通過這個簡單的方法傳遞。