Unable to access custom tableviewcell button properties in Swift - uitableview

Working on my first iOS app. I have created nib and a custom tableview cell. The cell has buttons and labels. I am able to access the labels but for some reason I cannot get the properties of the buttons such as set title, for example. I only have access to the following:
cell.followUserButton(sender: AnyObject)
Why can't I just go
cell.followUserButton.setTitle = "Unfollow"
I looked online quite a bit. Couldn't find anything. Any help is appreciated. Thanks.

You have to write setTitle() this way:
cell.followUserButton.setTitle("Sample Title", forState: .Normal)
Btw setTitle() is function not a property.

Solved my own problem. I was setting it as only an IBAction. If you want to change the attributes then you also have to make the button an outlet too!
#IBOutlet weak var followUser: UIButton!
As you may already know, you can do this within the storyboard. Works perfectly now.

Related

What to check when outlet detection is not working?

I'm not sure what I did wrong, but I tried to connect the outlet from my main storyboard to my new ViewControllerTableViewCell.swift that I just created.
I set the class to ViewControllerTableViewCell
It contains
import UIKit
internal class ViewControllerTableViewCell : UITableViewCell {
override internal func awakeFromNib()
override internal func setSelected(_ selected: Bool, animated: Bool)
}
Now, I open up my Main.storyboard and attempting to drag my image and label
I did't see the auto detect come up.
However, when I tried to drag those into my ViewController.swift, it works.
To make sure it is not XCode Caching issue. I also restart my XCode, and still face same issue.
I was hoping to see something like this
How do I check further to make this work again?
Good question. That's one of the disadvantages of using the interface building. But anyways, here's a solution:
Make sure that your custom tableView class, ViewControllerTableViewCell is assigned to your custom cell in your storyboard, like so:
Then, click on the automatic thing above, and see if you're viewing the cell class.
Try again now to have an outlet.
Also, CMD+B (building) helps the Xcode to correct some things.
You need to assign the custom tableCell class to the cell in IB
//
//

Swift. Can't Connect my Button to GIDSignIn Button in my Class

I am new to the swift. I follow the google tutorial to use Firebase Google OAuth method.
https://firebase.google.com/docs/auth/ios/google-signin
At step 9, I try to connect a custom button by control + drag to my property :
#IBOutlet weak var signInButton: GIDSignInButton!
But Xcode doesn't select it. It shows a pop up to create a new property. Though I have two others button: UIButton and when I try to connect to them, it works.
Could help me with that? It might miss some elements of my problem, so don't hesitate to ask.
Thank you!
Drag from the empty circle on the left of #IBOutlet to your button (not vice versa)

Can't change custom class of UIButton to GIDSignInButton

I'm trying to add Google sign in functionality to a custom button, and I'm following this tutorial. It's telling me to select the button, and set its class as GIDSignInButton, then add this code: #IBOutlet weak var signInButton: GIDSignInButton!.
However, it doesn't let me set the class to GIDSignInButton. When I type it in and press enter, the field just clears.
You should try assign GIDSignInButton not to the Button Object from the Object library but to the the View Object instead
It's work for me.
It will look like this using UIView instead of UIButton.
You can create UIButton and then on its action method you can write this code for signing via google:
GIDSignIn.sharedInstance().signIn()
It works for me, in this way you can customize UIButton according to your requirement and also perform signin by using google
That's because GIDSignInButton is a subclass of UIView, not UIButton.
Add to the storyboard / nib a regular UIView and change it's class to GIDSignInButton instead.
From google doc:
Add a GIDSignInButton to your storyboard, XIB file, or instantiate it
programmatically. To add the button to your storyboard or XIB file,
add a View and set its custom class to GIDSignInButton.
GIDSignInButton can be set by using a UIView or a UIButton.
If you are using GIDSignInButton as a UIButton
open the storyboard as source code
Find the button in resulting XML
Add the below code as an attribute for the button tag
customClass="GIDSignInButton"
4.open storyboard again as Interface Builder, button class will be changed
If you are using GIDSignInButton as a UIView
1.copy paste the custom class as GIDSignInButton in Identity Inspector
The second one is the correct approach in my opinion.
The workaround is open the storyboard in text mode and put it directly. When you return to the interface builder it will show normally.
Open storyboard as source code.
Locate the button in the xml.
Set customClass="GIDSignInButton" as an attribute for the button tag.
Open storyboard as interface builder.
You can now link the button to the IBOutlet
If you use a UIView instead of a UIButton, you can assign the view a custom class of GIDSignInButton. From there you can connect the view to a button outlet and action as seen below.
#IBOutlet weak var googleLoginButton: GIDSignInButton!
#IBAction func googleLoginButtonPressed(_ sender: Any) {
GIDSignIn.sharedInstance()?.signIn()
}
I had the same problem a few months ago,
Your code seems to be right
#IBOutlet weak var signInButton: GIDSignInButton!
But, the problem might be
• You haven’t added the Framework properly
(Go to your project setting in the left side navigator, and click Build phases, add your framework and SHIFT + CMD + K)
• Or alternatively, go ahead and write the #IBOutlet in your swift file, then drag the button to assign it
• your last option is to close xcode, or maybe delete the derived data
Xcode itself has plenty of bugs, I am not sure if it’s your problem, it’s xcode’s
Hope this helps!
The simple way to do is just make a button action and paste the following lines in it.
#IBAction func gSignInAction(_ sender: Any) {
GIDSignIn.sharedInstance()?.signIn()
}
Use UIView instead of UIButton and assign custom class as GIDSignInButton
Swift- 5
Open storyboard as source code.
Locate the button in the xml.
Set customClass="GIDSignInButton" as an attribute for the button tag.
Open storyboard as interface builder.
//MARK:- You will not find this with button XML Forcly Add this (customClass="GIDSignInButton")[![enter image description here][1]][1]

