我对委托(delegate)并不陌生,但我对在自己的类中创建委托(delegate)的语法很陌生。
本质上,我有一个在自己的类中创建的 PickerView 委托(delegate)。我希望能够将选择文本放入已在 ViewController 中声明的 UIScrollView 中的文本框中。但是我不知道向委托(delegate)显示 View 的语法。以下是委托(delegate)的 .h 和 .m 文件。如果还有任何其他缺少的组件,我需要让我的 ViewController 看到我没有的委托(delegate),也请告诉我。错误出现在 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 并让它使用这个委托(delegate)。然后这个委托(delegate)必须更新我的 ViewController 中 UIScrollView 中的文本字段。我需要以某种方式让这个委托(delegate)查看/使用 UIScrollView(inputsView)。
谢谢
最佳答案
再次编辑
你的 View Controller 类
在这里,您将文本字段的引用(例如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
关于iphone - 如何传递 UIscrollview 以供委托(delegate)类使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9114440/