Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object is nil when called from another class

I want to change properties of another object, when a method is called in another class.

The code to change the properties of this object sits in a method of the first class, and works when calling it from it's own class, but when called from the other class the object in the method returns nil.

Here is the code:

ViewController.h

@interface ViewController : UIViewController {

    UIView *menuView; //the object

}

@property (nonatomic, retain) IBOutlet UIView *menuView;

-(void)closeMenu; //the method

@end

ViewController.m

@implementation ViewController

@synthesize menuView;

-(void)closeMenu{

    [menuView setFrame:CGRectMake(menuView.frame.origin.x, -menuView.frame.size.height, menuView.frame.size.width, menuView.frame.size.height)];

    NSLog(@"%f", menuView.frame.size.height); //returns height when method is called from it's own class. But returns 0 (nil) when called from the other class.

}

SDNestedTableViewController.h (nothing too important, but might help?)

@interface SDMenuViewController : SDNestedTableViewController{


}

SDNestedTableViewController.m

#import "SDMenuViewController.h"
#import "ViewController.h"

- (void) item:(SDGroupCell *)item subItemDidChange:(SDSelectableCell *)subItem
{

    ViewController *firstViewController = [[[ViewController alloc] init] autorelease];

    SelectableCellState state = subItem.selectableCellState;
    NSIndexPath *indexPath = [item.subTable indexPathForCell:subItem];
    switch (state) {
        case Checked:
            NSLog(@"Changed Sub Item at indexPath:%@ to state \"Checked\"", indexPath);


            [firstViewController closeMenu]; //called from other class


            break;
        case Unchecked:
            NSLog(@"Changed Sub Item at indexPath:%@ to state \"Unchecked\"", indexPath);
            break;
        default:
            break;
    }
}
like image 228
Neil Avatar asked Mar 07 '26 06:03

Neil


1 Answers

What you posted looks like:

-(void)closeMenu{
    // menuView is never initialized, == nil
    [nil setFrame:CGRectMake(0, -0, 0, 0)];

    NSLog(@"%f", 0); //returns height when method is called from it's own class. But returns 0 (nil) when called from the other class.

}

So you are doing NSLog(@"%f", 0);.

If you do load the view by accessing the view property, the menuView will be initialized by IB rules. For the details of viewController view loading/unloading see the reference docs.

like image 174
A-Live Avatar answered Mar 08 '26 18:03

A-Live