如何在 SWIFT 中从 UIView 实例化 ViewController [英] how to instantiate ViewController from UIView in SWIFT

查看:19
本文介绍了如何在 SWIFT 中从 UIView 实例化 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 UiView 初始化视图控制器.但是我在 self.presentViewController(vc, animation: true, completion: nil) 线上遇到错误.单击表格行后,我试图显示另一个视图控制器.我已经初始化了故事板.

I am trying to initialize view controller from my UiView. But i am getting error on the line self.presentViewController(vc, animated: true, completion: nil) . I am trying to show another view controller after click on a table row. i have already initialized storyboard.

import UIKit

class CustomSwipeOut: UIView , UITableViewDataSource , UITableViewDelegate {

    var label: UILabel = UILabel()
     var myNames = ["item1","item2","item3"]

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addCustomView()   
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addCustomView()
    {
       //add blank subview to the view
        var blankView : UIView = UIView(frame: CGRectMake(0, 0, 300, 100))
        blankView.backgroundColor = UIColor.greenColor()
        self.addSubview(blankView)   

        //creating a tableview programmatically
        var tblView : UITableView = UITableView()
        tblView.frame = CGRectMake(0, 100, 300, 200)
        self.addSubview(tblView)
        tblView.delegate = self
        tblView.dataSource = self
        tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")

    }

    //pragma mark- table view data source methods
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell
        cell.textLabel?.text = self.myNames[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.myNames.count
    }

    //pragma mark - table view delegate methods

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        switch indexPath.row {
        case 0:
            println("index o clicked")
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("MoneySum") as UIViewController
            self.presentViewController(vc, animated: true, completion: nil)
        case 1:
            println("index 1 clicked")
        case 2:
            println("index 2 clicked")
        default:
            println("no index")
        }
    }
}

推荐答案

但是..我完成了使用委托将 id 从 UIView 传递到 UIViewController 并检查该 id 我从 UIViewController 类实例化了 ViewController 正如 Para 在他的回答中所说.我是这样做的

However..i accomplised using a delegate to pass an id from UIView to UIViewController and checking to the id i instantiated ViewController From UIViewController class as Para said in his answer . I did it as below

步骤:

1) 首先创建一个协议

2) 创建一个符合协议的变量委托

3)然后创建一个回调方法.

//Step1:    
        protocol SendIndexDelegate{            
            func sendIndex(Int);            
        }

        class CustomSwipeView: UIView , UITableViewDataSource , UITableViewDelegate {      

            var delegate : SendIndexDelegate? //Step2           
            override init(frame: CGRect) {                
                super.init(frame: frame)
                self.addCustomView()                
            }
             ...... 

     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {            
            var row = indexPath.row            
            //for optional binding            
            if let temp = self.delegate {                
                delegate?.sendIndex(row) //Step 3               
            }else{                
                println("optional value contains nill value")                
            }      
        }
    }

另一堂课的步骤:

4) 符合协议SendIndexDelegate(所以方法sendIndex(Int)必须由这个类实现)

5) 在可选变量委托中的变量委托中分配值 self(它说我将充当类 CustomSwipeView 的委托并实现方法 sendIndex(Int))

6) 现在实现该方法并为其添加主体(因为它已经被委托所以必须通过回调方法处理上述类的动作)

    class RootViewController: UIViewController,SendIndexDelegate //Step4 {


            let rect: CGRect = CGRect (x: self.view.frame.size.width, y :10 , width: self.view.frame.size.width-50, height: self.view.frame.size.height-10)
            var a = CustomSwipeView(frame : rect)
            a.delegate = self//step5
            self.myView = a
            self.view.addSubview(self.myView)                
        }
   // Step 6:        
     func sendIndex(row : Int){

            switch row {

            case 0:                

                    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                    let moneySummaryVC: MoneySummaryVC = storyboard.instantiateViewControllerWithIdentifier("moneyVC") as! MoneySummaryVC
                    self.navigationController?.pushViewController(moneySummaryVC, animated: true)                 

            default:
                println("no index")                

            }      

        }

这篇关于如何在 SWIFT 中从 UIView 实例化 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