xCode: Passing a Label text into a custom added subview
-
07-07-2021 - |
Question
I have rewritten this to try and make it more descriptive with more code hopefully:
I have set up a separate UIView
class called: pageTitleView code below:
header file:
#import <UIKit/UIKit.h>
@interface pageTitleView : UIView
@property (nonatomic, strong) IBOutlet UILabel *pageTitle;
@property (nonatomic, strong) IBOutlet UILabel *subTitle;
@end
The M File:
@implementation pageTitleView
@synthesize pageTitle;
@synthesize subTitle;
- (id)initWithFrame:(CGRect)frame{
pageTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,labelPosY,300,20)];
pageTitle.textColor = txtColour;
pageTitle.backgroundColor = [UIColor clearColor];
pageTitle.textAlignment = NSTextAlignmentCenter;
pageTitle.font = [UIFont systemFontOfSize:14];
// pageTitle.text to be set by parent view
[self addSubview:pageTitle];
}
With in my parent view controller I have the following:
The Header File:
#import <UIKit/UIKit.h>
@interface UsingThisGuide : UIViewController
@property (strong, nonatomic) UIView *PageTitleBlock;
@property (strong, nonatomic) UIWebView *dispalyPageContent;
@property (strong, nonatomic) UIView *FooterBar;
@end
In the M file i have the following:
#import <QuartzCore/QuartzCore.h>
#import "pageTitleView.h"
#import "MyFirstView.h"
@interface MyFirstView ()
@end
@implementation MyFirstView {
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
_PageTitleBlock = [[pageTitleView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
_PageTitleBlock.layer.cornerRadius = 5;
_PageTitleBlock.pageTitle.text = @"This should work but not";
[self.view addSubview:_PageTitleBlock];
[super viewDidLoad];
}
@end
No what i want to do is set the pageTitle.text
from the pageTitleView
class via its parent controller MyFirstView
using something like _PageTitleBlock.pageTitle.text=
but this is the error i am getting:
Property 'pageTitle' not found on object of type 'UIView *
Solution
The problem here, is that you are declaring your property called PageTitleBlock
of type UIView
in UsingThisGuide
, but then call pageTitle
which is declared in the class pageTitleView
. The compiler doesn't trust this.
Change the type of PageTitleBlock
from UIView
to pageTitleView
and you're good to go!
OTHER TIPS
Yes, you can do that easily, for example, using properties
.
In header file of
TitleBlock
(orpageTitleView
) class in@interface
section before@end
you should define property:@property (nonatomic, retain) UILabel pageTitle;
or for ARC-projects
@property (nonatomic, strong) UILabel pageTitle;
In parent view controller you should initiate
_PageTitleBlock
with frame:_PageTitleBlock = [[pageTitleView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; // specify needed frame // and add it to root view: [self.view addSubview:_PageTitleBlock];
Now you could access
pageTitle
property:_PageTitleBlock.pageTitle.text = @"Text of page title label";
Hope it will help you.
P.S. For class names it is better to use сapitalized names, i.e., PageTitleView
rather then pageTitleView
.