ios UI Elements in Custom UIView not responding - uiview

I'm using a custom UIView in myViewController. I have a SampleView which is my custom view. I have my class as..
SampleView.h
#property (strong, nonatomic) IBOutlet UIView *mainView;
#property (strong, nonatomic) IBOutlet UIButton *sampleButton;
and in the SampleView.m
I have added a basic init
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
UIView *view = nil;
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"SampleView"
owner:self
options:nil];
for (id object in objects) {
if ([object isKindOfClass:[UIView class]]) {
view = object;
break;
}
}
if (view != nil) {
_mainView = view;
view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:view];
[self setNeedsUpdateConstraints];
}
}
Now in my ViewController I want to change the property of the title on the sampleButton...
SampleView *sampleView = [[SampleView alloc]init];
sampleView.sampleButton.titleLabel.text = #"Hello";
[self.view addSubview:sampleView];
There is no change in the SubView's button text. Please do help me about the same.
Thanks.

I think
- (void)commonInit
{
//UIView *view = nil;
UIView *view=[[UIView all]init];
........
}

Related

Customize the UIComponents inside UITableViewCell by taking IBOutlets

I have made a UITableViewCell with xib, inside it I have made a UIView and made IBoutlet in my custom tableViewCell class. I want to set the border color of that UIView.
My code in tableViewCell.h:
#property (weak, nonatomic) IBOutlet UIView *circleView;
In tableViewCell.m:
#import "OUSTProfileTableViewCell.h"
#implementation OUSTProfileTableViewCell
//#synthesize circleView = _circleView;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
}
return self;
}
#end
But it's not working.
Put code inside
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}

UI code works but change to XIB failed

the code below works
#interface MyUICollectionViewCell : UICollectionViewCell {
}
#implementation UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self = [super initWithFrame:frame];
if (self) {
_moreView = [[UIView alloc] init];
_moreView.backgroundColor = [UIColor clearColor];
_moreView.frame = CGRectMake(self.frame.size.width - 70, 0, 45, 37);
[self addSubview:_moreView];
_moreView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(goToDoSomething)];
[_moreView addGestureRecognizer:tapMoreView];
}
return self;
}
return self;
}
- (void)doSomeThing
{
}
when I touch the subview (gray block), it will trigger the event,
but I change to XIB(the sub view links to IBOutlet mUIView), and change the code as below, goToDoSomething cannot be triggered.
Your comment welcome
#interface MyUICollectionViewCell : UICollectionViewCell {
IBOutlet UIView *mUIView;
}
#implementation UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self = [super initWithFrame:frame];
if (self) {
NSArray *nibView = [[NSBundle mainBundle] loadNibNamed:#"MyUICollectionViewCell"owner:self options:nil];
UIView *bw = [nibView objectAtIndex:0] ;
bw.userInteractionEnabled = YES;
[self.contentView addSubview:bw];
mUIView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(goToDoSomething)];
[mUIView addGestureRecognizer:tapMoreView];
}
return self;
}
return self;
}
- (void)doSomeThing
{
}

IBAction is not getting called for Button inside UIView

I have created a view for custom callout with a button. This custom callout gets displayed on click of any annotation on Mapview. But nothing happens when I click the button inside the custom callout.
#import <UIKit/UIKit.h>
#interface TestView : UIView
#end
#import "TestView.h"
#implementation TestView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(IBAction)showMessage:(id)sender
{
NSLog(#"Test");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if(![view.annotation isKindOfClass:[MKUserLocation class]]) {
CGRect rect = CGRectMake(0,0,268,140);
TestView *testview = [[TestView alloc] initWithFrame:rect];
testview = [self loadNibNamed:#"TestView" ofClass:[TestView class]];
[view addSubview:testview];
}
}
The view is loaded using loadNibNamed method.
- (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass {
if (nibName && objClass) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil];
for (id currentObject in objects ){
if ([currentObject isKindOfClass:objClass])
return currentObject;
}
}
return nil;
}
I have checked the userInteractionEnabled property for View and Button and both are set to true.
Also mapped the showMessage with Touch Up Inside event of button.
any suggestion on how I go about identifying this problem?
Try using the following static method to get TestView
+ (TestView *)getNewTestView {
TestView *view = nil;
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"TestView" owner:nil options:nil];
for (NSObject *obj in xib) {
if ([obj isKindOfClass:[TestView class]]) {
view = (TestView *)obj;
}
}
return view;
}
And then, to get an instance of the object, use:
TestView *testView = [TestView getNewTestView];
[self.view addSubview:testView];
Please make sure your MKAnnotationView has enough space to fill testView in.
If it's smaller than testView, the outer space would not respond to any actions.
You can set clipsToBounds to NO to check out the actual space.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if(![view.annotation isKindOfClass:[MKUserLocation class]]) {
view.clipsToBounds = NO;
CGRect rect = CGRectMake(0,0,268,140);
TestView *testview = [[TestView alloc] initWithFrame:rect];
testview = [self loadNibNamed:#"TestView" ofClass:[TestView class]];
[view addSubview:testview];
}
}

