[英]loaded custom xib UIView, how do I get it to expand to full screen from Interface Builder and to access methods in other controllers?
Hello all I have 2 basic questions. 大家好,我有2个基本问题。 I just learnt how to load a custom xib for the first time from code via ([[NSBundle mainBundle] loadNibNamed).
我刚刚学习了如何通过([[NSBundle mainBundle] loadNibNamed)从代码中首次加载自定义xib。
At the moment the xib's size is kept to 600,600 on every device. 目前,每个设备上的xib大小保持在600,600。 I was hoping I'd be able to make the settings changes in Interface Builder Simulation Size metrics setting etc I have the xib's Simulated Metrics Size on "Inferred" but it does not seems to make a difference what I set it to.
我希望我能够在Interface Builder Simulation Size指标设置中进行设置更改,等等。我在“推断”上设置了xib的Simulated Metrics Size,但似乎并没有改变我的设置。
GameScene.m
#import "GameScene.h"
#import "controlsViewController.h"
-(void)didMoveToView:(SKView *)view {
//other game stuff
//load xib
[super view];
controlsViewController *ControlsViewController = [[controlsViewController alloc] init];
[self.view addSubview:ControlsViewController];
//position on screen in the center
ControlsViewController.center = CGPointMake(CGRectGetMidX(view.frame),
CGRectGetMidY(view.frame));
}
controlsViewController.m
#import "controlsViewController.h"
@implementation controlsViewController
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@"controlsViewController" owner:self options:nil];
NSLog(@"CVC Frame Size: %@", NSStringFromCGRect(self.view.bounds)); //keeps showing
self.bounds = self.view.bounds;
[self addSubview:self.view];
}
return self;
}
controlsViewController.h
#import <UIKit/UIKit.h>
@interface controlsViewController : UIView
@property (strong, nonatomic) IBOutlet UIView *view;
@end
Any help would be great 任何帮助都会很棒
For the first question you can get the screen size of a device by calling; 对于第一个问题,您可以通过调用获取设备的屏幕尺寸。 [[UIScreen mainScreen] bounds];
[[UIScreen mainScreen]范围]; this returns a CGRect.
这将返回一个CGRect。
For the second question you cannot link an action in one view controller to a button in another. 对于第二个问题,您无法将一个视图控制器中的操作链接到另一个视图控制器中的按钮。 This is because the first view controller may not be instantiated (Ie Around in memory) when the second one is.
这是因为当第二个视图控制器未实例化时,它可能未实例化(即在内存中)。 You could use a singleton class to share resources between view controllers or you could use the responder chain.
您可以使用单例类在视图控制器之间共享资源,也可以使用响应者链。
You need to get used to the fact that view controllers are isolated and there's no getting around that for good reason and therefore most apps use singletons to share data globally around the app. 您需要适应以下事实:视图控制器是隔离的,并且没有充分的理由解决这个问题,因此大多数应用程序使用单例在应用程序周围全局共享数据。
Read the best practices for programming amd making apps documentation by Apple 阅读Apple编写和编写应用程序文档的最佳实践
You'd better to make a custom view class for a view's xib. 您最好为视图的xib创建自定义视图类。
Create these files. 创建这些文件。
MYControlsView.m MYControlsView.m
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
UINib *nib = [UINib nibWithNibName:@"MYControlsView" bundle:nil];
UIView *viewFromXib = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[self addSubview:viewFromXib];
}
return self;
}
You place MyControlsView on ViewController on Storyboard or xib. 您将MyControlsView放在Storyboard或xib上的ViewController上。 And you set constraints to fill ViewController's view.
然后设置约束以填充ViewController的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.