简体   繁体   English

在目标C中的视图控制器之间传递对象

[英]Passing Objects between View Controllers in Objective C

I have 3 Classes Class A, Class B, Class C. 我有3个班级:A级,B级,C级。

I am Passing Variable from ClassB to ClassA and then popping ClassB(View Controller) 我正在将变量从ClassB传递到ClassA,然后弹出ClassB(视图控制器)

//ClassB.m //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 //ClassA.h

NSString* storeValue;
-(void)passValue:(NSString*)getValue;

//ClassA.m //ClassA.m

-(void)passValue:(NSString*)getValue
{
   NSLog(@"Value is %@",getValue); // I am Getting Value Here
   storeValue = getValue; 
}

In ClassC. 在ClassC中。 I am Pushing ClassA View Controller 我正在推送ClassA View Controller

ClassC.m C类

ClassA *objA = [[ClassA alloc]init];
[self.navigationController pushViewController:objA animated:YES];
[objA release];

Now in Class A. Inside View Didload. 现在在A类中。内部视图Didload。 I tried to print storeValue. 我试图打印storeValue。 but it returns null. 但它返回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. 我考虑过以下类的层次结构-C-> A-> B您无需为A类创建任何对象,因为该对象已经在导航堆栈中。 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. 您实际上是在指ClassA的多个实例。 In ClassB you create a new ClassA's object and set a value. 在ClassB中,您将创建一个新的ClassA对象并设置一个值。

In classC you are creating a differnt Instance of ClassA and then checking the value set for previous instance. 在classC中,您将创建ClassA的另一个实例,然后检查为先前实例设置的值。

You can access the value set in 'storeValue' in all instances if its declared static. 如果声明为静态,则可以在所有实例中访问“ storeValue”中设置的值。

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. 我认为您这次做的完全错误。

  1. You could implement delegate methods to pass the object back before poping and then use it. 您可以实现委托方法,以在弹出对象之前将其传递回,然后再使用它。
  2. If you use static variable, it will be common for all instance of that Class. 如果使用静态变量,则该类的所有实例将是通用的。 Which may not be desirable. 这可能不是理想的。
  3. Or you could post an NSNotification. 或者,您可以发布NSNotification。 And pass the object along with it. 并将对象与之一起传递。 Listeners of those notifications could catch it and handle it differently. 这些通知的侦听器可以捕获它并以不同的方式处理它。

Change your class as you are popping to class C: 跳到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. 正如@JS Rodrigues所说,您并不是在指向ClassA的现有实例,而只是在创建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): 要获取ClassA的现有实例,可以在navigationcontroller的堆栈上进行控制(此代码类似于@rishi代码,但使用快速枚举):

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中打印值

viewDidLoad fire before after allocation and before you passes the variable, because of that, your variable returns NULL viewDidLoad在分配之后和传递变量之前触发,因此,您的变量返回NULL

if you really do need to pass this way, set the variable in the header , then use the passing method 如果确实需要通过这种方式,请在header中设置变量,然后使用通过方法

for instance : 例如 :

you want to pass string myString from A to B, and then go to B and print the string 您想要将字符串myString从A传递到B,然后转到B并打印字符串

on A, import the B and then do the following function 在A上,导入B,然后执行以下功能

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. 之后,在b视图控制器中打印myString变量,然后在viewDidAppear方法中。

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM