在我的ViewController我設置:是否可以從UIView XIB執行Segue?
self.view.addSubview(xib)
其中xib
是class myView: UIView {
當用戶點擊了那些XIB一個按鈕,我能SEGUE?因爲當我嘗試時,它說
Value of type myView has no member performSegue
解決此問題的另一種方法是什麼?
在我的ViewController我設置:是否可以從UIView XIB執行Segue?
self.view.addSubview(xib)
其中xib
是class myView: UIView {
當用戶點擊了那些XIB一個按鈕,我能SEGUE?因爲當我嘗試時,它說
Value of type myView has no member performSegue
解決此問題的另一種方法是什麼?
performSegue
是UIViewController
的一部分。 UIView
不能執行segue。嘗試從關聯中調用此方法UIViewController
是的,你可以,你可以調用一個繼續發生。
當用戶點擊該按鈕,你可以調用SEGUE發生類似以下內容:
self.storyboard(performSegueWithIdentifier("identifier", sender: self))
'使用未解析的標識符performSegueWithIdentifier'' –
而不是使用self.storyboard嘗試創建故事板的實例並使用它。讓storyboard = UIStoryboard(名稱:Main,bundle:nil);故事板(performSegueWithIdentifier(「identifier」,sender:self)) –
'let storyboard = UIStoryboard(name:「Main」,bundle:nil)'。同樣的錯誤 –
可以實現使用協議/委託。
在您UIView類有方法的協議,例如:userDidClickButton()
而在你的viewController類聽取代表回電。 當你創建你的實例UIView類分配委託給自例如: uiviewClassInstance.delegate = self
而在你的視圖控制器類實現上述委託方法和裏面調用perfromSegue
。
編輯: 您UIView類中
protocol SomeProtocol {
// protocol definition goes here
func didClickSubmit()
}
var delegate:SomeProtocol
@IBAction func buttonClick(sender: UIButton) {
delegate.didClickSubmit()
}
視圖控制器類:
public class YourViewController: UIViewController,SomeProtocol
let signUpView = SignUpView.signUpView()
singUpView.delegate = self
func didClickSubmit() {
// Perform your segue here
}
'讓signUpView = SignUpView.signUpView()'它沒有一個委託方法 –
你能把它寫在Gist中嗎? –
這個答案是正確的,你得到的原因沒有委託錯誤的方法是因爲你沒有將你的UIView類作爲一個實例鏈接到你的UIViewController - 或者你有和沒有使用實例作爲你的「instance.delegate =自我「 – Abhi
我花了幾個小時努力學習代表團雖然我沒有包裹着我的頭周圍完全。我知道代碼是關於什麼的。這裏是如何做到這一點備忘單:
目標:要執行的功能或UIView的數據傳回的UIViewController它在
例:你有一個UIViewController有在UIView中,你想要在UIView中按下一個按鈕並使UIViewController執行一個segue。
聲明:這個特定的例子是蹩腳的,因爲你可以通過故事板將UIView中的按鈕直接連接到UIViewController,但是你明白了。
要完成的例子遵循的步驟:
1.在你的UIView類,你會想要寫這個(這是發件人類):
import UIKit //written for reference on file scope
// Define protocol outside of class
protocol SomeUIViewDelegate {
func segueFunction() //this function will be in your receiving class
}
class SomeUIView: UIView {
//define the delegate as such:
var delegate: SomeUIViewDelegate?
//fake button press function I made for the tutorial
func someButtonPress(){
//this code goes wherever you want the delegate to "fire," like in this button press
delegate?.segueFunction()
}
}
2。在你的UIViewController類中,你會寫這個(這是接收器類):
import UIKit //written for reference on file scope
//state the class will conform to the delegate
class SomeUIViewController: UIViewController, SomeUIViewDelegate {
//IMPORTANT: you must instantiate the SomeUIView class!
//use storyboard to right click drag and drop a line onto assistant editor, then name the instance
@IBOutlet weak var SomeUIViewNamedInstance
override func viewDidLoad() {
super.viewDidLoad()
//set the classes delegate to itself in viewDidLoad
SomeUIViewNamedInstance.delegate = self
}
//this is the function we defined in the protocol in SomeUIView class, it executes whenever you call it from the sender class
func segueFunction() {
//perform segue or whatever else you want
//EXTRA CREDIT: You can pass data too from sender to receiver within the function parameters!!
}
}
編輯:希望將這個優秀的文章添加到這個好奇心的答案。 http://matteomanferdini.com/how-ios-view-controllers-communicate-with-each-other/
我該怎麼稱呼它?我有兩個不同的類 –
向你的類添加一個變量'viewController',它應該執行segue。在添加子視圖之前,將'viewController'設置爲您的視圖。 'xib.viewController = self; self.view.addSubview(xib);' – GRiMe2D
你能否描述一下關於'viewController'的第一部分? –