我是iphone的新用户,我正在使用UITextfield输入值并显示在其中,但我无法将textfield值传递给另一个类。
classA
textfieldA.text=3
如何将textfieldA值传递给textfieldB?
classB
textfieldB.text=
答案 0 :(得分:1)
简单:
<强> ClassA.h 强>
@interface ClassA : NSObject {
UITextField* textfield;
}
@property(nonatomic, retain) UITextField* textfield;
<强> ClassB.h 强>
@ClassA;
@interface ClassB : NSObject {
ClassA* refClassA;
UITextField* textfield;
}
@property(nonatomic, retain)ClassA* refClassA; // you can also use assign instead of retain if you masterize the concept
@property(nonatomic, retain)UITextField* textfield;
<强> ClassA.m 强>
@synthesize textfield;
- (void) somefunction {
self.textfield.text=@"3";
}
// and somewhere when creating ClassB
yourClassBObject.refClassA = self;
<强> ClassB.m 强>
#import "ClassA.h"
@synthesize refClassA;
@synthesize textfield;
- (void) somefunction {
self.textfield.text = self.refClassA.textfield.text;
}
答案 1 :(得分:0)
您可以通过以下方式设置委托变量中文本字段的值:
appDelegate.textValueVariable = textfieldA.text ;
现在您可以通过以下方式从ClassB访问委托变量:
(在ClassB的viewDidLoad
或viewWillAppear
)中写下以下代码
textfieldB.text = appDelegate.textValueVariable ;
答案 2 :(得分:0)
将TextfieldA值转换为变量,然后将类推送到另一个类,然后使用类发送您的值,并在另一个类中创建相同类型的变量,并将该变量的值分配给类似的文本字段此 -
Class A {
textfieldA.text=3;
int x;
anotherViewController *subControllr=[[anotherViewController alloc] initWithNibName:@"anotherViewController" bundle:nil];
[subControllr setY:x];
UINavigationController *controller =[[UINavigationController alloc] initWithRootViewController:subControllr];
controller.navigationBar.barStyle = UIBarStyleBlackOpaque ;
[subControllr setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[[self navigationController] presentModalViewController:controller animated:YES];
[subControllr release];
[controller release];
}
现在Class B{
//创建与setY相同的名称变量以传递值
int y;
textfieldB.text=y;
}