TextField在其他ViewController中的Delegate

时间:2013-07-25 19:12:40

标签: iphone ios delegates uitextfield

我在子视图中有一个textField,我已经将其子类化了。现在我想在我原来的视图中调用textField的委托方法。

我尝试使用此代码,但不知道这些方法是否被调用:

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

“NewImageViewController”是我的原始视图。

修改

我的Subclass.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");
}

2 个答案:

答案 0 :(得分: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


答案 1 :(得分: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
}

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