0

我正在尝试做的事情:

我有一个ViewController

@implementation ViewController
@synthesize myPicker;
@synthesize myPickerHelper;
@synthesize label;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    myPickerHelper = [[UIPickerViewHelper alloc]initWithUIPickerView:myPicker   andNumberOfComponents:4];

    [myPickerHelper.valuesForComponent1 addObject:@"TEST1"];
    [myPickerHelper.valuesForComponent1 addObject:@"TEST2"];
    ;

    [weightsPicker reloadAllComponents];
    [weightsPicker selectRow:0 inComponent:0 animated:YES];
    //TODO receive message from picker helper.
  //label.text = ... set a listner (like that one in java) or what else
    }

}

我有一个PickerViewHelper:(处理委托和数据源问题)

@interface UIPickerViewHelper : NSObject <UIPickerViewDelegate, UIPickerViewDataSource, OnPickerViewChangeDelegate>

@implementation UIPickerViewHelper 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
  //TODO send message to controller view.
}

和协议:

@protocol OnPickerViewChangeDelegate
@optional
-(void)onChange:(NSString*)result;
@end

我想要的是在我的UiPickerViewHelper类中使用协议方法“onChange”,以便通知我的标签(在我的 ViewController 中)。我知道如何在 Java 中解决这个问题,但我不知道如何在目标 c 中解决这个问题。

问候

4

2 回答 2

1

UIPickerViewHelper.h

@property (nonatomic, assign) id <OnPickerViewChangeDelegate> delegate

UIPickerViewHelper.m

@synthesize delegate;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //TODO send message to controller view.
    [self.delegate onChange:[NSString stringWithFormat:@"Selected row %d", row]];
}

视图控制器.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    myPickerHelper = [[UIPickerViewHelper alloc]initWithUIPickerView:myPicker   andNumberOfComponents:4];
    myPickerHelper.delegate = self;

    ...
}

-(void)onChange:(NSString*)result {

    NSLog(@"Here is the result: %@", result);
    label.text = result;
}
于 2013-02-25T14:26:59.657 回答
0

没有UIPickerview用于更改的委托方法。因为它会停在任何位置并且

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

方法将被调用,因此如果用户在选择器停止动画/滚动之前退出......隐藏的方法UIPickerView必须在滚动/动画时为最后选择的值调用 uipicker。

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    if(delegate respondsToSelector:@selector(onChange:)){
        [delegate onChange:row];//return the index of row selected
     }
    }
于 2013-02-25T14:36:27.763 回答