当在另一个类中调用方法时,我想更改另一个对象的属性。
更改此对象属性的代码位于第一个类的方法中,并且在从它自己的类调用它时起作用,但是当从另一个类调用时,方法中的对象返回 nil。
这是代码:
视图控制器.h
@interface ViewController : UIViewController {
UIView *menuView; //the object
}
@property (nonatomic, retain) IBOutlet UIView *menuView;
-(void)closeMenu; //the method
@end
视图控制器.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(没什么重要的,但可能有帮助?)
@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;
}
}