2013-04-29 62 views
0

我有一个UIViewController,我想要显示一个UIView呈现为一个菜单。这个菜单上会有几个按钮。我想在我的应用程序的几个不同的地方重复使用这个菜单,所以我想我会创建一个名为ViewFactory的类,该类有一个返回带有这些按钮的UIView的方法。UIButtons无法以编程方式创建UIView

在我的ViewController我打电话给这个方法,并得到返回UIView并将其作为子视图添加它。

这工作得很好。我可以看到视图及其所有按钮,但是,按钮不响应任何触摸事件。不知道为什么这是这种情况,并好奇知道我做错了什么。

这里是我的ViewFactoryClass代码:

- (UIView *) addCloseRow 
{ 
    // UIView container for everything else. 
    UIView *navRow = [[UIView alloc] initWithFrame:CGRectMake(0,225,350,45)]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.userInteractionEnabled = YES; 

    [navRow addSubview:button]; 

    [button addTarget:self action:@selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown]; 
    navRow.userInteractionEnabled = YES; 
    return navRow; 
} 

在我的主NavigationController类这里是我如何打电话,并得到UIView

ViewFactory *factory = [[ViewFactory alloc] init]; 
[self.navigationController.navigationBar addSubview:[factory MainNavigationUIView]]; 

同样,UIView显示出来,但按钮从不响应任何事情。

回答

2

您添加的目标和选择的按钮ViewFactoryClass

现在你正在创建实例,并试图从ViewFactory类调​​用一个动作。

您可以将方法更改为类似这样:

- (UIView *) addCloseRow : (id)object { 
    ... 
    [button addTarget:[object class] action:@selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown]; 
    ... 
} 
+0

问题解决了Anoop!当更新我的方法传入对象时,我可以点击按钮。更新方法后,我回到我的NavigationViewController并传递类如下:'[self.navigationController.navigationBar addSubview:[工厂MainNavigationUIView:self]];'' – Flea 2013-04-29 18:17:17

相关问题