how to instantiate ViewController from UIView in S

2019-01-29 10:43发布

问题:

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")
        }
    }
}

回答1:

presentViewController:animated:completion: is a method defined only for UIViewController, not UIView (see Apple doc here).

Your UIView is probably in the view hierarchy managed by a UIViewController.
A solution would be using a reference to that parent UIViewController and invoking presentViewController:animated:completion: on it.



回答2:

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

Steps:

1) First create a protocol

2) create a Variable delegate conforming to protocol

3)Then create a call back method.

//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")                
            }      
        }
    }

Steps Continued for Another Class:

4) Conform to the protocol SendIndexDelegate(so method sendIndex(Int) must be impelemented by this class)

5) Assign the value self in variable delegate in the optional variable delegate(it says that i will act as delegate for Class CustomSwipeView and implement the method sendIndex(Int))

6) Now implement the method and add body to it(because it has been delegate so must handle actions of the above class through call back method)

    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")                

            }      

        }