Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以使用swift在UIViewController和UIView之间传递数据?_Ios_Swift_Uiview_Uiviewcontroller_Message Passing - Fatal编程技术网

Ios 是否可以使用swift在UIViewController和UIView之间传递数据?

Ios 是否可以使用swift在UIViewController和UIView之间传递数据?,ios,swift,uiview,uiviewcontroller,message-passing,Ios,Swift,Uiview,Uiviewcontroller,Message Passing,是否可以在UIViewController和UIView之间传递数据? 如果是,如何进行 例如,当您按下按钮1时,UIViewController会对UIView说用户按下了按钮1,然后UIView会绘制一个三角形。但是如果你按下按钮2,UIViewController会告诉UIView他现在按下了按钮2,然后UIView会消除三角形并画一个圆 按钮是以编程方式生成的 问题是如何在UIViewController的UIView中调用使用drawRect绘制的形状 class ViewContro

是否可以在UIViewController和UIView之间传递数据? 如果是,如何进行

例如,当您按下按钮1时,UIViewController会对UIView说用户按下了按钮1,然后UIView会绘制一个三角形。但是如果你按下按钮2,UIViewController会告诉UIView他现在按下了按钮2,然后UIView会消除三角形并画一个圆

按钮是以编程方式生成的

问题是如何在UIViewController的UIView中调用使用drawRect绘制的形状

class ViewController: UIViewController { 
   @IBOutlet var UnitViewTabeller: UnitView!
   var btn: UIButton!

   override viewDidLoad {
     self.btn = UIButton(frame: CGRectMake(0.04 * view.bounds.width, 0.91 * view.bounds.height, 0.44 * view.bounds.width, 0.07 * view.bounds.height))  //set frame
            self.btn.layer.cornerRadius = 10
            self.btn.setTitle("0", forState: .Normal)  //set button title
            self.btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
            self.btn.backgroundColor = UIColor(red: 0.2745, green: 0.2784, blue: 0.2706, alpha: 1.0) //set button background color
            self.btn.tag = 0 // set button tag
            self.btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
            self.view.addSubview(btn) //add button in view
            self.btnArray.append(btw)
   }

   func btnclicked(sender: UIButton) {
       //draw the circle from the UIView
   }


}    
    class UnitView: UIView {

    override func drawRect(rect: CGRect) {
        let cirkel = UIGraphicsGetCurrentContext()
        let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
        CGContextSetLineWidth(cirkel, 4.0)
        CGContextAddEllipseInRect(cirkel, rectangle)
        CGContextStrokePath(cirkel)
    }
}
UIViewController具有UIView类型的视图属性

您可以使用self.view从UIViewController类内部进行访问

从类内部访问它:

class MyViewController: UIViewController {
  var buttonLabel:String
  var buttonRadius:CGFloat = 90
  var button = UIButton.buttonWithType(UIButtonType.System) as UIButton

  init(buttonColour:CGColorRef, buttonLabel:String){
    self.buttonColour = buttonColour
    self.buttonLabel = buttonLabel
  }

  func drawButton() {
    button.frame = 100, 100, buttonRadius * 2, buttonRadius * 2)
    button.layer.borderColor = buttonColour
    button.setTitle(buttonLabel, forState: UIControlState.Normal)
    button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button) 
  }
}
欢迎来到SO

这是一个非常基本的初学者问题

当然有可能。您可能应该创建UIView的自定义子类。我们称之为ShapeView。为它提供绘制各种形状的方法,并为它提供存储其当前状态的属性

现在,您可以将UIView添加到视图控制器,并进入视图控制器场景的标识检查器,将该视图的类型更改为新的ShapeView类。将插座添加到视图控制器中的形状视图中

class ViewController: UIViewController { 
   @IBOutlet var UnitViewTabeller: UnitView!
   var btn: UIButton!

   override viewDidLoad {
     self.btn = UIButton(frame: CGRectMake(0.04 * view.bounds.width, 0.91 * view.bounds.height, 0.44 * view.bounds.width, 0.07 * view.bounds.height))  //set frame
            self.btn.layer.cornerRadius = 10
            self.btn.setTitle("0", forState: .Normal)  //set button title
            self.btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
            self.btn.backgroundColor = UIColor(red: 0.2745, green: 0.2784, blue: 0.2706, alpha: 1.0) //set button background color
            self.btn.tag = 0 // set button tag
            self.btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
            self.view.addSubview(btn) //add button in view
            self.btnArray.append(btw)
   }

   func btnclicked(sender: UIButton) {
       //draw the circle from the UIView
   }


}    
    class UnitView: UIView {

    override func drawRect(rect: CGRect) {
        let cirkel = UIGraphicsGetCurrentContext()
        let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
        CGContextSetLineWidth(cirkel, 4.0)
        CGContextAddEllipseInRect(cirkel, rectangle)
        CGContextStrokePath(cirkel)
    }
}

由于形状视图是UIView的自定义子类,具有自定义方法和属性,因此视图控制器可以根据需要调用这些方法并设置这些属性。

是的,这是可能的。然后UIView绘制一个三角形。。。如果你愿意,你可以让UIView绘制东西,但是使用CAShapeLayer是在视图中包含形状的更简单的方法。我投反对票的原因。。。。请研究缩写词MVC。模型视图控制器。可能只需要一点时间就可以将这一点输入谷歌,但在对回报进行了一个小时的研究之后?你将为自己的职业生涯迈出几步。我这样说完全尊重你已经知道的。但也要承认你似乎还没有意识到的事情。祝你好运如果您了解MVC,您应该已经知道,不仅仅是在控制器和视图之间传递数据是它的工作内容,还有一些事情是它不涉及的。这个问题太广泛了。您是在问如何添加按钮吗?您是通过编程方式还是通过Interface Builder来执行此操作?您是否在询问如何将该按钮链接到某个方法,例如@iAction?你是在问如何让动作呈现三角形或圆形吗?总之,你真的应该编辑这个问题,把它缩小到更具体的范围,澄清你到底在问什么,并向我们展示你在这个问题上已经做了哪些研究。请参考关于堆栈溢出的帮助中心。是的,在做一些研究时,你必须加一些润滑脂。我想说,几乎所有iOS初学者的书/参考书都会告诉你如何做到这一点。下面是一些文章,可能会对您有所帮助:,还有一些关于RayWenderlich的文章:这个答案与问题无关。问题是关于从UIViewController内部访问UIView。我遗漏了什么?您遗漏了一个问题,该问题是关于向自定义视图发送消息,告诉它根据点击的按钮绘制不同的形状。问题是是否可以在UIViewController和UIView之间传递数据?如果是,怎么做?这是第一行。阅读整个第二段。