Ios Swift xcode错误:不允许加载控制器,并且自动调整大小不等于视图窗口
我试图创建的内容的屏幕截图: 注意:如果您决定运行该项目,则无需查看下面的代码,因为我在下面发布的所有类都在dropbox压缩的项目文件中 底部的每个方块选择不同的颜色,在绿色方块的右边有一个不可见的方块选择形状的类型。在用户选择其中一个形状后,用户将能够在屏幕的特定部分进行绘制 整个项目: 如果运行项目,创建帐户,然后单击“登录”,然后单击其中一行,然后单击其中一个彩色框(红色和绿色方块),应用程序将崩溃并收到以下错误消息:Ios Swift xcode错误:不允许加载控制器,并且自动调整大小不等于视图窗口,ios,xcode,swift,Ios,Xcode,Swift,我试图创建的内容的屏幕截图: 注意:如果您决定运行该项目,则无需查看下面的代码,因为我在下面发布的所有类都在dropbox压缩的项目文件中 底部的每个方块选择不同的颜色,在绿色方块的右边有一个不可见的方块选择形状的类型。在用户选择其中一个形状后,用户将能够在屏幕的特定部分进行绘制 整个项目: 如果运行项目,创建帐户,然后单击“登录”,然后单击其中一行,然后单击其中一个彩色框(红色和绿色方块),应用程序将崩溃并收到以下错误消息: 2016-03-11 17:06:43.580 finalProje
2016-03-11 17:06:43.580 finalProject2[11487:1058358] <UIView: 0x7faba1677710; frame = (0 0; 414 736); autoresize = W+H; gestureRecognizers = <NSArray: 0x7faba1658d90>; layer = <CALayer: 0x7faba16563f0>>'s window is not equal to <finalProject2.RowTableViewController: 0x7faba165a3c0>'s view's window!
File slot 1
File slot 2
File slot 3
File slot 4
File slot 5
File slot 6
File slot 7
File slot 8
File slot 9
File slot 10
File slot 11
File slot 12
2016-03-11 17:06:44.746 finalProject2[11487:1058358] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7faba16b7d60>)
ShapeButton类:
import UIKit
class ShapeButton: UIButton {
let shapes: [ShapeType] = [ .Line, .Ellipse, .Rectangle, .FilledEllipse, .FilledRectangle, .Scribble ]
var shape: ShapeType = .Line {
didSet {
setNeedsDisplay()
}
}
var color: UIColor = UIColor.blueColor() {
didSet {
setNeedsDisplay()
}
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextSetLineWidth(context, 2)
let x1: CGFloat = 5
let y1: CGFloat = 5
let x2: CGFloat = frame.width - 5
let y2: CGFloat = frame.height - 5
let rect = CGRect(x: x1, y: y1 + 5, width: frame.width - 10, height: frame.height - 20)
switch shape {
case .Line:
CGContextMoveToPoint(context, x1, y1)
CGContextAddLineToPoint(context, x2, y2)
CGContextStrokePath(context)
case .Ellipse:
CGContextStrokeEllipseInRect(context, rect)
case .Rectangle:
CGContextStrokeRect(context, rect)
case .FilledEllipse:
CGContextFillEllipseInRect(context, rect)
case .FilledRectangle:
CGContextFillRect(context, rect)
case .Scribble:
CGContextMoveToPoint(context, x1, y1)
CGContextAddCurveToPoint(context,
x1 + 80, y1 - 10, // the 1st control point
x2 - 80, y2 + 10, // the 2nd control point
x2, y2) // the end point
CGContextStrokePath(context)
}
}
}
画布视图类:
import UIKit
/*
This program is for Xcode 6.3 and Swift 1.2
*/
class CanvasView: UIView {
var shape: ShapeType = .Line
var color: UIColor = UIColor.blueColor()
var first :CGPoint = CGPointZero
var last :CGPoint = CGPointZero
var points: [CGPoint] = []
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetFillColorWithColor(context, color.CGColor)
let rect = CGRect(x: first.x, y: first.y, width: last.x - first.x, height: last.y - first.y)
switch shape {
case .Line:
CGContextMoveToPoint(context, first.x, first.y)
CGContextAddLineToPoint(context, last.x, last.y)
CGContextStrokePath(context)
case .Ellipse:
CGContextStrokeEllipseInRect(context, rect)
case .Rectangle:
CGContextStrokeRect(context, rect)
case .FilledEllipse:
CGContextFillEllipseInRect(context, rect)
case .FilledRectangle:
CGContextFillRect(context, rect)
case .Scribble:
CGContextMoveToPoint(context, first.x, first.y)
for p in points {
CGContextAddLineToPoint(context, p.x, p.y)
}
CGContextStrokePath(context)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
first = touch.locationInView(self)
last = first
points.removeAll(keepCapacity: true)
if shape == .Scribble {
points.append(first)
}
setNeedsDisplay()
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}
}
导入UIKit
/*
该程序适用于Xcode 6.3和Swift 1.2
*/
类画布视图:UIView{
变量shape:ShapeType=.Line
var color:UIColor=UIColor.blueColor()
变量优先:CGPoint=CGPointZero
最后一个变量:CGPoint=CGPointZero
变量点:[CGPoint]=[]
//仅覆盖drawRect:如果执行自定义绘图。
//空实现会对动画期间的性能产生不利影响。
重写func drawRect(rect:CGRect){
//绘图代码
let context=UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(上下文,color.CGColor)
CGContextSetFillColorWithColor(上下文,color.CGColor)
设rect=CGRect(x:first.x,y:first.y,宽度:last.x-first.x,高度:last.y-first.y)
开关形状{
案例行:
CGContextMoveToPoint(上下文,first.x,first.y)
CGContextAddLineToPoint(上下文,last.x,last.y)
CGContextStrokePath(上下文)
案例.椭圆:
CGContextStrokeEllipseInRect(上下文,rect)
长方形:
CGContextStrokeRect(上下文,rect)
案例。填充椭圆:
CGContextFilllellipseInRect(上下文,rect)
大小写。填充反射角:
CGContextFillRect(上下文,rect)
案例。涂鸦:
CGContextMoveToPoint(上下文,first.x,first.y)
对于p点{
CGContextAddLineToPoint(上下文,p.x,p.y)
}
CGContextStrokePath(上下文)
}
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
第一个=触摸。位置查看(自)
最后一个=第一个
points.removeAll(keepCapacity:true)
如果形状==.Scribble{
points.append(第一个)
}
setNeedsDisplay()
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果让触摸=先触摸{
最后一次=触摸。位置查看(自)
如果形状==.Scribble{
points.append(最后一个)
}
setNeedsDisplay()
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
最后一次=触摸。位置查看(自)
如果形状==.Scribble{
points.append(最后一个)
}
setNeedsDisplay()
}
}
覆盖功能触摸已取消(触摸:设置?,带事件:UIEvent?){
}
}
在情节提要中,转到MainProjectScene
并右键单击CanvasView
您将有两个指向CanvasView
拆下两个参考插座。现在再次选择CanvasView
并转到identity inspector,确保视图的类别为CanvasView
保留主情节提要和MainProjectScene
,并将CanvasView
链接到情节提要中的视图,如下所示
这解决了问题
我会告诉你一些关于你刚刚面临的问题的见解。您可能首先在代码中创建了一个视图出口,并将其链接,然后将其删除。然后,您必须再次创建并链接它。完成此操作后,视图中将有2个参考插座。您需要删除刚刚删除的代码,否则代码将崩溃。请编辑您的问题,以显示正在崩溃的行和相关的代码。它工作得非常好!非常感谢你。你是最棒的。
import UIKit
/*
This program is for Xcode 6.3 and Swift 1.2
*/
class CanvasView: UIView {
var shape: ShapeType = .Line
var color: UIColor = UIColor.blueColor()
var first :CGPoint = CGPointZero
var last :CGPoint = CGPointZero
var points: [CGPoint] = []
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetFillColorWithColor(context, color.CGColor)
let rect = CGRect(x: first.x, y: first.y, width: last.x - first.x, height: last.y - first.y)
switch shape {
case .Line:
CGContextMoveToPoint(context, first.x, first.y)
CGContextAddLineToPoint(context, last.x, last.y)
CGContextStrokePath(context)
case .Ellipse:
CGContextStrokeEllipseInRect(context, rect)
case .Rectangle:
CGContextStrokeRect(context, rect)
case .FilledEllipse:
CGContextFillEllipseInRect(context, rect)
case .FilledRectangle:
CGContextFillRect(context, rect)
case .Scribble:
CGContextMoveToPoint(context, first.x, first.y)
for p in points {
CGContextAddLineToPoint(context, p.x, p.y)
}
CGContextStrokePath(context)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
first = touch.locationInView(self)
last = first
points.removeAll(keepCapacity: true)
if shape == .Scribble {
points.append(first)
}
setNeedsDisplay()
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
last = touch.locationInView(self)
if shape == .Scribble {
points.append(last)
}
setNeedsDisplay()
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
}
}