我得到了一個自定義組件,(帶有自己的控制器和它自己的xib),它有一個按鈕。我使用this教程做了它。如何從ScrollView中的自定義組件中使用prepareForSegue?
在主ViewController中,我有四個這樣的組件,在ScrollView中。我希望應用程序對點擊按鈕做出反應來準備賽季。
我不能從我的自定義組件控制器,因爲它的UIView,並不能從自定義組件連接到主ViewController,因爲它不是一個直接的兄弟姐妹,有一個ScrollView。所以,我無法從Main.storyboard的視圖控制器中的按鈕創建一個IBAction。
我的目標是,從自定義組件中,在Main.storyboard的Controller中創建一個IBAction,這樣我就可以啓動一個segue並呈現一個場景或另一個場景,具體取決於按下哪個按鈕的用戶。
我怎麼能做到這一點?謝謝。
我的自定義組件控制器:
import UIKit
@IBDesignable class CustomView: UIView {
// Our custom view from the XIB file
var view: UIView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var icono: UIImageView!
@IBAction func onClick(sender: UIButton) {
println("button \(label.text!)")
switch label.text!{
case "Text 1":
println("First component")
case "Text 2":
println("Second component")
case "Text 3":
println("Third component")
case "Text 4":
println("Fourth component")
default:
println("Error")
}
}
@IBInspectable var image: UIImage? {
get {
return icono.image
}
set(image) {
icono.image = image
}
}
@IBInspectable var text: String? {
get {
return label.text
}
set (text){
label.text=text
}
}
func xibSetup() {
view = loadViewFromNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
//self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0)
self.view.backgroundColor=UIColor.clearColor()
// Adding custom subview on top of our view (over any custom drawing > see note below)
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CustomView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
override init(frame: CGRect) {
// 1. setup any properties here
// 2. call super.init(frame:)
super.init(frame: frame)
// 3. Setup view from .xib file
xibSetup()
}
required init(coder aDecoder: NSCoder) {
// 1. setup any properties here
// 2. call super.init(coder:)
super.init(coder: aDecoder)
// 3. Setup view from .xib file
xibSetup()
}
}
我Main.storyboard的視圖控制器:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var customViewVirtual: CustomView!
@IBOutlet weak var customViewContactar: CustomView!
@IBOutlet weak var customViewPresenciales: CustomView!
@IBOutlet weak var customViewCalendario: CustomView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "fondo")!)
customViewVirtual.backgroundColor=UIColor.clearColor()
customViewContactar.backgroundColor=UIColor.clearColor()
customViewPresenciales.backgroundColor=UIColor.clearColor()
customViewCalendario.backgroundColor=UIColor.clearColor()
// 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.
}
}
您的自定義組件控制器如何是一個UIView? UIViewController在哪裏拿着你的UIView? –
我做了一個xib,並製作了一個ViewController來控制它。然後,在Main.storyboard中,我添加了四個視圖,使該視圖的類成爲我的自定義組件的控制器,這是UIView的一個實例。我添加代碼的問題。 – Fustigador