Reputation: 353
I have got a viewController in storyboard with following file import UIKit
class jokesviewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
} and another controller in which i use this controller by making its object , (well this a JSQmessagesController Class in which i make object of jokesviewController class) and the code i use is
var jokeviewController :jokesviewController?
//at class begnning
and somewhere in some function
jokeviewController = jokesviewController()
self.view.addSubview(jokeviewController!.view)
jokeviewController!.view.backgroundColor = UIColor.grayColor()
my question is i get to use jokeviewController!.view giving me a view to use but when i try to get jokeviewController!.tableview it says i got a nil value . I have tried both ways putting tableView inside a view and tableview without container view in the controller . Any guesses
Upvotes: 0
Views: 31
Reputation: 7948
First of all, always use upper case for controllers( or classes ).
Secondly you instantiate view controller as is, but IB outlets and other stuff lives in storyboard. So, instead of
jokeviewController = jokesviewController()
use
jokeviewController = UIStoryboard(name: "yourStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("giveYourViewControllerIdentifier") as! jokesviewController
and you will have everything set up.
For giving identifier for your view controller in storyboard, select your view controller in storyboard, select third tab and set storyboard ID to whatever you want.
Upvotes: 1