【问题标题】:perform segue from UIView从 UIView 执行 segue
【发布时间】:2017-08-20 09:46:02
【问题描述】:

我有 ViewController,里面有 UIView。

这个 UIView 有单独的 myView 类,并且有许多 UI 元素 - 其中之一是 CollectionView。

我想要的是在选择 myView 中的集合元素之一时执行 segue。但是当我尝试添加行时

performSegue(withIdentifier: "myIdintifier", sender: self)

到集合的视图 didSelectItemAt 方法我得到错误

使用未解析的标识符“performSegue”

我知道这是因为我在扩展 UIView 而不是 UIViewController 的类中执行此操作。

那么在这种情况下我该如何执行 segue 呢?还有,我该如何为 segue 做准备?

【问题讨论】:

  • 你可以使用自定义委托触发 UIViewController 中的事件,然后你可以使用 performSegue
  • 您能否提供更详细的示例作为答案?

标签: ios objective-c uiview segue uistoryboardsegue


【解决方案1】:

这里我将逐步评估它。

步骤 - 1

使用如下协议创建自定义委托 sn-p 将指导您自定义 UIView。 protocol 必须存在于您的自定义视图范围之外。

protocol CellTapped: class {
    /// Method
    func cellGotTapped(indexOfCell: Int) 
}

不要忘记在您的自定义视图中创建上述类的委托变量,如下所示

var delegate: CellTapped!

使用您的集合视图 didSelect 方法,如下所示

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if(delegate != nil) {
            self.delegate.cellGotTapped(indexOfCell: indexPath.item)
        }
    }

步骤 - 2

让我们来看看你的视图控制器。将CellTapped 提供给您的视图控制器。

class ViewController: UIViewController,CellTapped {

    @IBOutlet weak var myView: MyUIView! //Here is your custom view outlet
    override func viewDidLoad() {
        super.viewDidLoad()
        myView.delegate = self  //Assign delegate to self
    }

    // Here you will get the event while you tapped the cell. inside it you can perform your performSegue method.
    func cellGotTapped(indexOfCell: Int) {
        print("Tapped cell is \(indexOfCell)")
    }
}

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用协议/委托来实现。

     // At your CustomView
    
    protocol CustomViewProtocol {
        // protocol definition goes here
        func didClickBtn()
    }
    
    
     var delegate:CustomViewProtocol
    
    
    
    
    @IBAction func buttonClick(sender: UIButton) {
        delegate.didClickBtn() 
      }
    
    
    
    
    //At your target Controller
    public class YourViewController: UIViewController,CustomViewProtocol
    
    let customView = CustomView()
    customView.delegate = self
    
    func didClickSubmit() {
         // Perform your segue here
    }
    

    【讨论】:

      【解决方案3】:

      除了定义协议之外,您还可以使用通知。 一、extent nonfiction.name:

      extension Notification.Name {
          static let yourNotificationName = Notification.Name(“yourNotificationName”)
      }
      

      然后就在你想要执行 segue 但不能在你的自定义 UIView 中:

      NotificationCenter.default.post(name: .yourNotificationName, object: self)
      

      最后,您可以在 viewControllers 中收听通知:

      private var observer: NSObjectProtocol?
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          observer = NotificationCenter.default.addObserver(forName: .yourNotificationName, object: nil, queue: nil) {notification in
          self.performSegue(withIdentifier:”your segue”, sender: notification.object}
      

      别忘了删除它:

      override func viewWillDisappear(_ animated: Bool){
          super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(observer)
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 2017-12-06
        • 2018-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多