iphone - 如何传递 UIscrollview 以供委托(delegate)类使用

标签 iphone objective-c ios xcode

我对委托(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/

相关文章:

php - 部署我的第一个移动站点 : 3 newbie questions

iphone - UISegmentedControl 在 UIView 中没有响应

ios - Xcode 11.4 - 终端介绍

ios - 登录/注销用户出现推送通知问题

适用于 Objective-c 的 IOS Admob 插页式广告

ios - Firebase 在 Swift 中使用 PromiseKit/AwaitKit 调用永远不会返回

iphone - CoreFoundation 中的 CFStringGetLength 导致 iOS 崩溃

ios - 使用 OpenGL ES 在 iPhone 上渲染到立方体贴图

objective-c - 在 UITableView 中的 UIImageView 中从网络加载图像

ios - iOS 中的纹理和动画