How to trigger IBAction from UIButton in Storyboard static cell? - ios

I have a Storyboard with a UITableViewController. In this controller, I have static cells where one of them is a subclass of UITableViewCell. In the IB I added a UIButton to this static cell and connected it to an IBAction in the UITableViewCell subclass. However, the IBAction is never called. Why doesn't this work? How can I achieve the desired behaviour where a button inside a cell triggers an event through the IB?
.h
#import <UIKit/UIKit.h>
#interface BSWorkoutsCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIButton *button;
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons;
- (IBAction)tapButton:(id)sender;
#end
.m
- (IBAction)tapButton:(id)sender{
if (!_buttonsHidden){
[self animateHideButtons:(UIButton *)sender];
_buttonsHidden = YES;
}
}
Images

I realised that the UITableViewCell had User Interaction Enabled unchecked in the IB. This made any subviews inside it not interactive, thus the buttons not actually being pressed, and the IBAction not being called.
Once I checked the box, everything worked fine.

Related

selecting UITextField in custom cell crashes

I'm trying to create a form like view using custom cells in uitableview. I have 3 custom cells, one with 6 UITextFields, one with just one, and one with a UITextView. It displays perfectly, and scrolls up and down with no problems. My problem is that whenever I select anything, either a textField or TextView it crashes with EXC_BAD_ACCESS. My break point shows that the textfields are not nil.
Any ideas about what may be causing my crash?
custom cell - properties in .h file - delegates set in custom cell class
#interface CandidateInfo1Cell : UITableViewCell
#property (nonatomic, strong) IBOutlet UITextField *firstNameText;
#property (nonatomic, strong) IBOutlet UITextField *lastNameText;
#property (nonatomic, strong) IBOutlet UITextField *cityText;
#property (nonatomic, strong) IBOutlet UITextField *stateText;
#property (nonatomic, strong) IBOutlet UITextField *emailText;
#property (nonatomic, strong) IBOutlet UITextField *phoneText;
#end
registering my nib:
UINib *infoCellNib = [UINib nibWithNibName:REUSE_INFO_CELLID bundle:[NSBundle bundleForClass:[InfoCell class]]];
[self.listTable registerNib:infoCellNib forCellReuseIdentifier:REUSE_INFO_CELLID];
cellForRowAtIndexPath:
InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSE_INFO_CELLID];
[self configureInfoCell:(InfoCell *)cell forRowAtIndexPath:indexPath];
return cell;

How to get the label in the collection cell when click a button in the same collection cell

