0

我在子视图中有一个文本字段,我已经对其进行了子类化。现在我想在我原来的视图中调用 textField 的委托方法。

我用这段代码试过了,但不知何故没有调用这些方法:

NewImageViewController *ni = [[NewImageViewController alloc] init];
textField.delegate = ni;

“NewImageViewController”是我的原始视图。

编辑:

我的子类.h:

#import <UIKit/UIKit.h>

@protocol SubviewProtocol<NSObject>
- (void) textFieldDidEndEditing:(UITextField *)inTextField;
- (void) textFieldDidBeginEditing:(UITextField *)inTextField;
@end

@interface PopOverViewController : UIViewController <FPPopoverControllerDelegate>

@property (strong, nonatomic) UITextField *textField;
@property (nonatomic, assign) id<SubviewProtocol> delegate;

@end

Subview.m 中的 viewDidLoad :

textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 170, 30)];
    textField.placeholder = @"Example";
    textField.backgroundColor = [UIColor whiteColor];
    textField.alpha = 0.9;
    textField.borderStyle = UITextBorderStyleLine;
    textField.layer.borderColor = [[UIColor grayColor]CGColor];
    textField.textColor = [UIColor blackColor];
    textField.delegate = self;
    [self.view addSubview:textField];

NewImageViewController.m 中的委托方法:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"asd");
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"fgh");
}
4

2 回答 2

1

由于您将 UITextField 作为子视图中的属性

就这样做

我正在发布与您的案例类似的示例代码希望它可以帮助您完成此操作它可能会帮助您

 #import "SubView.h"
 @implementation SubView
 @synthesize aTextField;

 - (id)initWithFrame:(CGRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    UITextField *atextField = [[UITextField alloc]initWithFrame:CGRectZero];
    self.aTextField = atextField;
    [self addSubview:atextField];
    [atextField release];

  }
 return self;
}

   -(void)dealloc{
    [self.aTextField release];
    [super dealloc];
   }

 //i am setting the frame hear 
 -(void)layoutSubviews
   {
      self.aTextField.frame = CGRectMake(20,30, 100, 45);


   }

  @end


   // in your main view

   #import "ViewController.h"
   #import "SubView.h"

   @interface ViewController ()<UITextFieldDelegate> // add this

   @end

   @implementation ViewController

   - (void)viewDidLoad
   {
     [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    //see below lines carfully
      SubView *subView = [[SubView alloc]initWithFrame:self.view.frame];

   subView.aTextField.delegate = self; // you need do this
   subView.aTextField.placeholder = @"type hear";
   subView.aTextField.backgroundColor = [UIColor greenColor];

   [self.view addSubview:subView];

   }

   - (void)didReceiveMemoryWarning
   {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }  

     //check out in main view
  -(void) textFieldDidEndEditing:(UITextField *)inTextField
    {
          NSLog(@"textField end editing");
    }
     - (void) textFieldDidBeginEditing:(UITextField *)inTextField
      {
          NSLog(@"textField begin editing");
      }


   @end


于 2013-07-26T06:46:37.930 回答
0

您可以在 textField 所在的子视图中实现委托方法,而不是在原始视图中设置委托。

您可以为您的子视图实现一个协议,如下所示:

在文件 YourSubview.h

@protocol YourSubviewProtocol<NSObject>
- (void) textFieldDidEndEditing:(UITextField *)inTextField;
- (void) textFieldDidBeginEditing:(UITextField *)inTextField;
@end
@interface YourSubview
@property (nonatomic, assign) id<YourSubviewProtocol> delegate;
//properties and methods

@end

在实现文件中,即 YourSubview.m,您必须从 TextField 委托方法内部调用这些委托方法,如下所示

[[self delegate] textFieldDidBeginEditing:textField];

在文件 NewImageViewController.h

@interface NewImageViewController<YourSubviewProtocol>

//properties and methods

@end 

在文件 NewImageViewController.m 中,您必须实现委托方法:

- (void) textFieldDidEndEditing:(UITextField *)inTextField;
{
  //do the operation
}
- (void) textFieldDidBeginEditing:(UITextField *)inTextField;
{
  //do the operation
}

希望这会有所帮助,一切顺利!:)

于 2013-07-25T19:27:07.720 回答