Swift中一个按钮的两个动作

时间:2018-03-19 14:31:02

标签: swift

我正在尝试激活iOS通知中心计时器,同时将用户发送到网址(实际上是一项调查)。我无法弄清楚如何从一个按钮获取这两个动作。这是我目前使用两个单独按钮的代码:

@IBAction func timer(_ sender: Any)
{
    let content = UNMutableNotificationContent()
    content.title = "Reminder"
    content.subtitle = "Click This Notice"
    content.body = "Please Repeat Daily"
    content.sound = UNNotificationSound.default()

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 86400, repeats: true)
    let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

@IBAction func survey(_ sender: Any) {
    if let url = NSURL(string: "https://yahoo.com"){ UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)}
}

我非常感谢任何帮助,因为我不是一个基本的程序员。

2 个答案:

答案 0 :(得分:1)

从故事板中,您只需按住从按钮拖动到View Controller即可。选择操作并命名action1 ..或其他。

第二次执行action2

实施您的两项行动,您就完成了。单击该按钮时,将调用这两个操作。

答案 1 :(得分:0)

这是你的按钮目标的样子:

button.addTarget(self, action: #selector(timer(_:)), for: .touchUpInside)

只需从第一个方法中调用第二个方法:

@objc func timer(_ sender: Any) {
    print("timer")
    survey(sender)
}
@objc func survey(_ sender: Any) {
    print("survey")
}

只需将按钮作为第二个方法参数的参数传递。