将xib放入UIView控件iphone中

发布于 2024-11-19 00:10:50 字数 794 浏览 3 评论 0原文

我知道如何在“主”视图控制器中放置一个 nib 文件:

在此处输入图像描述

当我执行此方法时将显示新的 nib 文件。

是否可以将该 nib 文件放入 UIView 控制器中?

在此处输入图像描述

我想将该 nib 文件放置在该视图控制器中而不是主视图中。我怎么能这么做呢?


编辑:

我尝试做你提到的保罗,我不知道我做错了什么。无论如何,这就是我所做的:

我使用您提到的属性和方法创建了另一个视图控制器。

在此处输入图像描述

我将我的 IBoutlet UIView *exampleView 连接到基本视图: 在此处输入图像描述

然后从这里我迷失了,我不知道在哪里放置其他方法。我想将该 exampleView 放入:

在此处输入图像描述

该视图。

我尝试将它们放在 pgBackground.m 中,但它不起作用。我想将它们放置在 IBaction 中,以便我可以通过带有按钮的内部事件触摸来加载它。

I know how to place a nib file in the "main" view controller:

enter image description here

when I execute this method a new nib file will be displayed.

Is it possible to place that nib file in a UIView controller instead?

enter image description here

I want to place that nib file in that view controller instead of in the main view. How could I do that?


Edit:

I tried doing what you mention Paul I don't know what am I doing wrong. Anyways here is what I did:

I created another view controller with the properties an methods you mentioned.

enter image description here

I hooked up my IBoutlet UIView *exampleView to the base view:
enter image description here

then from here I am lost I don't know where to place the other methods. I would like to place that exampleView into:

enter image description here

that view.

I have tried placing them in pgBackground.m but it does not work. I want to place them inside an IBaction so that I can load that with a touch up inside event with a button.

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

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

发布评论

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

评论(4

飞烟轻若梦 2024-11-26 00:10:50

我设法将 nib 文件放置在 uiview 控件中,但我遇到的问题是,如果设备旋转,子视图将不会旋转。我目前正在研究解决方案,但到目前为止 这是一个问题,可以向您展示如何将另一个 nib 文件中的视图添加到 uiview 控件。

I managed to place a nib file in a uiview control but I have the problem that the subview will not rotate if the device does. I am currently working on a solution to this but so far here is a question that can show you how to add the view from another nib file to a uiview control.

眼睛会笑 2024-11-26 00:10:50

如果我遵循,我认为您将需要:

创建一个具有关联 xib 的 UIView 子类

ExampleView.h
ExampleView.m
ExampleView.xib

设置一个属性来包含视图层次结构(您想要在界面构建器中连接的所有内容)

ExampleView.h
@property (nonatomic, retain) IBOutlet UIView *exampleView;

ExampleView.m
@synthesize exampleView = _exampleView;

ExampleView.m 您需要添加:

- (void)awakeFromNib
{
  [super awakeFromNib];
  [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:self options:nil];
  [self addSubview:self.exampleView];
}

您使用 nib 唤醒,以便您可以在 Interface Builder 中使用此子类。如果您还想在代码中实例化它,则需要在 init 方法中连接 xib。在这种情况下,我可能会像这样提取笔尖加载:

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [self loadNibIntoView];
  }
  return  self;
}

- (void)awakeFromNib
{
  [super awakeFromNib];
  [self loadNibIntoView];
}

- (void)loadNibIntoView
{
  [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:self options:nil];
  [self addSubview:self.exampleView];
}

在ExampleView.xib文件中,确保将您的IBOutlet UIView *exampleView连接到基本视图。

现在,在您想要使用自定义视图的任何 xib 文件中,只需将其拖到窗口中并将类更改为您的子类即可。这就是我的工作方式,我期待有人提出改进建议。

If I follow I think you will need to:

Create a UIView sub class with associated xib

ExampleView.h
ExampleView.m
ExampleView.xib

Set up a property to contain the View hierarchy (everything you want to be hooked up in Interface builder)

ExampleView.h
@property (nonatomic, retain) IBOutlet UIView *exampleView;

ExampleView.m
@synthesize exampleView = _exampleView;

In the ExampleView.m you need to add:

- (void)awakeFromNib
{
  [super awakeFromNib];
  [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:self options:nil];
  [self addSubview:self.exampleView];
}

You use awake from nib so you can use this sub class in Interface Builder. If you also want to instantiate it in code you need to hook up the xib in the init method. In this case I would probably extract the nib loading like so:

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [self loadNibIntoView];
  }
  return  self;
}

- (void)awakeFromNib
{
  [super awakeFromNib];
  [self loadNibIntoView];
}

- (void)loadNibIntoView
{
  [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:self options:nil];
  [self addSubview:self.exampleView];
}

In the ExampleView.xib file make sure to hook up your IBOutlet UIView *exampleView to the base view.

Now in any xib file where you want to use your custom view simply drag it into your window and change the class to your subclass. This is how I got it working I look forward to some people suggesting improvements.

淡写薰衣草的香 2024-11-26 00:10:50

你能告诉我你想做什么吗?如果你想改变方向,那么首先你在应用程序委托文件中声明一个 bool en 类型变量,然后在应用程序 delegate.m 文件中定义,如果方向改变,然后检测并使布尔类型变量对于横向为 true,对于纵向为 false。此过程用于接下来的所有视图。并且您希望在主视图控制器中执行 nib,那么您有两种方法,一种是动态的,另一种是通过界面生成器执行的。在界面生成器中 u 选择导航控制器并放入主窗口文件中。之后,您可以通过按 cmd+4 更改导航控制器的视图,然后选择要首先显示的视图。

can u tell me what u want to do? if u want to change orientation then first u declare a bool en type variable in app delegate file then define in app delegate.m file if orientation change then detect and make Boolean type variable true for landscape and false for portrait. this process use for next all views. and u want u execute nib in main view controller then u have two ways one is dynamically and other by interface builder. in interface builder u Select navigation controller and drop in main window file . after that u change the view of navigation controller's view by press cmd+4 and select view which u want to display first.

往昔成烟 2024-11-26 00:10:50

-(IBAction)btnSubmit_click{

UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation]; 

if (orientation == UIDeviceOrientationLandscapeLeft) {
   // Do stuff
}
else if (orientation == UIDeviceOrientationLandscapeRight) {
    // Do stuff like  [self setCordinateLandscape];
}

}
-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) 接口方向
{

// Return YES for supported orientations
//set as per requirements ====>>  
return (interfaceOrientation == UIDeviceOrientationLandscapeRight);
// Or  return TRUE;

}

-(IBAction)btnSubmit_click{

UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation]; 

if (orientation == UIDeviceOrientationLandscapeLeft) {
   // Do stuff
}
else if (orientation == UIDeviceOrientationLandscapeRight) {
    // Do stuff like  [self setCordinateLandscape];
}

}
-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{

// Return YES for supported orientations
//set as per requirements ====>>  
return (interfaceOrientation == UIDeviceOrientationLandscapeRight);
// Or  return TRUE;

}

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