我可能正在做一些非常愚蠢的事情,但我似乎无法使用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
}
*/
}
答案 0 :(得分:11)
我遇到了类似的问题,我认为这部分是一个缓存问题,部分只是一个Xcode6 / Swift问题。我发现需要的第一步是确保在选择“自动”时将视图控制器.swift文件加载到Assistant Editor中。
当Xcode发现两个文件都已链接时,我有时可以控制 - 从视图/按钮/等拖动。从IB到.swift文件,但经常不得不从@IBOutlet var newView:MyView
行的阴沟中的空心圆拖到我希望它匹配的视图。
如果你无法在助理编辑器中加载文件,那么我发现执行以下操作通常会有效:
如果这似乎让你中途/无处添加评论,我会看到它是否触发了我做的其他事情
答案 1 :(得分:3)
在我的情况下import UIKit
丢失,添加此行后我可以再次从Storyboard创建一个IBOutlet。
答案 2 :(得分:0)
我遇到了与此主题中描述的问题类似的问题。也许你找到的解决方案可能不是,但是将来会遇到这种情况的人。我发现关键是使用“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;
}
}
}
}
希望这会有所帮助......