In my app I have a custom Google Map pin marker designed in a Nib file. Part of the pin marker will display an estimated arrival time.
Here is how I am loading the Nib, and attempting to set a custom time:
self.markerPinFromNib = [[[NSBundle mainBundle] loadNibNamed:#"PinWithTimeView" owner:self options:nil] firstObject];
self.markerPinFromNib.estTime.text = #"20";
When I run this, I get an error:
-[UIView estTime]: unrecognized selector sent to instance
PinWithTimeView.h
#import <UIKit/UIKit.h>
#interface PinWithTimeView : UIView
#property (nonatomic, weak) IBOutlet UIImageView *rotateCircle;
#property (nonatomic, strong) IBOutlet UILabel *estTime;
#end
Can someone point out what I'm doing wrong?
In your PinWithTimeView.h
#interface PinWithTimeView : UIView
#property (nonatomic, weak) IBOutlet UIImageView *rotateCircle;
#property (nonatomic, weak) IBOutlet UILabel *estTime;
-(id)initWithEstTime:(NSString*)time;
#end
In your PinWithTimeView.m
-(id)initWithEstTime:(NSString*)time{
self = [super init];
if (self) {
self = [[[NSBundle mainBundle] loadNibNamed:#"PinWithTimeView" owner:self options:nil] objectAtIndex:0];
self.estTime.text = time;
}
return self;
}
In your controller
self.markerPinFromNib = [[PinWithTimeView alloc]initWithEstTime:#"20"];
You should convert the instance from nib to PinWithTimeView in force. Like this:
((PinWithTimeView *)self.markerPinFromNib).estTime.text = #"20";
Because loadNibNamed:owner:options: will return a array include UIView instances.
Related
The problem is View is loaded but the inner elements are not loaded or did not appear why?
the xib image is:
and the corresponding .h is:
#import <UIKit/UIKit.h>
#interface DownloadView : UIView
#property (strong, nonatomic) IBOutlet UIView *view;
#property (weak, nonatomic) IBOutlet UILabel *downloadFilename;
#property (weak, nonatomic) IBOutlet UILabel *downloadpercentage;
#property (weak, nonatomic) IBOutlet UIProgressView *downloadprogressBar;
#property (weak, nonatomic) IBOutlet UIButton *downloadCancel;
#end
.m file is:
#import "DownloadView.h"
#implementation DownloadView
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[[NSBundle mainBundle] loadNibNamed:#"DownloadView" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
-(id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
[[NSBundle mainBundle] loadNibNamed:#"DownloadView" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
The calling file is:
polygonView = [[DownloadView alloc]initWithFrame:CGRectMake(0, height-170, width, 100)];
polygonView.downloadFilename.text = #"Initiating Download...";
[polygonView.downloadprogressBar setProgress:0];
polygonView.downloadpercentage.text = #"0%";
[window addSubview:polygonView];
What is the Problem ?
Debugger update:
The output is:
Unfortunately you can't create custom UIView like this, it will not work. I use different way for reusable custom components which is:
Create new view controller in the storyboard, change its frame, and customize it.
Give it an identifier
In all places you want to use in storyboard, just create Container view and embed your custom view controller.
If you want to use it programmatically. Just instantiate it from storyboard with the identifier. Resize its view frame and add it as a subview.
I have a custom UIView which was build with xib that have a file owner called CustomModuleUIView which contains labels and buttons . I have called this custom view in my Rootviewcontroller and I succeeded to display it using initWithCoder method. The problem is that I can't change the default text of UILabel neither from customMouleUIView nor from the root ViewController. I found example that tells me to do custom initialisation in in initWithCoder but it doesn't work for me and nothing changes and without any error it displays the default text.
This is my custom UIView xib
This is my root view controller
This is my code oh custom UIView .h
#import <UIKit/UIKit.h>
#interface ModuleCustomUIView : UIView
-(void) setup;
#property (weak, nonatomic) IBOutlet UIImageView *_moduleIcon;
#property (retain, nonatomic) IBOutlet UILabel *_moduleName;
- (IBAction)openDemo:(id)sender;
- (IBAction)close:(id)sender;
#property (weak, nonatomic) IBOutlet UIImageView *_moduleImage;
#property (strong, nonatomic) IBOutlet UILabel *_positionLabel;
#end
code of .m , i use setup method to init my UIView because I couldn't call
[[NSBundle mainBundle] loadNibNamed:xib owner:self options:nil] ;
inside initWithCoder that causes infinite loop .
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
}
return self;
}
-(void) setup
{
NSString *xib= #"CustomUIView";
NSArray *array=[[NSBundle mainBundle] loadNibNamed:xib owner:self options:nil] ;
UIView *view =[array objectAtIndex:0];
//code that doesn't work
[_positionLabel setText:#"hello world"];
//
[self addSubview:view];
}
this is my root view controller .h
- (void)viewDidLoad
{
[super viewDidLoad];
[_moduleCustomView setup];
[self.view addSubview:_moduleCustomView];
//code doesn't work
[_moduleCustomView setText:#"hello world"];
}
even in the did load I can't change the text
I have found my mistake , it's about file owner , i have change it in the inspector but by selecting the uiview and not te file's owner , i change NSObject to my class name and reconnect the label .
I guess the Files Owner attribute should be your 'root viewcontroller'.
I created Custom UIView with bunch of labels. label1, label2,
UIView *testContentView = [[[UINib nibWithNibName:#"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];
How do I access the label and setting text ?
You need to define them on your interface.
#interface TestContentView : UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#end
And then...
testContentView.label1.text = #"foo";
testContentView.label2.text = #"bar";
Edit
Added IBOutlet to the properties since you are using a NIB, you will also need to wire these up in Interface Builder.
In order to access your custom properties, you need to cast the UIView to a TestContentView.
TestContentView *testContentView = (TestContentView *)[[[UINib nibWithNibName:#"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];
I need an example of how to instantiate a custom view from a nib file. i wrapped my head around several stackoverflow posts but i didn't get it. Maybe i only need a hint how to do it.
what I want to do is - creating a custom view with interface builder. it should be a template so that i can instantiate it multiple times to add it to multiple view controllers.
So far I have created my custom view xib file called MyIssueView.xib. Which consists in fact just of the main view and some labels.
And I created a Subclass of UIView which is called IssueView with the Outlets for my labels in the MyIssueView.xib.
How do I now hook up the outlets with the IB? And how can I instantiate the IssueView from any ViewController?
Would be glad for an example!
Cheers.
UPDATE:
I now have
IssueView.xib
IssueView.h (UIView Subclass)
IssueView.m
My IssueView.h:
#import <UIKit/UIKit.h>
#interface IssueView : UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#property (weak, nonatomic) IBOutlet UILabel *label3;
#property (weak, nonatomic) IBOutlet UILabel *label4;
#end
My IssueView.m:
#import "IssueView.h"
#implementation IssueView
#end
My ViewController.m:
#import "AllIssuesViewController1.h"
#import "IssueView.h"
#import "UIView+NibLoading.h"
#interface AllIssuesViewController1 ()
#end
#implementation AllIssuesViewController1
- (void) loadView
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
_issueView = [IssueView loadInstanceFromNib];
}
It gives me an error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x8292580> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label1.'
I have a category that extends UIView called UIView+NibLoading.
In it is this code...
#import "UIView+NibLoading.h"
#implementation UIView (NibLoading)
+ (id)loadInstanceFromNib
{
UIView *result;
NSArray* elements = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id anObject in elements) {
if ([anObject isKindOfClass:[self class]]) {
result = anObject;
break;
}
}
return result;
}
#end
Then when I want to instantiate a UIView subclass from a nib I just import the category and I can run...
self.issueView = [IssueView loadInstanceFromNib];\
You need to connect the labels like this...
If you connecting the labels to "File's Owner" then you'll need to remove those connections.
I'm newbie in the iOS development and I'm working in a projecte that it uses iOS5 and Storyboarding. I would like to create a reusable component, like a button, with a custom view (inside the button there will be some images and labels, it will be a big button), to create some of them in the same view of a Storyboard.
So I've created a XIB file (ItemClothes.xib) with a UIButton and inside of it some UIImagesViews and a UILabel. Then I've created a subclass of UIButton (UIItemClothes.h and UIItemClothes.m), with properties for UIImageViews and UILabel components.
UIItemClothes.h
#import <UIKit/UIKit.h>
#import "SCClothes.h"
#interface UIItemClothes : UIButton
#property (nonatomic, strong) IBOutlet UIImageView *imageClothes;
#property (nonatomic, strong) IBOutlet UIActivityIndicatorView *loadingImage;
#property (nonatomic, strong) IBOutlet UIImageView *seasonIconClothes;
#property (nonatomic, strong) IBOutlet UIImageView *categoryIconClothes;
#property (nonatomic, strong) NSString *idClothes;
#property (strong, nonatomic) IBOutlet UILabel *lblProva;
#end
UIItemClothes.m
#import "UIItemClothes.h"
#implementation UIItemClothes
#synthesize imageClothes = _imageClothes;
#synthesize loadingImage = _loadingImage;
#synthesize seasonIconClothes = _seasonIconClothes;
#synthesize categoryIconClothes = _categoryIconClothes;
#synthesize idClothes = _idClothes;
#synthesize lblProva = _lblProva;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"ItemClothes" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
#end
Then in the .XIB file I've set the class of UIButton as UIItemClothes, and make the relationships between XIB's UI components and my class properties.
So, after that, in the Storyboard, in the ViewController of one view I've written this code:
UIItemClothes *item = [[UIItemClothes alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 200.0)];
item.lblProva.text = #"test!";
[item.categoryIconClothes setImage:iconCategory];
item.seasonIconClothes.image = iconSeason;
[cell addSubview:item];
As you see, this component will be inside a TableViewCell, and the idea is to put more components (UIItemClothes) inside of it.
The problem is that the component is drawed but any outlet is set as I do in the code above.
Can anyone help me?
Thanks!
well, the problem has been resolved... Instead of having a custom subclass of UIButton, there will be needed a ViewController with all Outlets. Then in the other ViewController (StoryBoard) is initialized this new ViewController