【问题标题】:Passing 2 strings via the delegate通过委托传递 2 个字符串
【发布时间】:2011-03-29 23:50:36
【问题描述】:

我想知道如何通过我的委托传递 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;

【问题讨论】:

    标签: iphone delegates uiviewcontroller


    【解决方案1】:

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

    - (void) functionName:(NSString*)str1 string2:(NSString*)str2;
    
    [delegate functionName:stringtogive1 string2:stringtogive2];
    

    这就是所有人;-)

    【讨论】:

      【解决方案2】:

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

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

      无论你是要传递一个还是一百个值,都可以用这个简单的方法传递。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多