【问题标题】:Swift, iboutlet and custom controlsSwift、iboutlet 和自定义控件
【发布时间】:2014-06-08 09:17:38
【问题描述】:

我可能正在做一些非常愚蠢的事情,但我似乎无法使用 Interface Builder 将 IBOutlet 变量连接到自定义视图,但只能在 Swift 中使用。

我创建了一个名为 MyView 的类,它从 UIView 扩展而来。在我的控制器中,我有一个 MyView 变量(声明为 @IBOutlet var newView: MyView)。我进入 IB 并将 UIView 拖到窗口上并给它一个 MyView 类。

每当我在 Objective C 中完成类似操作时,我都可以单击应用程序窗口顶部的 View Controller 按钮,选择变量并将其向下拖动到控件以将两者链接在一起。当我在 Swift 中尝试时,它拒绝识别视图存在。

如果我将控制器中的变量类更改为 UIView,它可以正常工作。但不是我的自定义视图。

还有其他人遇到过这个问题吗?它是一个功能,还是只是我的白痴?

控制器代码

import UIKit

class ViewController: UIViewController {

    @IBOutlet var newView:MyView

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

查看代码

import UIKit

class MyView: UIView {

    init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code
    }

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect)
    {
        // Drawing code
    }
    */

}

【问题讨论】:

  • 我也遇到了这个问题,我认为这是一个错误,你应该报告它。

标签: ios xcode swift iboutlet


【解决方案1】:

我遇到过类似的问题,我认为这部分是缓存问题,部分只是 Xcode6/Swift 问题。我发现需要的第一步是确保在选择“自动”时视图控制器 .swift 文件将加载到助理编辑器中。

当 Xcode 发现这两个文件都已链接时,我有时可以从视图/按钮/等中进行控制拖动。从 IB 到 .swift 文件,但经常不得不从 @IBOutlet var newView:MyView 行的排水沟中的空圆圈拖动到我希望它匹配的视图。

如果您无法在助手编辑器中加载文件,那么我发现执行以下操作通常会起作用:

  1. 从 IB 视图中删除自定义类
  2. 清理项目(cmd + K)
  3. 关闭/重新打开 Xcode
  4. 可能再次清洁?
  5. 将自定义类添加回视图
  6. 希望它有效:)

如果这似乎让你半途而废/无处可去,请添加评论,我会看看它是否会触发我所做的其他事情

【讨论】:

  • 谢谢。这些都没有真正解决直接问题(从现有的@IBOutlet 拖动到 IB 窗口中的自定义视图),但确实提供了另一种方法 - 从 IB 窗口中的 MyView 拖动到助理编辑器,它创建(和链接)相关的 IBOutlet。这似乎达到了相同的总体需求。
  • 从排水沟的空圈中钩住就成功了!谢谢!
【解决方案2】:

在我的情况下,import UIKit 丢失了,添加此行后,我可以再次从 Storyboard 创建一个 IBOutlet。

【讨论】:

    【解决方案3】:

    我遇到了与此线程中描述的问题类似的问题。也许您找到了解决方案,也许没有,但将来遇到此问题的任何人。我发现关键是使用“required init”函数如下:

    required init(coder aDecoder: NSCoder) {
        print("DrawerView: required init")
        super.init(coder: aDecoder)!
        screenSize = UIScreen.mainScreen().bounds
        screenWidth = screenSize.width
        screenHeight = screenSize.height
        self.userInteractionEnabled = true
        addCustomGestureRecognizer()
    }
    

    这是我的自定义视图的完整类:

    导入 UIKit 导入基金会

    类 DrawerView: UIView {

    var screenSize: CGRect!
    var screenWidth: CGFloat!
    var screenHeight: CGFloat!
    
    var drawerState: Int = 0
    
    override init (frame : CGRect) {
        print("DrawerView: main init")
        super.init(frame : frame)
    }
    
    override func layoutSubviews() {
        print("DrawerView: layoutSubviews")
        super.layoutSubviews()
    }
    
    convenience init () {
        self.init(frame:CGRect.zero)
    }
    
    required init(coder aDecoder: NSCoder) {
        print("DrawerView: required init")
        super.init(coder: aDecoder)!
        screenSize = UIScreen.mainScreen().bounds
        screenWidth = screenSize.width
        screenHeight = screenSize.height
        self.userInteractionEnabled = true
        addCustomGestureRecognizer()
    }
    
    func addCustomGestureRecognizer (){
        print("DrawerView: addCustomGestureRecognizer")
        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleDrawerSwipeGesture(_:)))
        swipeDown.direction = UISwipeGestureRecognizerDirection.Down
        self.addGestureRecognizer(swipeDown)
        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleDrawerSwipeGesture(_:)))
        swipeUp.direction = UISwipeGestureRecognizerDirection.Up
        self.addGestureRecognizer(swipeUp)
        print("DrawerView self: \(self)")
    }
    
    func minimizeDrawer(){
        UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: {
            //            let height = self.bookButton.frame.size.height
            //            let newPosY = (self.screenHeight-64)*0.89
            //            print("newPosY: \(newPosY)")
            self.setY(self.screenHeight*0.86)
            }, completion: { finished in
                self.drawerState = 0
                for view in self.subviews {
                    if let _ = view as? UIButton {
                        let currentButton = view as! UIButton
                        currentButton.highlighted = false
                    } else if let _ = view as? UILabel {
                        let currentButton = view as! UILabel
                        if self.tag == 99 {
                            currentButton.text = "hisotry"
                        } else if self.tag == 999 {
                            currentButton.text = "results"
                        }
                    }
                }
        })
    }
    
    func handleDrawerSwipeGesture(gesture: UIGestureRecognizer) {
        print("handleDrawerSwipeGesture: \(self.drawerState)")
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch self.drawerState{
            case 0:
                if swipeGesture.direction == UISwipeGestureRecognizerDirection.Down {
                    // nothing to be done, mini and swiping down
                    print("mini: !")
                } else {
                    // mini and swiping up, should go to underneath city box
                    UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: {
                        let toYPos:CGFloat = 128 + 64 + 8
                        self.setY(toYPos)
                        }, completion: { finished in
                            self.drawerState = 1
                            for view in self.subviews {
                                if let _ = view as? UIButton {
                                    let currentButton = view as! UIButton
                                    currentButton.highlighted = true
                                } else if let _ = view as? UILabel {
                                    let currentLabel = view as! UILabel
                                    currentLabel.text = "close"
                                }
                            }
    
                    })
                }
                break;
            case 1:
                if swipeGesture.direction == UISwipeGestureRecognizerDirection.Down {
                    // open and swiping down
                    self.minimizeDrawer()
                } else {
                    // open and swiping up, nothing to be done
                }
                break;
            default:
                break;
            }
        }
    }
    

    }

    希望这会有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多