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;
}
}
EDIT 2:
Well, you shipped your code over to me, so now I can no longer say that I don’t have enough information to solve your problem.
Let’s see.
Now I see that your ViewController is the rootViewController of your app, like so:
Good, now how does the ViewController relate to your SDNestedTableViewController?
You have this in your ViewController’s viewDidLoad:
Alright, so it looks like SDMenuViewController is the child of ViewController. Now, you have a method in SDMenuViewController called item:subItemDidChange:
So, you want the reference back to the existing ViewController object, right? Because right there you’re making another one. So, you can do this:
That gets you a reference to SDMenuViewController’s parent, which is the instance of ViewController. This property is set when you do your
addChildViewController:call.Okay, this is confusing though:
In your post, you say that your
item:subItemDidChange:method is in SDNestedTableViewController, but in the code you sent me it’s in the SDMenuViewController.In the SDNestedTableViewController, I found this method:
So it looks like you’re not using the same code as in the original post, but close enough, whatever.
Now, if you want to get a reference to the ViewController instance from anywhere in the app, not just your SDMenuViewController (which happens to be the child of the ViewController instance) you should use @Mathew Varghese’s answer.
Here’s a restatement of this method:
+ (AppDelegate *)instance;to your AppDelegate.h file.Like so:
Then, in whatever object you want that reference, you
#import AppDelegate.hand sayViewController *vc = AppDelegate.instance.firstViewController;Anyway, it’s just another way of saying what Mathew mentioned earlier.