Adding a switch to UITableViewCell: storyboard vs code

I want to add a UISwitch to my settings view controller. The switch looks identically to Airplane Mode toggle in the 'Settings' app on iOS. I'm not sure about what is the best way to implement this.
I am choosing between:
adding a UISwitch in code
creating a custom cell in the Storyboard and creating an outlet for a switch
Here's my Swift code for adding a switch to UITableViewCell:
let soundSwitch = UISwitch(frame: CGRectZero)
soundSwitch.addTarget(self, action: "test:", forControlEvents: UIControlEvents.ValueChanged)
// I created an outlet for the cell that will contain a switch
soundCell.accessoryView = tickingSoundSwitch
What are advantages and disadvantages of using these solutions?
My suggestion is to use either
1.UISwitch in storyboard along with an IBAction , if you use this approach things becomes easy ,you need not to add target and mess with lot of codes.
or
2.Completely rely on code, Using this approach your UISwitch will not get tight coupled with storyboard's UI. Even in the future if you want to change the UI stuffs in storyboard you need not to do any changes for your UISWitch ,i.e you don't need to attach all those IBAction and outlet stuffs as you are using pure code based approach(provided your UISwitch requirement doesn't change).

Can't link a bar button to an #IBAction in superclass

I have a number of view controllers, each with their own menu button (a UIBarButton, added in the storyboard). Now I want to link all these up to a single #IBAction function in their superclass (the superclass is the same for all the view controllers with that menubutton).
Now I have linked up #IBOutlets to a superclass before, but it doesn't seem to work with #IBActions, even though the function isn't private, and it definitely is part of the superclass (I am refactoring, previously it was an #IBAction in each class, which only did menuButtonTap() (calling the method in the superclass).
Any ideas?
I have solved the problem by manually creating an #IBAction on the superclass, and giving them the same name as the ones I create in the subclasses. Then I deleted the ones in the subclasses. This leaves a 'dangling reference' from the storyboard, according to Xcode, but I know it's there.
Although this still does not work as of Xcode 9.4 for general purpose UIViewController (but your workaround still does work 👍🏻), please note that it works as expected for UITableViewCell templates in storyboard.
If some of your template cells in storyboard share the same base class containing #IBOutlet properties, you will be able to link them to every template cell instance as you usually do:
Then Xcode will show a popup for telling in which prototype cell the link is "backed":
I'm not sure why this second step is necessary though, since you designate a specific component from within a given prototype cell
You can do it like you do when adding an action to a UITabbar button from subview class.
Assuming btn is a UIBarButtonItem,
[btn setTarget:self.superview];
[btn setAction:#selector(menuButtonTap:)]
Are you using __unused keyword by any chance? If you do the IBAction won't show up in storyboard(I am using Xcode 6.3.2)
- (IBAction)actionBack:(__unused id)sender;
vs
- (IBAction)actionBack:(id)sender;
To make it show up and selectable removed the __unsused keyword.

Resources