UIView Subclass with a delegate using NIB

I am trying to subclass a UIView using the nib. Using the following code:
- (void)awakeFromNib
{
[super awakeFromNib];
NSArray *v = [[NSBundle mainBundle] loadNibNamed:#"Qus_Scale1to7View" owner:self options:nil];
[self addSubview:[v objectAtIndex:0]];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSArray *v = [[NSBundle mainBundle] loadNibNamed:#"Qus_Scale1to7View" owner:self options:nil];
[self addSubview:[v objectAtIndex:0]];
}
return self;
}
this creates the object correctly and view also displayed and when the object loads from its nib the delegate instantly becomes null and ignores any attempt to assign values to it.
Can anyone know the reason why it is?
Thanks in advance.
It won't work reusing same xib for multiple view controllers. If you want to reuse that view make a class that inherits from UIView and add the code there.
#import "SomeProtocol.h"
#interface MyCustomView : UIView {
IBOutlet UIView *headerView;
IBOutlet UIView *footerView;
IBOutlet UIButton *updateBtn;
}
#property (nonatomic, assign) id<SomeProtocol> delegate;
#end
............
#implementation BCFirmwareView
#synthesize delegate = _delegate;
+ (id)viewFromNibWithName: (NSString*)name {
UIView *view = nil;
NSArray *views = [[NSBundle mainBundle] loadNibNamed: name owner: self options: nil];
if (views) {
for (UIView *aView in views) {
if ([aView isKindOfClass: NSClassFromString(name)])
view = aView;
}
}
return view;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder: aDecoder];
if (self) {
}
return self;
}
- (id)init {
self = [[MyCustomView viewFromNibWithName: #"MyCustomView"] retain];
if (self) {
}
return self;
}
- (void)dealloc {
self.delegate = nil;
[headerView release];
[footerView release];
[updateBtn release];
[super dealloc];
}
- (void)awakeFromNib {
[super awakeFromNib];
// Do any additional setup after loading the view from its nib.
headerView.backgroundColor = [UIColor redColor];
footerView.backgroundColor = [UIColor greenColor];
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview: newSuperview];
if (!newSuperview)
return;
}
- (void)didMoveToSuperview {
[super didMoveToSuperview];
}
- (IBAction)updateBtnPressed: (id)sender {
// do some stuff
}
#end
The next step is to open the xib in Interface Builder and set your class as the custom class for the view, not for the File's Responder. Right click on the view and make the outlet and actions connections.
Now you should be able to simply make instances of your MyCustomView in any view controller and use it. Will work from Interface Builder as well if you don't forget to change your view custom class to your class.
You can create a custom UIView with Xib and add properties to it.
Then you link the class with the xib and link the properties with the IB.
Or you can only use the
NSArray *v = [[NSBundle mainBundle] loadNibNamed:#"Qus_Scale1to7View" owner:self options:nil];
UIView *view = [v objectAtIndex:0];
and set your objects values using viewWithTag: method.
UILabel *label = (UILabel *)[view viewWithTag:yourTag];
Let me know if this helps.

UIControl subclass setHighlighted is not called

I have subclassed UIControl to create my own class. I am trying to override setHighligheted: but for some reason it doesn't get called...
I have double checked and none of my subviews have userInteractionEnabled = NO. Are there any other methods that I should override?.
Thank you!
Here is some sample code:
#import "Booking.h"
#interface BookingCloud : UIControl
// Booking
#property(nonatomic, strong) Booking *booking;
// BackgroundView
#property (strong, nonatomic) IBOutlet UIView *backgroundView;
And the implementation
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setUpSubViews];
}
return self;
}
- (void)setUpSubViews {
[[NSBundle mainBundle] loadNibNamed:#"BookingCloud" owner:self options:nil];
[self addSubview:backgroundView];
self.frame = backgroundView.frame;
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:#"background_color"]] colorWithAlphaComponent:0.8];
} else {
backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:#"background_color"]] colorWithAlphaComponent:1];
}
}

Resources