Problem Statement : I have a UIView Nib. Inside that Nib I have UICollectionView. I created Custom UICollectionViewCell and Assigned its class i.e. ServiceCollectionView Cell. I have 2 labels inside that UICollectionViewCell. Now When I try to Make an Outlet for those labels in custom class, I doesn't let me do that. I Have attached an image too. Anyone have an idea where the problem is ? Or What I am doing wrong. Any Help would be much appreciated.
Thanks
In case of adding a collection view/ table view in a UIView with separated .xib file, you should also create a separated .xib custom cell.
In your UIView Custom class, you should implement register(_:forCellWithReuseIdentifier:), for example:
// collectionView is the IBOutlet for the collection view that exists in the view .xib file
collectionView.register(UINib(nibName: "CustomCollectionViewCell", bundle: nil), forCellReuseIdentifier: "CustomCollectionViewCell")
And you will be good to go.
For some reason Xcode opens the interface (yourclass.swift (interface))and not the class. Restart Xcode and switch between automatic and manual did help me.
You create a collection View Cell nib file separately. Now you can give the outlet connection. error is not displayed.
Related
I am trying to create an IBOutlet connection/reference from my button to my UIViewController in Xcode, but it is only giving me the "option" to add an Action connection. I am trying to utilize the viewDidLoad and baselineAdjustment to vertically center the buttons label when the text scales to fit the width, but am not having any luck.
Does anyone have a suggestion or know what I'm doing wrong?
EDIT: Adding a few more screenshots for clarity
If I select my main Scene View, the class is of type ViewController. If I select the view that the Button in question is a part of(it is in a ContainerView), it is of class UIViewController and the options do not present a choice of ViewController.
Make sure the right class subclassing the UIViewController, make sure when you are doing it, you choosing the UIViewController and not the View or the safe area.
Let me know if it solved it
I see the you have multiple ViewControllers in storyboard. Ideally, each View controller in the storyboard is supposed to be of only one type of UIViewController implementation and it's also true the other way around. so, If you have say 3 UIViewControllers in Your storyBoard, then you will need to create 3 .swift files which implement UIViewController like so:
abcVC:UIViewController { .....
efgVC:UIViewController { .....
ViewController:UIViewController { ..... //this is the default one you get.
and then set the class of each ViewController in your storyboard to one of each of the above.
if your ViewController which has the button you want to outlet has a class type abcVC, then you can outlet your button only in abcVc's implementation in abcVC.swift.
Hope it makes sense. to see how to set class, refer #Vadim F. 's answer.
and if you happen to upvote this answer, please also consider upvoting #Vadim F. 's answer.
This is how you can crate a new .swift file while subclassing a UIViewController: File -> new -> File -> Cocoa touch class -> #make it subclass of UIViewController and give it a unique name eg: abcVC
Certify that your ViewController in the Storyboard is of the class ViewController (i.e your view controller) and not from UIViewController.
So everything is done the way you would assume. I drag and drop a collection view into a controller view. I connect the datasource and delegate. blah blah blah everything is done by the book.
Ok so i want to connect a label inside my collectionViewCell to my viewController class. I use control+drag to drop it into my code and type in its name.
As soon as i hit connect, i get a red octagonal error. it says: "Illegal Configuration: The hoursPerDayOutlet outlet from the monthCellViewController to the UILabel is invalid. Outlets cannot be connected to repeating content."
I need to connect this though because as each cell is created it will have a different label.
Please help me fix and use swift. All advice is appreciated. Thanks!
You can't connect items inside a UICollectionViewCell to a UIViewController, create a UICollectionViewCell subclass, make the class of the cell to the subclass and then you can connect the items inside the cell to the subclass (not the controller). I recommend you to look at more tutorials on UICollectionView.
To add to the other answer, after creating a UICollectionViewCell subclass and verifying that the class of that cell is subclassed, you have to make sure that your UIViewController that you were originally trying to connect the outlet to is a UICollectionViewDelegate subclass.
After that you should be able to successfully create an outlet for your label, etc.
I created a custom UICollectionViewCell (let's call it MyCustomCell) using a xib (MyCustomCell.xib) and I want to create a subclass of MyCustomCell (we'll call it MyCustomCell2). The parent of MyCustomCell2 (MyCustomCell) has an IBOutlet called titleLabel. However, titleLabel for MyCustomCell2 always returns nil even after I do
self.collectionView.
registerClass(MyCustomCell2.self, forCellWithReuseIdentifier: cellIdentifier);
in viewDidLoad
and
let cell = collectionView.
dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyCustomCell2;
in cellForRowAtIndexPath
Any thoughts on what the issue is?
Firstly, when you use registerClass your outlets are not getting connected. You need to use registerNib.
Secondly, the outlets need to be connected in the xib which you are loading. If you are loading the xib of your subclass you need to connect outlets in it.
I have two class and in both class tableview uses custom nib file(for table cell)as well as i make IBOutlet instance of myview controller to that custom cell nib file but i can not use same nib for both class becuase i have give file owner of that xib to the myviewcontroller so when i used that one in another class it give error so any ideas so use the same xib for both the view controller?
If you want to reuse your custom table cell with multiple view controllers, check out this answer. The two methods described there let you load your custom table cell from a nib file without having to set the File's Owner to your specific controller.
I have created a UITableViewcontroller and a UINavigationController in a TableController.m
with UITableviewCell set to say #"CellOne" #"CellTwo". Now I also created two other files
`ImageView1.m`
ImageView2.m
where if I click on CellOne I should be able to get the view placed on ImageView1.m, same applied to the ImageView2.m. How should I achieve this programmatically without using nib file?
override the touches event in ImageView1 and ImageView2.