开发者

Passing 2 strings via the delegate

I'm wondering how I can pass 2 strings through my delegate? I want to pass it from viewcontroller2 (SelectStationViewController) to viewcontroller1 (SubGabViewController).

I have it currently working, just passing 1 string (NSString *test).

Here's the code I have set up:

// in SelectStationViewController.h
@protocol SelectStationViewControllerDelegate;
...
@interface ... {
   开发者_如何学Python 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];
}

In order to pass 2 strings to the delegate, would I do something like this?

// 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];

and then have another function set up like this?

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


Just write a delegate method in SubGabViewController that gets 2 strings in its params instead of one and call it from SelectStationViewController :

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

[delegate functionName:stringtogive1 string2:stringtogive2];

That's all folks ;-)


As another option, you could pass an array of strings through your delegate method, like this:

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

Whether you have one or a hundred values to pass over, they can be passed with this one simple method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