简体   繁体   English

将xib放入UIView控制iphone

[英]place xib in UIView control iphone

I know how to place a nib file in the "main" view controller:我知道如何在“主”视图 controller 中放置一个 nib 文件:

在此处输入图像描述

when I execute this method a new nib file will be displayed.当我执行此方法时,将显示一个新的 nib 文件。

Is it possible to place that nib file in a UIView controller instead?是否可以将该笔尖文件放在 UIView controller 中?

在此处输入图像描述

I want to place that nib file in that view controller instead of in the main view.我想将该 nib 文件放在该视图 controller 而不是主视图中。 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.我使用您提到的属性和方法创建了另一个视图 controller。

在此处输入图像描述

I hooked up my IBoutlet UIView *exampleView to the base view:我将IBoutlet UIView *exampleView连接到基本视图:在此处输入图像描述

then from here I am lost I don't know where to place the other methods.然后从这里我迷路了,我不知道在哪里放置其他方法。 I would like to place that exampleView into:我想将该 exampleView 放入:

在此处输入图像描述

that view.那个观点。

I have tried placing them in pgBackground.m but it does not work.我尝试将它们放在 pgBackground.m 中,但它不起作用。 I want to place them inside an IBaction so that I can load that with a touch up inside event with a button.我想把它们放在一个 IBaction 中,这样我就可以通过一个按钮的内部事件来加载它。

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

If I follow I think you will need to:如果我遵循,我认为您将需要:

Create a UIView sub class with associated xib创建具有关联 xib 的UIView子 class

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:ExampleView.m ,您需要添加:

- (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.您从 nib 使用 awake,因此您可以在 Interface Builder 中使用这个子 class。 If you also want to instantiate it in code you need to hook up the xib in the init method.如果您还想在代码中实例化它,则需要在 init 方法中连接 xib。 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.在 ExampleView.xib 文件中,确保将您的IBOutlet UIView *exampleView连接到基本视图。

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.现在,在您想要使用自定义视图的任何 xib 文件中,只需将其拖入 window 并将 class 更改为您的子类。 This is how I got it working I look forward to some people suggesting improvements.这就是我让它工作的方式,我期待一些人提出改进建议。

-(IBAction)btnSubmit_click{ -(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 { } -(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {

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

} }

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.如果你想改变方向,那么首先你在 app 委托文件中声明一个 bool en 类型变量,然后在 app delegate.m 文件中定义如果方向改变,然后检测并让 Boolean 类型变量为 true 为横向,为 false 为纵向。 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.并且您希望您在主视图 controller 中执行 nib,然后您有两种方式,一种是动态的,另一种是通过界面生成器。 in interface builder u Select navigation controller and drop in main window file.在界面构建器 u Select 导航 controller 并放入主 window 文件。 after that u change the view of navigation controller's view by press cmd+4 and select view which u want to display first.之后,您通过按 cmd+4 和 select 视图更改导航控制器视图的视图,您首先要显示这些视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM