如何将文本字段值传递给iphone中的另一个类文本字段?

时间:2011-11-08 06:39:48

标签: iphone

我是iphone的新用户,我正在使用UITextfield输入值并显示在其中,但我无法将textfield值传递给另一个类。

classA
textfieldA.text=3

如何将textfieldA值传递给textfieldB?

classB
textfieldB.text=

3 个答案:

答案 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的viewDidLoadviewWillAppear)中写下以下代码

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;
}