2011-03-29 26 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; 

无论你有一个还是一百个值都可以通过,他们都可以通过这个简单的方法传递。