i have created a UIView .xib with some labels and a button. I connected all the outlets and the IBAction for the button.
When i add the .xib subview from my viewcontroller it shows ok, but the button is not working.
This is my xib´s .m file
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
[self load];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
[self load];
}
return self;
}
-(void)load
{
[[self superview] setUserInteractionEnabled:YES];
[self setUserInteractionEnabled:YES];
UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:#"AgregarAlCarrito" owner:self options:nil] firstObject];
[self addSubview:view];
view.frame = self.bounds;
}
i have tried everything you can imagine without results
thanks
So this line looks very suspect to me:
[self addSubview:view];
You should be adding the xib view to the parent view. Where I did something like this in one of my apps, I had something like this:
[[self view] addSubview:_xibView];
Related
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];
........
}
I have UITableView is Storyboard.
I try to create UITableViewCell in Xib and register it in UITableView as that:
-(void)viewDidLoad{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:#"FlipperTableViewCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:#"fliperCell"];
}
It work perfectly.
But this cell must have one view inside and flip it to another view when button on first view pressed.
I create another two views in same xib.
File's owner is FliperTableViewCell class. Class of first view is FliperTableViewCell.
But now a can't figure out how to add second view (UIView) as subview in first view (UITableViewCell), which loaded by table view.
I try to get second view from loadNibNamed:, but it became infinite cycle.
#implementation FlipperTableViewCell
-(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]){
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil] objectAtIndex:1]];
}
return self;
}
What is another way to get second view from same xib and add it as subview when first view loaded?
problem was solved when I replace
[self.tableview registerNib:nib forCellReuseIdentifier:#"FlipperTableViewCell"];
to
[self.tableview registerClass:[FlipperTableViewCell class] forCellReuseIdentifier:#"FlipperTableViewCell"];
then remove first view from xib and rewrite FlipperTableViewCell to initiate in code
#import "FlipperTableViewCell.h"
#interface TableViewCell ()
#property (nonatomic, strong) UIView *firstView;
#property (nonatomic, strong) UIView *secondView;
#end
#implementation FlipperTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
self.firstView = nibObjects[0];
self.secondView = nibObjects[1];
[self.contentView addSubview:self.firstView];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
// this is for test changing views
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (self.contentView.subviews[0] == self.firstView){
[self.firstView removeFromSuperview];
[self.contentView addSubview:self.secondView];
}
else{
[self.secondView removeFromSuperview];
[self.contentView addSubview:self.firstView];
}
}
#end
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];
}
}
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.
I am trying to create a custom UIView and have it load in a xib file. The custom UIView is called JtView and here is the code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(#"initWithFrame was called"); // this was called
if (self) {
// Initialization code
[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:NULL];
[self addSubview:self.view];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self addSubview:self.view];
}
In creating the xib file (File -> New -> File -> User Interface -> View), I deleted out the main Window and dragged a View from Xcode's object pallette. I tried alt-dragging to the header for JtView but this wasn't working. Do I need to create an association here or should I just leave what XCode created in place? (edit - see comments below for further clarification)
I have also added a UILabel to the xib.
However, when I run in simulator and set the background color to red, the label is not showing up. Do I need to create the UILabel as a reference in the custom UIView? Should I be deleting this or leaving it in place?
thx
edit 1
Here's a screenshot of the connections and the header file:
I solve it adding a class method to the UIView:
+ (id)customView;
+ (id)customView
{
CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:nil options:nil] lastObject];
// make sure customView is not nil or the wrong class!
if ([customView isKindOfClass:[CustomView class]])
return customView;
else
return nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CustomView *customView = [CustomView customView];
//And where you gonna use it add this line:
//this self.view refers to any view (scrollView, cellView...)
[self.view addSubview:customView];
}
I hope it´s gonna help
Try this:
-(void)awakeFromNib {
NSArray *obj = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil];
[self addSubview:obj[0]];
}
Or if you have an #property IBOutlet named "nibview" you could do:
-(void)awakeFromNib {
[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil];
[self addSubview:self.nibview];
}