问题描述
我使用Xcode 4处理iPad项目。
I work on a project for iPad with Xcode 4.
我有一个带有许多UITextField的主视图控制器。
I have a main view controller with many UITextField.
TextFieldDelegate是单独文件中的一个单独的类。
The TextFieldDelegate is a separate class in a separate file.
如何从TextFieldDelegate引用主视图控制器的属性(到UITextField) (例如将值赋给double)?
How can I refer, from TextFieldDelegate to a property (to a UITextField) of the main view controller (for example assign a value to a double)?
谢谢。
推荐答案
在大多数情况下,如果要使用单独的委托,则不需要比传递给委托的更多信息(方法的参数)。但是,如果您不想将MainViewController用作UITextField的委托,则可以在MainViewController实例中初始化TextFieldDelegate并将其传递给MainViewController实例。
In most cases, if you want to use a separate delegate you should not need more information than what is passed to the delegate (the method's parameters). However, if you don't want to use your MainViewController as a delegate for your UITextField, you can initialize your TextFieldDelegate in your MainViewController instance and pass it the MainViewController instance.
例如,您可以:
#import "MainViewController.h"
@interface TextFieldDelegate<UITextFieldDelegate> {
MainViewController* mainViewController;
}
@property(nonatomic,retain) MainViewController* mainViewController;
-(id)initWithController:(MainViewController*)controller;
@end
@implementation TextFieldDelegate
@synthesize mainViewController;
-(id)initWithController:(MainViewController*)controller {
if(self = [super init]) {
//some stuff
self.mainViewController = controller;
}
return self;
}
@end
然后在你的MainViewController中:
Then in your MainViewController:
TextFieldDelegate tfd = [[TextFieldDelegate alloc] initWithController:self];
您只需要将TextFields的委托设置为tfd,您就应该能够引用MainViewController属性来自TextFieldDelegate实例。只要将MainViewController实例发送到TextFieldDelegate实例,也可以在其他地方启动它。
You just need to set the TextFields' delegate to tfd and you should be able to reference the MainViewController properties from the TextFieldDelegate instance. It's also possible to initiate it somewhere else, as long as you send the MainViewController instance to your TextFieldDelegate instance.
编辑:woups忘了一些'*'
woups forgot a few '*'
这篇关于请参阅另一个类的主视图控制器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!