开发者

Set another class' property

开发者 https://www.devze.com 2023-04-04 06:39 出处:网络
I want to set an NSString property of a main view from a nested view. I do so right now by allocating the previous class and accessing the class.property. When I pop the view controller programmatical

I want to set an NSString property of a main view from a nested view. I do so right now by allocating the previous class and accessing the class.property. When I pop the view controller programmatically and NSLog the property from the main view, it's null.

How does this happen?

EDIT:

MainViewController *controller = [[MainViewController alloc] init];
switch (indexPath.row) {
    case 0:
        controller.category = @"Categorie 1";
        break;
    default:
        break;
} 
[contr开发者_JAVA百科oller release];


You mentioned you are going to pop the viewController so I assume you are trying to set a property of the controller below the navigation stack.

Instead of creating a new object of the class, you should get back the original object that was already created.

NSArray *viewControllers = [self.navigationController viewControllers]; // array of viewControllers currently on the navigation stack.
MainVC *mainVC = (mainVC *)[viewControllers objectAtIndex:viewControllers.count - 2];
[mainVC setProperty:...];


Well first of all, you cannot access a specific instance's properties by simply calling the class. The class has no connection to any specific instance of it.

You need to have an instance variable in your nested view that references the parent. set this up when you create it. Then when you are in the child view controller you can still access the parent.

Something like this in the child:

MyParentViewController *parentVC;
0

精彩评论

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