gpt4 book ai didi

iphone - TextField在其他ViewController中的Delegate

转载 作者:行者123 更新时间:2023-11-29 03:44:49 25 4
gpt4 key购买 nike

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

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

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

“NewImageViewController”是我的原始 View 。

编辑:

我的子类.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 中的委托(delegate)方法:

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

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

最佳答案

因为您正在将 UITextField 作为 subview 中的属性

就这样吧

我发布了与您的情况类似的示例代码希望它能帮助你,只是经历一下它可能会帮助你

 #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


关于iphone - TextField在其他ViewController中的Delegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17866788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com