iOS5 添加和操作 UIView 类

发布于 2025-01-01 09:02:29 字数 1791 浏览 3 评论 0原文

我创建了一个 UIVeiw 类和一个 .xib。在此 .xib 视图中,我将其设置为尺寸为 400x200 的自由格式,并将其分配给具有相同名称的自定义类:

Storyboard: blogView 类文件:blogView.h & blogView.m

在 .xib 中,我添加了一个标签和一个文本字段,并将它们链接到 .h 文件等中的变量(参见下面的代码)。

blogCont.h

  #import <UIKit/UIKit.h>

  @interface blogCont : UIView
  @property (strong, nonatomic) IBOutlet UILabel *lbBlogDate;
  @property (strong, nonatomic) IBOutlet UITextView *txtBlogTitle;

  @end

blogCont.m

  #import "newsStoryView.h"

  @implementation blogCont
  @synthesize lbBlogDate;
  @synthesize txtBlogTitle;

  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
          // Initialization code place a log to see if it loads
          NSLog(@"View Loaded");


      }
      return self;
  }
  @end

现在,在我的主 viewController.m 文件中,我添加了以下代码来初始化此视图类,并且添加了背景颜色以查看它是否加载。

viewController.m

 UIView *blogCont = [[blogView alloc] init];
 blogCont.backgroundColor = [UIColor purpleColor];
 [subview addSubview:blogCont];

现在,当我运行它时一切正常,但由于我没有看到紫色背景,看起来好像视图没有加载,但在日志中我确实看到了此视图中的 NSLog 消息 NSLog(@"View Loaded");所以看起来它正在启动这个,但我一生都不能让它显示出来?

现在,如果我将代码稍微更改为我的主 View Controller.m 字段:

 CGRect blogFrame;
 blogFrame.origin.x = 20;
 blogFrame.origin.y = 20;
 blogFrame.size = CGRectMake(400,200);;


 newsStoryView *blogCont = [[blogView alloc] blogFrame];
 blogCont.backgroundColor = [UIColor purpleColor];
 [subview addSubview:blogCont];

然后我的视图显示一个漂亮的紫色框,因此当我设置帧大小并使用“blogFrame”初始化视图时,会显示此内容,但是我认为所有这些都会在 .xib 设置中设置,所以不需要这样做?

那么我如何创建这个外部视图类并将其分配到另一个视图中,然后操作其数据,因为使用 blogCont.lbBlogDate.text 访问 .xib 中的标签似乎不起作用,它可能会起作用,但因为我无法查看我无法确认。

我做错了什么?

谢谢

I have create a UIVeiw class and a .xib. Within this .xib view I have its set to freeform with the dimensions of 400x200 and I have assigned it to my custom class with the same name:

Storyboard: blogView
Class Files: blogView.h & blogView.m

Within in the .xib i have added a label and a text field and linked them up to variable within the .h files etc (See code below).

blogCont.h

  #import <UIKit/UIKit.h>

  @interface blogCont : UIView
  @property (strong, nonatomic) IBOutlet UILabel *lbBlogDate;
  @property (strong, nonatomic) IBOutlet UITextView *txtBlogTitle;

  @end

blogCont.m

  #import "newsStoryView.h"

  @implementation blogCont
  @synthesize lbBlogDate;
  @synthesize txtBlogTitle;

  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
          // Initialization code place a log to see if it loads
          NSLog(@"View Loaded");


      }
      return self;
  }
  @end

Now with in my main viewController.m file i have added the following code to init this view class, and I have added a background colour to see if this loads in.

viewController.m

 UIView *blogCont = [[blogView alloc] init];
 blogCont.backgroundColor = [UIColor purpleColor];
 [subview addSubview:blogCont];

Now when I run this it all works well but as I do not see the purple background it looks as if the view does not load, but within the log I do see the NSLog message I have within this view NSLog(@"View Loaded"); so it seems it initiating this, but I cannot for the life of me get this to display?

Now if I change the code slightly to my main View Controller.m fiel to:

 CGRect blogFrame;
 blogFrame.origin.x = 20;
 blogFrame.origin.y = 20;
 blogFrame.size = CGRectMake(400,200);;


 newsStoryView *blogCont = [[blogView alloc] blogFrame];
 blogCont.backgroundColor = [UIColor purpleColor];
 [subview addSubview:blogCont];

Then I get my view display a nice purple box, so this shows up when I set a frame size and the init the view with it 'blogFrame', bu tI thought that all this would be set within the .xib settings so no need to do this?

SO how can I create this external view class and assign it into another view and then manipulate its data, as accessing the label in the .xib using blogCont.lbBlogDate.text does not seem to work that is it probably does but as I cannot view it i cannot confirm it.

What am i doing wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神回复 2025-01-08 09:02:29

似乎我几乎回答了我自己的问题,然后做了:

我没有在单独的类视图中设置大小,我在初始化时要求一个大小:

  - (id)initWithFrame:(CGRect)frame

这是要求一个大小

,以便我可以对上面执行以下操作:

  - (id)init
{
    self = [super initWithFrame:CGRectMake(0, 0, 478, 220)];
  .... rest of code

设置大小在视图负载内。

但我也可以在主视图控制器中初始化它时设置它,如下所示:

 newsStoryView *blogCont = [[blogView alloc]  initWithFrame:CGRectMake(0, 0, 400, 200)];

这更好,因为我可以控制每个视图的位置。希望这对任何人都有帮助

Seems I nearly answered my own question then did:

I was not setting the size within my separate class view I was asking for a size when init it:

  - (id)initWithFrame:(CGRect)frame

this is asking for a size

so I could do the following to the above:

  - (id)init
{
    self = [super initWithFrame:CGRectMake(0, 0, 478, 220)];
  .... rest of code

Setting the size within the view load.

But I could also set it when I init it in my main view controller as below:

 newsStoryView *blogCont = [[blogView alloc]  initWithFrame:CGRectMake(0, 0, 400, 200)];

This is better as I can control the position of each one. Hope this helps anyone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文