如何添加UIViewController作为以编程方式创buildUIView创build的UIButton行动的目标?

我编程创build了一个UIView并添加了一个UIButton作为它的子视图。
我想要一个UIViewController成为该button操作的目标。
我该怎么做?
如果它是由Interface Builder创build的,那么使用IBAction很容易

如果以编程方式将button添加到UIView的子类中,则可以采用以下两种方法之一:

  1. 您可以使button成为视图的属性,然后在实例化视图的viewController中,您可以按如下方式设置button的目标:

     [viewSubclass.buttonName addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

    这将button的目标设置为buttonTapped:在viewController.m中的方法

  2. 您可以在您的子视图中创build一个协议,父视图控制器将符合该协议。 在你看来,当你添加你的button的时候,将它设置为在你的视图中调用一个方法。 然后从该视图中调用委托方法,以便您的viewController可以响应它:

在视图的子类顶部.h创build协议:

 @protocol ButtonProtocolName - (void)buttonWasPressed; @end 

为委托创build一个属性:

 @property (nonatomic, assign) id <ButtonProtocolName> delegate; 

在子类.m中设置你的buttonselect器:

 [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

在buttonTapped:方法中调用委托方法:

 - (void)buttonTapped:(id)sender { [self.delegate buttonWasPressed]; } 

在你的viewController.h中,你需要确保它符合协议:

 @interface someViewController : UIViewController <SomeButtonProtocolName> 

在你的viewController.m当你启动你的子视图时,你必须设置委托:

 SomeView *view = ... // Init your view // Set the delegate view.delegate = self; 

最后,将委托方法buttonWasPressed添加到viewController.m中:

 - (void)buttonWasPressed { // Put code here for button's intended action. } 

你可以添加目标和动作button。

 [button addTarget:controller/self action:@selector(onTapButton) forControlEvents:UIControlEventTouchUpInside]; 

目标是控制器所以如果你想处理当前控制器中的触摸事件使用自己。 其他方面,你应该有一个指针/控制器obj的引用,并使用该引用,而不是自我。 onTapButton是当用户点击button时将被调用的select器。 onTapButton不接受任何参数,如果你想使用onTapButton:参数onTapButton:

 - (IBAction/void)onTapButton{ } -(IBAction/void)onTapButton:(id)sender{ } 

注意:更好的办法是处理这个是使用委托模式,有自己的目标是什么类,然后调用委托,并且控制器应该实现这个委托。 保持直接参考控制器是不好的做法。

 [buttonName addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

现在,如果你使用的是UINavigationController,你的buttonTapped函数将会是

 - (void)buttonPressed:(id)sender { NewViewController *newVC = [NewViewController new]; [self.navigationController pushViewController:newVC animated:YES]; } 

如果你不使用导航控制器比

 - (void)buttonPressed:(id)sender { NewViewController *newVC = [NewViewController new]; [self presentViewController:newVC animated:YES completion:nil]; } 

我们也可以在没有代表的情况下做到这一点。

在您的CustomUIView.m类中,实现以下方法:

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; NSArray *viewsInNib = [[NSBundle mainBundle]loadNibNamed:@"AddressAlertView" owner:self options:nil]; for (id view in viewsInNib) { if ([view isKindOfClass:[self class]]) { self = view; break; } } return self; } 

说明: – 在initWithFrame方法中,我正在加载当前的笔尖。 加载笔尖意味着初始化它包含的所有子视图。 获得完全初始化的相同类的视图,并分配给自己。 返回包含完全初始化的uiview的自我。

在您的viewcontroller.m文件中,编写下面的代码来添加自定义uiview并设置button的目标: –

 UIWindow *mainWindow = [[[UIApplication sharedApplication]delegate]window]; AddressAlertView *objAddressAlertView = [[AddressAlertView alloc] initWithFrame:mainWindow.bounds]; [objAddressAlertView.btnCross addTarget:self action:@selector(dummy) forControlEvents:UIControlEventTouchUpInside]; if (objAddressAlertView) [mainWindow addSubview:objAddressAlertView];