简体   繁体   English

加载了自定义的xib UIView,如何将其从Interface Builder扩展到全屏并访问其他控制器中的方法?

[英]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。

  1. Just wondering how do I get the xib to expand to full screen on every device it goes on (iPad/iPhone etc)? 只是想知道如何使xib在其运行的每台设备(iPad / iPhone等)上扩展到全屏?

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
  1. Also I have a button in this xib file that I'd like to be connected to a method that does not belong to its own controller controlsViewController.m but to run a method that is inside GameScene.m. 另外,我在此xib文件中有一个按钮,希望将其连接到不属于其自己的控制器controlsViewController.m的方法,但要运行GameScene.m内部的方法。 How do I get it to be able to run a method there? 我如何获得能够在其中运行方法的信息? I know I can easily drag from the xib to controlsViewController.m to create an action but it won't drag to GameScene.m as expected. 我知道我可以轻松地从xib拖动到controlsViewController.m来创建一个动作,但不会像预期的那样拖动到GameScene.m。 How do I get around that? 我该如何解决?

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.xib MYControlsView.xib
  • MYControlsView class file(MYControlsView.m/h) MYControlsView类文件(MYControlsView.m / h)

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.

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