I have a View Controller with a UICollectionView. Each UICollectionViewCell has one UILabel and one UIButton. How can I print the text of each UILabel in the UICollectionView when clicking the corresponding UIButton.
If I understood you right, you can subclass your UICollectionViewCell, link UILabel to corresponding property and create a method for UIButton touch. Here is the sample code of this method:
#interface MyCustomCollectionViewCell: UICollectionViewCell
#property (weak, nonatomic) IBOutlet UILabel *cellLabel;
- (void)cellButtonClicked:(id)sender;
#end
#implementation
- (void)cellButtonClicked:(id)sender {
NSLog(#"%#",_cellLabel.text);
}
#end
Besides it, you can look through some guide, e.g. this one.

Custom TableViewCell programmatically

I have source code that does not use storyboard or xib. It contains a UITableView and the cells use the standard cell.textlabel.
What i want is to use a custom TableViewCell, normally i would create a UITableViewCell class and connect them in storyboard, but i cant connect them here since its not using storyboard or xib.
I have following UITableViewClass
#interface chatCell : UITableViewCell
#property(nonatomic, weak) IBOutlet UIImageView *homeTeamImage;
#property(nonatomic, weak) IBOutlet UILabel *homeTeamLabel;
#property(nonatomic, weak) IBOutlet UIImageView *awayTeamImage;
#property(nonatomic, weak) IBOutlet UILabel *awayTeamLabel;
#end
How can i place them inside the TableViewCell and connect them to the properties?
so i can start using
cell.homeTeamImage
cell.homeTeamLabel
cell.awayTeamImage
cell.awayTeamLabel
You don't need to use IBOutlet, since you don't want to use the properties in IB
#interface chatCell : UITableViewCell
{
UIImageView *homeTeamImage;
UILabel *homeTeamLabel;
UIImageView *awayTeamImage;
UILabel *awayTeamLabel;
}
#property(nonatomic, weak) UIImageView *homeTeamImage;
#property(nonatomic, weak) UILabel *homeTeamLabel;
#property(nonatomic, weak) UIImageView *awayTeamImage;
#property(nonatomic, weak) UILabel *awayTeamLabel;
#end
and don't forget to
#synthesize homeTeamImage, ...;
in your implementation file.
You can override the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath. Instead of returning a standard UITableViewCell you can return yours
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell c = [[chatCell alloc] init];
return c;
}
You don't need the IBOutlet keyword: it isn't a type, it's only used by xcode when you use the storyboard. I think that without storyboard you should change the properties to strong pointers because no one else is strongly pointing to them so they'll be deleted.
The #synthesize is not mandatory if you use xcode 5. It's required only if you override both setter and getter of a property.
See Programmatically Adding Subviews to a Cell’s Content View from Table View Programming Guide for iOS. In this example cells are created in tableView:cellForRowAtIndexPath: but you can use same approach (add views on cell's content view) in UITableViewCell subclass.
#interface chatCell : UITableViewCell
{
UIImageView *homeTeamImage;
UILabel *homeTeamLabel;
UIImageView *awayTeamImage;
UILabel *awayTeamLabel;
}
#property(nonatomic, weak) UIImageView *homeTeamImage;
#property(nonatomic, weak) UILabel *homeTeamLabel;
#property(nonatomic, weak) UIImageView *awayTeamImage;
#property(nonatomic, weak) UILabel *awayTeamLabel;
#end
and don't forget to
enter code here
#synthesize "All properties"

how to connect IBOutlet from a class to another class

I have Class A which contains a xib with some buttons and a UITextField and Class B which contains a tableView
I want to add Class A to the table view header.
I am importing the xib from class A into class B using [[[NSBundle mainBundle] loadNibName: owner:self options:nil] objectAtIndex:0];
I just want to reflect the button and textview actions inside classB. In ClassB I do have some UITextfield Delegate methods which i expect to be called when typing some text in uitextfield defined and declared in ClassA (xib + property definition in .h file).
Class A .h:
//properties are set from xib file ClassA.xib
#interface ClassA : UIView
#property (unsafe_unretained, nonatomic) IBOutlet UIButton *addC;
#property(strong, nonatomic) IBOutlet UITextField *nText;
#end
Class B.h
#import "ClassA"
#interface ClassB : UIViewController <UITextFieldDelegate, UITableViewDataSource,...>
#property (strong, nonatomic) IBOutlet UITableView *mTableView; //property set from ClassB.xib
#property(strong, nonatomic) IBOutlet UITextField *nText_B;
#property (unsafe_unretained, nonatomic) IBOutlet UIButton *addC_B;
- (IBAction)addC:(UIButton *)sender;
#end
I don't know how to make the link ClassB button and textfield to ClassA
In ClassB viewDidLoad method i;ve tried this:
getHeader = [[ClassA alloc]init];
_addC_B = [getHeader addC];
_nText_B = [getHeader nText];
_nText_B.delegate =self;
_nText_B.text=#"";
but it's not working
I am new to iOS on this side area.
Thank you for your help
You haven't explained your problem well. Classes do not "contain xibs".
If your objective is to create a piece of user interface in XIB and reuse it, you can achieve this with [NSBundle loadNibNamed:owner:options:]. The best way to reference parts of it is through properties like Noval explained in his answer. Another possibility is to attach views to "File's Owner" outlet's properties.
you can't connecting IBOutlet that placed outside your XIB class. but if you just want to access the outlet from other class, it's possible by creating instance of ClassA in ClassB. and then accessing the property like instanceOfClassA.outletObject

UITableViewCell is loading filler content instead of programmatic input?

I have created a custom UITableViewCell that contains 3 UILabels and a UIImageView. I am setting this up via storyboards and have a UITableView class that is connected to the table cell. In that class I am wanting to handle the different UI objects and have the option to pass in information from my view controller.
Here is the .h of my table cell view class
#property (retain, nonatomic) IBOutlet GBPathImageView *ProfileThumbnailImageView;
#property (weak, nonatomic) IBOutlet UILabel *profileDisplayNameLabel;
#property (weak, nonatomic) IBOutlet UILabel *profileUniversityNameLabel;
#property (weak, nonatomic) IBOutlet UILabel *profileNumberOfStringsLabel;
#property (strong, nonatomic) NSString *profileDisplayName;
#property (strong, nonatomic) UIImage *profileThumbnailImage;
#property (strong, nonatomic) NSString *profileUniversityName;
#property (nonatomic) NSUInteger profileNumberOfStrings;
- (id)initWithProfileImage:(UIImage *)profileImage
displayName:(NSString *)displayName
universityName:(NSString *)universityName
numberOfUserStrings:(NSUInteger)numberOfStrings;
As you can see I have a property outlet that all of the storyboard objects are connected to. I also have properties for each piece of necessary information. In my view controllers
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method I am wanting to instantiate this custom cell with information that I pass in. I am wanting to set the information properties with the correct info and have it be setup in the view class.
Here is the code inside the previously mentioned method:
static NSString *cellIdentifier = #"UserProfileCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if ([cell isKindOfClass:[StringrUserTableViewCell class]]) {
StringrUserTableViewCell * userProfileCell = (StringrUserTableViewCell *)cell;
userProfileCell.profileDisplayName = #"Alonso Holmes";
//userProfileCell.profileDisplayNameLabel.text = #"User Name";
}
I have two lines there, one is setting the information property for the name, and I have it in the view's .m file to set its text to that properties name. The problem that is happening is that the profileDisplayName property is null at the time it tries to set that UILabels text. How can I set this information at a time so that it will be there before the cell is loaded or when it's initially loading? You can also see a commented out line where I directly set the text of the UILabel right there. I would prefer not to do it this way because I want to leave the abstraction/logic to the view's class. The image view I setup is custom so it takes more code.
You are trying to set your text in profileDisplayName which is NSString type not a UILabel class. ( see #property (strong, nonatomic) NSString *profileDisplayName; ).
Change your line of code:
here is the problem
userProfileCell.profileDisplayName = #"Alonso Holmes"; //Use Label Object instead of NSString

Resources