Initialize SubView and exception - ios

I have a UICollectionView Cell that try to initialize a custom subview but giving me error.
//In Init - (id)initWithFrame:(CGRect)frame
DetailSubView *view=[[DetailSubView alloc]initWithFrame:CGRectZero];
self.contentView=view;---> ERROR
//.h file
#property (strong, nonatomic) DetailSubView *contentView;
Error
[DetailCustomCell setContentView:]: unrecognized selector sent to instance 0xe48a090

This is because the contentView property of a cell is read-only. To customize the cells content view, add subviews to it.

Related

iOS: Detect backspace in empty UITextField with UITextField subclass

The clear answer to detecting a backspace is by creating a subclass of UITextField and overriding the deleteBackward property.
I've created a subclass a UITextField subclass, but am getting this error:
'NSInvalidArgumentException', reason: '-[UITextField setMyDelegate:]: unrecognized selector sent to instance
This is my subclass code:
My TextField.h:
#protocol MyTextFieldDelegate <NSObject>
#optional
- (void)textFieldDidDelete;
#end
#interface MyTextField : UITextField<UIKeyInput>
//create "myDelegate"
#property (nonatomic, assign) id<MyTextFieldDelegate> myDelegate;
#end
My TextField.m:
- (void)deleteBackward {
[super deleteBackward];
if ([_myDelegate respondsToSelector:#selector(textFieldDidDelete)]){
[_myDelegate textFieldDidDelete];
}
}
In my ViewController that I would like to access the UITextField subclass I do the following:
#import "MyTextField.h"
<MyTextFieldDelegate>
#property (nonatomic, strong) IBOutlet MyTextField *passcode1;
self.passcode1.myDelegate = self;
Any ideas as to why I am getting the unrecognized selector error? It looks to me like I have done everything correctly in subclassing UITextField.
The problem is with your outlet. The property is defined with your custom class but in a Interface Builder you added a plain old UITextField. Hence the error at runtime. In Interface Buldier, update the text field's class to be your custom class.

iOS custom UIView access properties in code

So I've created a custom UIView subclass and have it assigned to a UIView in my main storyboard. When the view loads everything is displayed properly.
The issue I'm having is that I need to be able to access properties of said custom UIView since the view is data driven.
JSON_table.h:
#import <UIKit/UIKit.h>
#interface JSON_table : UIView
#property (strong, nonatomic) IBOutlet UIView *view;
#property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
#property (weak, nonatomic) IBOutlet UITableView *table_view;
#property (weak, nonatomic) NSString *data_header;
#property (weak, nonatomic) NSString *data_list;
#end
JSON_table.m:
#import "JSON_table.h"
#implementation JSON_table
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[[NSBundle mainBundle] loadNibNamed:#"JSON_table" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
#end
(I know I'm missing delegates for tableview, ill be adding these later)
The issue I'm having is when I right click on my UIView on my storyboard I get:
The problem is when I try to connect "view" to my header file "
ViewController.h" it doesn't let me create a IBOutlet, so I cannot reference my view and its properties in code.
This is what I am trying to accomplish:
"Table" is of type UIView
Idea:
Would this have anything to do with the UIView being on the second view in my storyboard? I noticed that I don't seem to have any problem attaching to anything on the first page, but the second one I can't.
You can only connect the outlets of a view to it's class object. You are trying to connect outlets of JSON_table object to UIViewController object.
If you need to access those properties in UIViewController object. You need to import
JSON_table.h
in your view controller. And create and instantiate a object of it.
JSON_table * customView = [[JSON_table alloc]init];
Now you can access all the properties of it as:
customView.searchbar, customView.view etc.
Added by theshadow124:
Thanks to everyone who attempted to help me solve my problem. Due to being fairly new to coding for iOS I didn't realize I had to assign a custom class to every UIViewController in my storyboard(I thought they they would inherit from the base if I didn't specify). simply creating a new subclass of UIViewController and assigning it under the Identity inspector fixed my problem and now I can properly assign outlets.
Im going to accept this answer because it was one of the issues I ran into after fixing the subclass on the storyboard issue.
Please make sure that in assistant editor your are opening the same class that your custom class is contained in .

iOS Custom View Text Labels Are Nil

Right now I have a custom view class called OTGMarkerDetailView which inherits from UIView and a corresponding .xib with it. It just has two text labels and I've linked the text labels to the text label IBOutlets in OTGMarkerDetailView.m.
OTGMarkerDetailsView.h:
#import <UIKit/UIKit.h>
#interface OTGMarkerDetailView : UIView
- (void)setLabelsWithMainAddress:(NSString *)mainAddress subAddress:(NSString *)subAddress;
#end
OTGMarkerDetailView.m
#import "OTGMarkerDetailView.h"
#interface OTGMarkerDetailView ()
#property (nonatomic, strong) IBOutlet UILabel *mainAddressLabel;
#property (nonatomic, strong) IBOutlet UILabel *subAddressLabel;
#end
#implementation OTGMarkerDetailView
- (void)setLabelsWithMainAddress:(NSString *)mainAddress subAddress:(NSString *)subAddress {
NSLog(#"%#", self.mainAddressLabel.text);
self.mainAddressLabel.text = mainAddress;
self.subAddressLabel.text = subAddress;
NSLog(#"%#", self.mainAddressLabel.text);
}
#end
I load it in another view as a subview, using initWithFrame. But the console always logs null when I try to set the text label values, and when I use a breakpoint it seems the mainAddressLabel and the subAddressLabel are nil themselves. Did I do something wrong in linking the xib to the view? What am I missing? Thanks.
I found a work around. I have created a custom UIView.
1.
I attached Nib file to it in initWithFrame method
CustomView *nibView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil];
nibView = [array objectAtIndex:0];
[self addSubview:nibView];
}
return self;
}
You can see clearly, I haven't created the instance of UIView instead I created nibView of the same class type.
2.
Now creating IBOutlet properties and work on it. In customView.m file.
#interface FTEndorsedExpandedView : UIView
#property (retain) IBOutlet UILabel *label;
#end
3.
Create functions to set title or changing properties. (in customView.m file). Use nibView to access the properties rather than using self.label
-(void)setLabelText:(NSString*)string{
[nibView.label setText:string];
}
When you create your custom view in another view using initWithFrame a new instance of your custom class is created. This instance is not the same one you have in interface builder and hence the label properties are nil for this newly created instance. In order to solve this problem either put your view in its parent view in interface builder with its connection attached or override initWithFrame for your custom view and initialise your labels in there.

Subclassing UIGestureRecognizer and returning custom UIView

I've created my own UIView subclass with a new prototype. That no problem, but when I'm trying to get my state of the subclassed UIView it goes wrong.
I have an GestureRecognizer on my UIView and with subclassing and using the new variable it is not recognized because the UIGestureRecognizers view variable returns an UIView.
So I subclassed the UIGestureRecognizer but this does not work. I'm getting an error:
-[BannerView countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xe872480
And here is my code in the subclassed UIGestureRecognizers:
#import <UIKit/UIKit.h>
#import "BannerView.h"
#interface GestureReco : UIGestureRecognizer
#property (nonatomic,readonly) BannerView *view;
#end
How can I solve this problem or do I need to handle this on an other way that the UIGestureRecognizer knows that I've subclassed the UIView and want to GET my variable from my own UIView class.

Multiple UIViews in XIB of UITableViewCell

I've been trying to make a custom UITableViewCell with a UIScrollView.
I have a .xib which contains a UITableViewCell (that contains only a UIScrollView) and a UIView that is the content view for the cell's scrollView.
Alright, now here's the code for the UITableView that should display the custom cell :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.tableView registerNib:[UINib nibWithNibName:#"RecessCell" bundle:nil] forCellReuseIdentifier:#"Recess"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RecessCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Recess"];
return cell;
}
The header for the RecessCell class:
#import <UIKit/UIKit.h>
#interface RecessCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIView *contentView;
#end
scrollView is an outlet for the UIScrollView inside each cell.
contentView is an outlet for the UIScrollView's content view.
Well, so far everything seems legit, but when I run the app:
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class
is not key value coding-compliant for the key contentView.'
I've read plenty about that run-time error, all I could find is :
I need to verify that the contentView is actually declared in
the header. (as you can see, it is declared there)
I need to change UITableViewCell to RecessCell in cellForRow, which I did.
How can I fix this problem? is creating custom UITableViewCells so difficult?
You shouldn't create another outlet called contentView in your header file. The UITableViewCell already has a property called contentView.
Also if you are creating a .xib with a UITableVieCell and you are adding views to the UITableViewCell the views will be added to the content view.
So what you need is to go in your .xib file and remove the link between the view that is linked to your contentView IBOutlet. (You can also delete that view, but be careful so that you won't delete the subviews)

Resources