I have 3 Classes Class A, Class B, Class C.
I am Passing Variable from ClassB to ClassA and then popping ClassB(View Controller)
//ClassB.m
ClassA *obj = [[ClassA alloc]init];
[obj passValue:value];
[obj release];
[self.navigationController popViewControllerAnimated:YES]; //It will pop and show ClassC View Controller.
//ClassA.h
NSString* storeValue;
-(void)passValue:(NSString*)getValue;
//ClassA.m
-(void)passValue:(NSString*)getValue
{
NSLog(@"Value is %@",getValue); // I am Getting Value Here
storeValue = getValue;
}
In ClassC. I am Pushing ClassA View Controller
ClassC.m
ClassA *objA = [[ClassA alloc]init];
[self.navigationController pushViewController:objA animated:YES];
[objA release];
Now in Class A. Inside View Didload. I tried to print storeValue. but it returns null. What i am doing wrong here. can anyone guide me do it in right way with Sample Code.
I tried using property to access the variable. but it returns null value.
Thanks, Anish
I had considered following hierarchy of class - C -> A -> B You don't need to create any object for Class A, as that is already on navigation stack. Use -
NSMutableArray *activeControllerArray = [self.navigationController.viewControllers mutableCopy];
for(int i = 0, i <[activeControllerArray count], i++) {
if([[activeControllerArray objectAtIndex:i] isKindOfClass:[ClassA class]) {
//Set Property and instance variable here
ClassA *obj = [activeControllerArray objectAtIndex:i];
obj.someVariable = somevalue; //you can use in any way that you want
break;
}
}
//Pop to previous view now
You are actually refering to multiple instances of ClassA. In ClassB you create a new ClassA's object and set a value.
In classC you are creating a differnt Instance of ClassA and then checking the value set for previous instance.
You can access the value set in 'storeValue' in all instances if its declared static.
Else Instance variables belongs to instance of class and is managed seperatley by each instance.
I Think you are doing it entirely wrong this time.
Change your class as you are popping to class C:
ClassC *obj = [[ClassC alloc]init];
[obj passValue:value];
[self.navigationController popViewControllerAnimated:YES];
[obj release];
As @JS Rodrigues said, you're not pointing to exists instance of ClassA, you're just creating another instance of ClassA.
To get the existing instance of ClassA you can control on the stack of the navigationcontroller (this code is similar to @rishi code but use fast enumetarion):
for (UIViewController *obj in self.navigationController.viewControllers) {
if ([obj isKindOfClass:[ClassA class]]) {
ClassA *ca=(ClassA *)obj;
[ca passValue:value];
}
}
don't print the value in viewDidLoad
viewDidLoad fire before after allocation and before you passes the variable, because of that, your variable returns NULL
if you really do need to pass this way, set the variable in the header , then use the passing method
for instance :
you want to pass string myString from A to B, and then go to B and print the string
on A, import the B and then do the following function
self.bViewController = [[BVIEWCONTROLLER alloc]init];
[self.bViewController setmyString:myString];
//if u're running OS lower than 5.0, include this
[self.bViewController viewDidAppear];
after that you print the myString variable in the b view controller, INSIDE the viewDidAppear method.
good luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.