开发者

using composition to access values of two different variables?

开发者 https://www.devze.com 2023-03-25 06:55 出处:网络
I have a class A having textFieldA and class B having textFieldB. I want to access values of textFieldA and textFieldB into class C and wants to show in label.

I have a class A having textFieldA and class B having textFieldB.

I want to access values of textFieldA and textFieldB into class C and wants to show in label.

If I use composition, how can i access these values or any other? Well this is my classA -

(IBAction开发者_JAVA技巧)buttonPress:(id)sender
{ NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
 [defaults setObject:field1.text forKey:@"field1Key"]; 
[defaults setObject:field2.text forKey:@"field2Key" ]; 
[defaults setObject:field3.text forKey:@"field3Key"]; 
[defaults synchronize]; 
ConfirmController *conf=[[ConfirmController alloc]initWithNibName:nil bundle:nil];
 // corfim=[[ConfirmController alloc]initWithNibName:nil bundle:nil]; 
[self.navigationController pushViewController:conf animated:YES ]; 
[conf release]; 
}



This is classB:-- -
(void)viewWillAppear:(BOOL)animated
{ 
label.text=massage; currancyLabel.text=massage1; 
[super viewWillAppear:animated]; 
outputRate.text=[[NSUserDefaults standardUserDefaults]objectForKey:@"outputRateKey"];
 float x= [amount.text floatValue]*[massage floatValue]; 
outputRate.text=[NSString stringWithFormat:@"%.2f", x]; 
} 

i want to show these classes textFields in class C ConfirmController class is classC, formController is classA, currancyController is classB


I don't understand what you mean for "composition". Anyway if in your class "C" you initialize two instances:


A *a;
B *b;

then you can access the two text values using:

NSString *textA = a.textFieldA.text;
NSString *textB = b.textFieldB.text;


Step1: Create an object for both class A and B lets say objA and ObjB

Step2: Create a property textFieldA and textFieldB respectively for class A and class B

Step3: Create a label in your class C lets say labelC

Step4: Now access the value of text field

NSString* msgA = objA.textFieldA.text
NSString* msgB = objB.textFieldB.text

Step5:

labelC.text = [msgA stringByAppendingString:msgB];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号