通过委托传递2个字符串

时间:2011-03-29 23:50:36

标签: iphone delegates uiviewcontroller

我想知道如何通过我的委托传递2个字符串?我想将它从viewcontroller2(SelectStationViewController)传递给viewcontroller1(SubGabViewController)。

我现在正在使用它,只传递1个字符串(NSString * test)。

这是我设置的代码:

// 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个字符串传递给委托,我会做这样的事吗?

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

然后设置另一个这样的功能?

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

2 个答案:

答案 0 :(得分:2)

只需在SubGabViewController中编写一个委托方法,该方法在其params而不是一个字符串中获取2个字符串并从SelectStationViewController调用它:

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

[delegate functionName:stringtogive1 string2:stringtogive2];

这是所有人; - )

答案 1 :(得分:0)

作为另一种选择,您可以通过委托方法传递一个字符串数组,如下所示:

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

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