【问题标题】:How to set action for a button when using different storyboard for iphone and ipad?iphone和ipad使用不同的故事板时如何为按钮设置动作?
【发布时间】:2016-12-16 09:01:11
【问题描述】:

我想为 ipad 和 iphone 使用不同的故事板。我按照这个tutorial成功做到了。

但我想知道如何在 iPad 和 iPhone 中为 Button 调用相同的操作。

【问题讨论】:

    标签: iphone swift xcode ipad storyboard


    【解决方案1】:

    创建一个IBAction 并将其分配给故事板中的button,现在如果您想区分它是从iphone 还是ipad 调用,您可以在IBAction 中为其添加条件。

    @IBAction func buttonClick(sender: UIButton) {
        if (UIDevice.currentDevice().userInterfaceIdiom == .Pad) {
             // It is from iPad
        }
        else { 
             // It is from iPhone
        }   
    }
    

    【讨论】:

    • 我想为 ipad 和 iphone 做同样的功能。我真的很想知道从哪里将按钮连接到视图控制器。是来自 ipad 故事板还是 iphone 故事板?我无法从两者连接。第二个故事板是否需要单独的视图控制器?
    • 不,您需要一个 ViewController.swift 文件并将其设置为 viewcontroller 的两个文件,然后您就可以使用按钮连接操作。
    • 好的。我明白了。非常感谢。
    • 如何将按钮从第二个故事板连接到 IBAction?
    • 如果你设置了 ViewController 与你相同的类,那么你将能够连接。
    【解决方案2】:

    创建一个 IBAction 并将其分配给情节提要中的按钮,现在如果您想区分它是从 iPhone 还是 iPad 调用,您可以通过为情节提要中的按钮设置标签来在 IBAction 中添加条件。

    @IBAction func buttonClick(sender: UIButton) {
        if (sender.tag == 1) {
             // It is from iPad
        }
        else { 
             // It is from iPhone
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 2013-11-16
      • 2012-11-16
      • 2015-03-05
      • 2015-09-20
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 2014-11-22
      相关资源
      最近更新 更多