如何传递UIscrollview以供委托类使用

时间:2012-02-02 14:55:34

标签: iphone objective-c ios xcode

我不是代表新手,但我不熟悉在自己的类中创建委托的语法。

基本上我有一个我在自己的类中创建的PickerView委托。我希望能够将选择文本放入已在我的ViewController中声明的UIScrollView中的文本框中。但是我不知道显示委托视图的语法。以下是代理的.h和.m文件。如果有任何其他缺少的组件我需要让我的ViewController看到我的代表我没有,请告诉我。错误在didSelectRow方法中。

RadioPickerDelegate.h

 #import <Foundation/Foundation.h>
 @class MissionInputViewController;


 @interface RadioPickerDelegate : NSObject<UITextFieldDelegate>{
   NSArray *radioOptions;
 }
 @property (nonatomic, retain) NSArray *radioOptions;


 @end

RadioPickerDelegate.M

#import "RadioPickerDelegate.h"
#import "MissionInputViewController.h"

@implementation RadioPickerDelegate

@synthesize radioOptions;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
    UITextField *textField =  (UITextField *)[inputsView viewWithTag:pickerView.tag];
    textField.text = [radioOptions objectAtIndex:row];


     //The error is at the inputsView value.  That is my UIScrollView from the view controller

}

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return 2;    

}

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    radioOptions = [[NSArray alloc] initWithObjects: @"Yes", @"No"];

   return [radioOptions objectAtIndex:row];


}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    int sectionWidth = 200;

    return sectionWidth;
}



@end

基本上我想在我的ViewController中创建一个PickerView并让它使用这个委托。然后,该委托必须更新ViewController中UIScrollView中的文本字段。我需要以某种方式让这个委托看到/使用UIScrollView(inputsView)。

由于

2 个答案:

答案 0 :(得分:1)

再次编辑


你的视图控制器类
在这里,您将文本字段的引用(例如textFiledSV)传递给无线电选择器类

//set the delegate where you have created the object of RadioPickerDelegate class
radioPickerDelegateObject.textField = yourTextField;


RadioPickerDelegate.h文件

#import <Foundation/Foundation.h>
@class MissionInputViewController;


@interface RadioPickerDelegate : NSObject<UIPickerViewDelegate>{
    NSArray *radioOptions;
    //Text Field so that we can set it's value
    UITextField *textField;
}
@property (nonatomic, retain) NSArray *radioOptions;
@property (nonatomic, assign) UITextField *textField;

@end


RadioPickerDelegate.m文件

#import "RadioPickerDelegate.h"
#import "MissionInputViewController.h"

@implementation RadioPickerDelegate

@synthesize radioOptions;
@synthesize textField;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
   //here you are setting the text of the text field which was passed priviously (textFiledSV) thus it changes the text of the textField of you UIScrollView
   textField.text = [radioOptions objectAtIndex:row];  
}

//other method definitions

@end

答案 1 :(得分:0)

我确定错误来自inputsView,而且没有在任何地方声明。

您的界面说它符合UITextFieldDelegate而不是< UIPickerViewDelegate>