Gunaseelan
Gunaseelan

Reputation: 15515

Using container view in ios with swift

I just want to reuse the ProgressView and 3 UILables from following image. I will reuse these in many Views. That's why I am trying to create it as a Container.

enter image description here

I combined these as a container and try the following code.

class ViewController: UIViewController {

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

    internal var progressViewController : ProgressViewController ;

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }    
}

and ProgressViewController is

class ProgressViewController: UIViewController
{

    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var lblVLow: UILabel!
    @IBOutlet weak var lblMedium: UILabel!
    @IBOutlet weak var lblVHigh: UILabel!

    override func viewDidLoad() {

    }
}

And while trying to run this code, I am getting following error,

fatal error: init(coder:) has not been implemented,

I don't know this is correct way or not. But I want that box as a reusable. Any idea or help will be highly appreciable.

Upvotes: 0

Views: 511

Answers (2)

Gasim
Gasim

Reputation: 7961

If you need to pass parameters to ProgressViewController, you should do it through prepareForSegue. Something like:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ProgressViewEmbed" {
         let dest = segue.destinationViewController as ProgressViewController;
         dest.strVLow = "Hello"
    }

}

So, remove the internal var progressViewController and remove all the init functions. And then pass parameters through non-IB variables and then assign these values to IB values in ProgressViewController.viewDidLoad(). Something like the following:

class ProgressViewController {
    // IB Outlets
    var strVLow : String!;

    override func viewDidLoad() {
        super.viewDidLoad();
        if let str = strVLow {
            self.lblVLow.text = str;
        }
    }
}

In order to change the Embed Segue identifier, go to storyboard and click on the embed segue and change the identifier to anything you need to: enter image description here

EDIT: Reconstructed the answer completely.

Upvotes: 1

sketchyTech
sketchyTech

Reputation: 5906

The UIViewController adopts the NSCoding protocol, which requires an initWithCoder: method, or init(coder:) as it becomes in Swift. To meet these needs you need to comment out the fatalError or delete it. The property progressViewController then needs to be set and you need to call super.init().

class ViewController: UIViewController {

    internal var progressViewController : ProgressViewController        

    required init(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        progressViewController = ProgressViewController()
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }    
}

Upvotes: 1

Related Questions