简体   繁体   English

从自己的View Controller调用View的方法,不起作用

[英]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的视图中调用方法。 closeMenu as seen below remove's a UIImageView called menu from the superView and sets it equal to nil. 如下所示,closeMenu从superView中删除一个名为menu的UIImageView并将其设置为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. 当调用该方法时,视图控制器将UIImageView'menu'视为nil,即使它存在。

Any ideas on how to give the view controller the ability to remove menu from the superview and set it equal to nil? 关于如何使视图控制器能够从超级视图中删除菜单并将其设置为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. 当我在视图中创建一个针对closeMenu方法的操作的按钮实例时,它的效果很好。

Be assured that 'where' you call -closeMenu from makes no difference whatsoever. 确保从哪里调用-closeMenu都没有任何区别。 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. 您可能需要发布更多代码给任何人,以弄清楚为什么会发生这种情况,因为您的代码其他地方有错误。

Is menu an outlet? menu是插座吗? If so, is it hooked up in IB? 如果是这样,它是否与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? 如果该视图创建或加载了自己的菜单视图, 那么这是否发生了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM