开发者

place xib in UIView control iphone

开发者 https://www.devze.com 2023-03-18 15:16 出处:网络
I know how to place a nib file in the \"main\" view controller: when I execute this method a new nib file will be displayed.

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

place xib in UIView control iphone

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?

place xib in UIView control iphone

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.

place xib in UIView control iphone

I hooked up my IBoutlet UIView *exampleView to the base view:

place xib in UIView control iphone

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

place xib in UIView control iphone

that view.

I have tried placing them in pgBackground.m but it 开发者_JS百科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.


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.


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.


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.


-(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;

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号