簡體   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?

大家好,我有2個基本問題。 我剛剛學習了如何通過([[NSBundle mainBundle] loadNibNamed)從代碼中首次加載自定義xib。

  1. 只是想知道如何使xib在其運行的每台設備(iPad / iPhone等)上擴展到全屏?

目前,每個設備上的xib大小保持在600,600。 我希望我能夠在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. 另外,我在此xib文件中有一個按鈕,希望將其連接到不屬於其自己的控制器controlsViewController.m的方法,但要運行GameScene.m內部的方法。 我如何獲得能夠在其中運行方法的信息? 我知道我可以輕松地從xib拖動到controlsViewController.m來創建一個動作,但不會像預期的那樣拖動到GameScene.m。 我該如何解決?

任何幫助都會很棒

對於第一個問題,您可以通過調用獲取設備的屏幕尺寸。 [[UIScreen mainScreen]范圍]; 這將返回一個CGRect。

對於第二個問題,您無法將一個視圖控制器中的操作鏈接到另一個視圖控制器中的按鈕。 這是因為當第二個視圖控制器未實例化時,它可能未實例化(即在內存中)。 您可以使用單例類在視圖控制器之間共享資源,也可以使用響應者鏈。

您需要適應以下事實:視圖控制器是隔離的,並且沒有充分的理由解決這個問題,因此大多數應用程序使用單例在應用程序周圍全局共享數據。

閱讀Apple編寫和編寫應用程序文檔的最佳實踐

您最好為視圖的xib創建自定義視圖類。

創建這些文件。

  • MYControlsView.xib
  • MYControlsView類文件(MYControlsView.m / h)

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

您將MyControlsView放在Storyboard或xib上的ViewController上。 然后設置約束以填充ViewController的視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM