gpt4 book ai didi

objective-c - x代码 : Passing a Label text into a custom added subview

转载 作者:行者123 更新时间:2023-11-28 20:29:13 25 4
gpt4 key购买 nike

我重写了这个,希望用更多的代码让它更具描述性:

我已经设置了一个单独的 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];
}

在我的父 View Controller 中,我有以下内容:

头文件:

  #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

我不想做的是通过其父 Controller MyFirstViewpageTitleView 类设置 pageTitle.text 使用类似 _PageTitleBlock.pageTitle.text= 但这是我得到的错误:

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

最佳答案

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

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

    @property (nonatomic, retain) UILabel pageTitle;

    或用于 ARC 项目

    @property (nonatomic, strong) UILabel pageTitle;
  2. 在父 View Controller 中,您应该使用 frame 启动 _PageTitleBlock:

    _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";

希望对你有所帮助。

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

关于objective-c - x代码 : Passing a Label text into a custom added subview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12900188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com