开发者

Calling a View's method from its own View Controller, Not Working

I have a View Controller calling a method in its own view called closeMenu. closeMenu as seen below remove's a UIImageView called menu from the superView and sets it equal to nil. The method works when called from within the view 开发者_开发知识库but not when called from the View Controller

When calling the method, the view controller sees the UIImageView 'menu' as nil even though it is exists.

Any ideas on how to give the view controller the ability to remove menu from the superview and set it equal to nil?

View Controller:

     loadview {
        View *mainView = [[View alloc] initWithFrame:CGRectMake(0,0,320,480)];
        self.view = mainView;
        [mainView release];
     }

//Call closeMenu in View
[(View *)self.view closeMenu];

View:

menu = [[UIImageView alloc] initWithImage:image];
[self addSubview:menu];

-(void)closeMenu {
     NSLog(@"%@", menu);    //Displays: (null), only when called by controller
     if( menu != nil) {
           [menu removeFromSuperview];
           self.menu = nil;
     }
 }

When I create a button instance in the view with an action directed at the closeMenu method it works just fine.


Is menu an outlet? If so, is it hooked up in IB? Have you loaded the nib where it's hooked up?

If not, where are you assigning to it? Is the controller giving the view its menu? If that's the case, has that happened yet? If the view creates or loads its own menu view, has that happened yet, if at all?


Be assured that 'where' you call -closeMenu from makes no difference whatsoever. If your log statement prints null then your menu variable is null. This doesn't mean you haven't got an 'open' instance - just that your menu variable doesn't point to it.

You would need to post some more code for anyone to work out why this is happening as you have an error elsewhere in your code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