0

我已经重写了它以尝试使用更多代码使其更具描述性:

我设置了一个单独的UIView类,名为:pageTitleView 代码如下:头文件:

 #import <UIKit/UIKit.h>

 @interface pageTitleView : UIView
    @property (nonatomic, strong) IBOutlet UILabel *pageTitle;
    @property (nonatomic, strong) IBOutlet UILabel *subTitle;
 @end

M 文件:

 @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];
  }

在我的父视图控制器中,我有以下内容:

头文件:

  #import <UIKit/UIKit.h>

   @interface UsingThisGuide : UIViewController

     @property (strong, nonatomic) UIView *PageTitleBlock;
     @property (strong, nonatomic) UIWebView *dispalyPageContent;
     @property (strong, nonatomic) UIView *FooterBar;

   @end

在 M 文件中,我有以下内容:

  #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

不,我想做的是通过其父控制器使用类似的东西pageTitle.text从类中设置,但这是我得到的错误:pageTitleViewMyFirstView_PageTitleBlock.pageTitle.text=

  Property 'pageTitle' not found on object of type 'UIView *
4

2 回答 2

2

是的,您可以轻松地做到这一点,例如,使用properties.

  1. TitleBlock(或pageTitleView)类的头文件中,您应该定义属性@interface之前的部分:@end

    @property (nonatomic, retain) UILabel pageTitle;
    

    或用于 ARC 项目

    @property (nonatomic, strong) UILabel pageTitle;
    
  2. 在父视图控制器中,您应该_PageTitleBlock使用frame启动:

    _PageTitleBlock = [[pageTitleView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; // specify needed frame
    // and add it to root view:
    [self.view addSubview:_PageTitleBlock];
    
  3. 现在您可以访问pageTitle属性:

    _PageTitleBlock.pageTitle.text = @"Text of page title label";
    

希望它会帮助你。

PS 对于类名,最好使用大写名称,即,PageTitleView而不是pageTitleView.

于 2012-10-15T19:26:35.663 回答
1

这里的问题是,您要声明您的属性称为inPageTitleBlock类型,然后调用在 class 中声明的属性。编译器不相信这一点。UIViewUsingThisGuidepageTitlepageTitleView

PageTitleBlock将from的类型更改为UIViewto pageTitleView,您就可以开始了!

于 2012-10-17T20:40:49.543 回答