Subclassing UIGestureRecognizer and returning custom UIView - ios

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.

Related

Detect when UIView is added in other UIView which MUST has a UIViewController?

Is there a notification or callback when a UIView is added in other UIView which has a UIViewController. We can get a UIView's ViewController, if it has one, via nextResponder. (Reference)
But depending on nextResponder is not reliable, if the UIView is not been added into a View which has ViewController, this method fails. For example, when we are calling from the cell's responder in UITableViewDataSource's cellForRowAtIndexPath. Because by the time of calling cellForRowAtIndexPath, the cell being dequeued hasn't been added into UITableView yet. However, we can call that method in - tableView:willDisplayCell:forRowAtIndexPath:, because by the time of calling - tableView:willDisplayCell:forRowAtIndexPath:, the cell is already being added into TableView.
So if you have ViewA which is the view of ViewControllerA and you want to add the ViewB you can subclass ViewB
#protocol ViewBDelegate
- (void) viewAddedToSuperview:(ViewB*)sender;
#end
#interface ViewB: UIView
#property (nonatomic, assign) id< ViewBDelegate > delegate;
#end
#implementation
- (void)didMoveToSuperview {
[super didMoveToSuperview];
[delegate viewAddedToSuperview:self];
}
#end
and in ViewControllerA you implements the protocol ViewBDelegate.
This is just an example of my idea.
Let me know in the comments it is can help you, otherwise I will try to propose other solutions depending on your goals.

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 .

Initialize SubView and exception

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.

Trigger method in parent uiviewcontroller from uibutton in uiscrollview

I have a uibutton inside a uiScrollview inside a uiviewcontroller. When I click the button I want to be able to call a method in the uiviewcontroller.
I have the following action on my button that calls a method within my uiscrollview:
[categoryButton addTarget:self action:#selector(categoryButtonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
But this just calls a method within my uiscrollview (which I will still keep). How do I then call a method in the viewcontroller??
Thank you for any help you can provide.
enter code hereTry doing something like this
#import <UIKit/UIKit.h>
#protocol myUIScrollViewProtocol <NSObject>
-(void)buttonWasPressInScrollView:(id)sender;
#end
#interface MyUIScrollView : UIScrollView
#property (weak, nonatomic)id myDelegate;
#end
Create a subclass of UIScrollView and add a delegate then assign your UIViewController as the delegate and implement the "buttonWasPress.." method in it.
Then from the categroryButtonWasClicked method in your uiScrollView:
-(void)categoryButtonWasClicked{
if ([self.myDelegate respondsToSelector:#selector(buttonWasPressInScrollView:)]){
[self.myDelegate buttonWasPressInScrollView:self];
}
...
}
in your viewController's .h add the following
#interface MyViewController : UIViewController <myUIScrollViewProtocol>

Resources