0

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 *
Simon Davies
  • 3,668
  • 9
  • 41
  • 69

2 Answers2

2

Yes, you can do that easily, for example, using properties.

  1. In header file of TitleBlock (or pageTitleView) class in @interface section before @end you should define property:

    @property (nonatomic, retain) UILabel pageTitle;
    

    or for ARC-projects

    @property (nonatomic, strong) UILabel pageTitle;
    
  2. 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];
    
  3. 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.

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • i have most of the above and then I have tried the last bit but getting the error a the _PageTitleBlock.pageTitle.text = @"Text of page title label"; section getting the following error Property 'pageTitle' not found on object of type 'UIView *' – Simon Davies Oct 16 '12 at 19:27
  • Is `_PageTitleBlock` instance of class with that property? – Nekto Oct 16 '12 at 19:52
  • +1 Nekto is right. By using properties you can expose the label in order to modify it. One other (uglier) way to achieve it would be to grab the label from the `subviews` array: `UILabel *pageTitle = [[_PageTitleBlock subViews] lastObject];` – Alladinian Oct 16 '12 at 20:56
  • 1
    @SimonDavies do you understand Object-Oriented Programming? – Nekto Oct 16 '12 at 21:59
  • yes i do understand OOP i have updated the question with more of the code for understanding, hopt this helps, sorry – Simon Davies Oct 17 '12 at 20:27
  • @SimonDavies you really didn't give chance for me to answer and accepted other answer when it become more clearly what you wanted? Guy, I won't answer on any of your questions. – Nekto Oct 17 '12 at 21:04
  • @Nekto apologies but am working on this and like yours tried the answer, again apologies that is all I can offer – Simon Davies Oct 17 '12 at 21:16
1

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!

fguchelaar
  • 4,779
  • 23
  • 36