简体   繁体   English

按钮单击事件(UIButton TouchUpInside操作)不起作用-iOS / Swift

[英]Button click event (UIButton TouchUpInside action) not working - IOS/Swift

In my application's storyboard there is a UIViewController(child) which loads inside another UIViewController(parent). 在我的应用程序的情节提要中,有一个UIViewController(child)加载到另一个UIViewController(parent)中。

This is the code I used inside the parent view controller to load the child view controller over all the subviews in parent view controller including navigation bar. 这是我在父视图控制器内部用于在子视图控制器(包括导航栏)的所有子视图上加载子视图控制器的代码。

let window: UIView? = UIApplication.sharedApplication().keyWindow;

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("CPBackConfirmMessageUIView");

window?.addSubview(viewController.view); 

This is how they look in my storyboard 这就是他们在我的故事板上的样子

在此处输入图片说明

This is how it looks after child view controller is loaded in side parent view 这是将子视图控制器加载到侧面父视图中后的样子

在此处输入图片说明

Inside the child view controller there are two buttons as you can see.I have added (TouchUpInside) actions to those buttons inside child view's controller class and it is not working. 如您所见,在子视图控制器中有两个按钮。我在子视图控制器类中的那些按钮上添加了(TouchUpInside)操作,但它不起作用。 But ViewDidLoad methods is working fine(I added some animations and they are working). 但是ViewDidLoad方法可以正常工作(我添加了一些动画,它们可以正常工作)。

Can someone tell me how can I write actions to those buttons and get them work or let me know if I am doing any thing wrong here. 有人可以告诉我如何为这些按钮编写动作并使它们起作用,或者让我知道我在这里做错了什么。 Any help would be highly appreciated. 任何帮助将不胜感激。

Edit: This is how I wrote my IBAction 编辑:这就是我写我的IBAction的方式

@IBAction func abc(sender: AnyObject) {    
    print("Worked");
}

Try to use child viewController as a property. 尝试将子viewController用作属性。 In current implementation viewController object should be released as soon as method completes. 在当前实现中,方法完成后应立即释放viewController对象。 Property will make sure that viewController object will be alive when you press a button. 属性将确保当您按下按钮时viewController对象将保持活动状态。

So instead of: 所以代替:

let viewController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("CPBackConfirmMessageUIView");

Try this: 尝试这个:

class YourParentViewController: UIViewController {

    var viewController: UIViewController!

 func someMethod() {
    let window: UIView? = UIApplication.sharedApplication().keyWindow;

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
    viewController = storyboard.instantiateViewControllerWithIdentifier("CPBackConfirmMessageUIView");

    window?.addSubview(viewController.view); 
 }

}

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

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