Storyboard Instantiateviewcontrollerwithidentifier Not Setting Iboutlets

storyboard instantiateViewControllerWithIdentifier not setting IBOutlets

The view seems to be initialized properly only after it is accessed first. The problem goes away when calling

[self presentViewController:vc animated:NO completion:nil];

or more simply

[vc view];

IBOutlet is nil, but it is connected in storyboard, Swift

The storyboard wasn't recognizing any further UI things I added to it. At run time all the references were nil. So I cleared my derived data folder and then those connections worked again.

IBOutlet nil after instantiateViewControllerWithIdentifier

Thanks to rdelmar, I was able to figure this out using protocols.

Here's what I added:

  • SearchDelegate
  • SearchDelegate function runSearch()

VC1

class MainViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, SearchDelegate {  

func runSearch(searchString: String) {
loadPhotos(searchString)
}

}

VC2

protocol SearchDelegate {
func runSearch(searchString: String)
}

class SearchViewController: UIViewController, UITextFieldDelegate, UISearchBarDelegate, UICollectionViewDelegate {

@IBOutlet var searchMask: UIView!
@IBOutlet var searchField: UITextField!
@IBOutlet var searchBar: UISearchBar!

var delegate: SearchDelegate?

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
delegate?.runSearch(searchBar.text)
self.dismissViewControllerAnimated(true, completion:nil)

}

IBOutlet elements come out nil when I try to set them in viewDidLoad

I've had the same problem under iOS7 on very slow devices.

Just wait for your views to be rendered in viewDidLayoutSubviews then do your changes.
Don't forget to do so just once though for viewDidLayoutSubviews is being called a lot.

var first = false

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if self.first {
//Update your views
self.first = false
}
}

You could also use view.layoutSubviews() but I recommend you not to ;)

EDIT

I did not see the problem at first sight but you are creating your view controllers from scratch! You must instantiate them using a storyboard or your IBOutlets won't be set ;)

var vc1 = storyboard?.instantiateViewControllerWithIdentifier("YourControllerStoryboardId") as! CarouselViewController

Hope this'll help!

instantiate ViewController With storyboard Identifier in IOS

The problem here is that you are referencing outlets that haven't yet been created. I would try adding the User model as a property of the view controller (or use some sort of intermediate controller for this), setting the user property in your function above, then in viewDidAppear set the text of all the controls appropriately using the User model.

If you do this on the viewDidAppear function you can be certain all of your views have been instantiated. this is also better design, after all; why does your LoginViewController class need to know about the internals of profileViewController? Encapsulating logic in this way will lead to less problems down the line.

Hope this helps.

IBOutlets to other view controllers in storyboard

An IBOutlet is probably not the way to go about this. The best way to do so in my opinion would be to get the nib file using an identifier that you specify in storyboard and then in the viewDidLoad method, type this in and replace the variable name and identifier with the applicable names.

UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"myIdentifier"];

Hope this helped you get it working.



Related Topics



Leave a reply



Submit