簡體   English   中英

在子視圖中添加 UIViewController

[英]add UIViewController in subview

我不知道這是否是搜索“在子視圖中添加 UIViewController”的正確鍵。 正如你在我的圖片中看到的,有兩個 ViewController,主控制器和第二控制器。 在主控制器內部有一個 UIView(藍色背景色)。 在 UIView 中,我想在 UIView 中添加第二個 ViewController。 我有這個代碼,但沒有用。

在此處輸入圖片說明

這是我的代碼

#import "ViewController.h"
#import "SampleViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *testView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    SampleViewController * sample = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
    sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height);
    [self.testView addSubview:sample.view];
} 

@end

我想知道這是否可能? 我知道initWithNibName:在 xib 文件中工作,我不知道在谷歌中搜索這個的確切術語。 如果這在 IOS 中可行,我只是想嘗試一些東西。 希望你明白我在做什么。 希望得到您的建議。 提前致謝

這是我的更新

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *testView;
@property(strong,nonatomic) SampleViewController * samples;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

UIStoryboard *storyBoard = self.storyboard;
SampleViewController * sample = [storyBoard instantiateViewControllerWithIdentifier:@"SampleViewController"]; 
// SampleViewController * sample = [[SampleViewController alloc] //initWithNibName:@"SampleViewController" bundle:nil];

[self displayContentController:sample];
//commented the below line because it is not needed here, use it when you want to remove        
//child view from parent.
 //[self hideContentController:sample];

}

- (void) displayContentController: (UIViewController*) content;
{
    [self addChildViewController:content];                 // 1
    content.view.bounds = self.testView.bounds;                 //2
    [self.testView addSubview:content.view];
    [content didMoveToParentViewController:self];          // 3
}


- (void) hideContentController: (UIViewController*) content
{
    [content willMoveToParentViewController:nil];  // 1
    [content.view removeFromSuperview];            // 2
    [content removeFromParentViewController];      // 3
}

我總是收到這個錯誤

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/ace/Library/Developer/CoreSimulator/Devices/035D6DD6-B6A5-4213-9FCA-ECE06ED837EC/data/Containers/Bundle/Application/EB07DD14-A6FF-4CF5-A369-45D6DBD7C0ED/Addsubviewcontroller.app> (loaded)' with name 'SampleViewController''

我想,它在尋找一個筆尖。 我沒有在這里實現筆尖。

您應該使用子包含概念,這里 MainViewController 是一個父視圖控制器,您希望將子視圖控制器視圖添加為主視圖控制器上的子視圖。

添加和刪​​除子項

//call displayContentController to add SampleViewCOntroller view to mainViewcontroller
 [self displayContentController:sampleVCObject];

// write this method in MainViewController
- (void) displayContentController: (UIViewController*) content;
{
   [self addChildViewController:content];                 // 1
   content.view.bounds = testView.bounds;                 //2
   [testView addSubview:content.view];
   [content didMoveToParentViewController:self];          // 3
}

下面是代碼的作用:

它調用容器的 addChildViewController: 方法來添加孩子。 調用 addChildViewController: 方法也會自動調用孩子的 willMoveToParentViewController: 方法。 它訪問子視圖屬性以檢索視圖並將其添加到自己的視圖層次結構中。 容器在添加視圖之前設置孩子的大小和位置; 容器總是選擇子內容出現的位置。 盡管本示例通過顯式設置框架來實現這一點,但您也可以使用布局約束來確定視圖的位置。 它顯式調用子節點的 didMoveToParentViewController: 方法來發出操作完成的信號。

//you can also write this method in MainViewController to remove the child VC you added before.
- (void) hideContentController: (UIViewController*) content
{
   [content willMoveToParentViewController:nil];  // 1
   [content.view removeFromSuperview];            // 2
   [content removeFromParentViewController];      // 3
}

更多詳情請參考蘋果文檔: https : //developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

在 Interface Builder 中配置一個容器,適合那些不想寫代碼的人。

要在設計時創建父子容器關系,請將容器視圖對象添加到故事板場景中,如圖 5-3 所示。 容器視圖對象是一個占位符對象,表示子視圖控制器的內容。 使用該視圖來調整與容器中其他視圖相關的子級根視圖的大小和位置。

在 Interface Builder 中添加容器視圖 當你加載一個帶有一個或多個容器視圖的視圖控制器時,Interface Builder 也會加載與這些視圖關聯的子視圖控制器。 子項必須與父項同時實例化,以便可以創建適當的父子關系。

您只需使用 StoryBoards 即可完成此操作

  • 打開故事板並選擇您的藍色視圖所在的視圖控制器,打開實用程序搜索 ContainerView 並將其拖到您的藍色視圖中,這將自動添加一個視圖控制器作為您的視圖的子視圖。 您可以在大小檢查器中調整容器視圖的大小。

在此處輸入圖片說明

我已經解決了關於第二個 uiviewcontroller 上的 uiview 的問題。 按照我使用過的代碼:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *sb = (SecondViewController *)[storyboard instantiateViewControllerWithIdentifier:@"sb"];

sb.view.backgroundColor = [UIColor redColor];    
[sb willMoveToParentViewController:self];

[self.view addSubview:sb.view];

[self addChildViewController:sb];
[sb didMoveToParentViewController:self];

我希望能幫助你。

非常感謝

從 iOS5 開始,您可以向 UIViewController 添加 childViewController ..這是制作更小、更可重用的 viewController 的好方法,我也非常喜歡它。

你真的很接近,但你只需要多幾行代碼..

///
 [self addChildViewController:sample];

 [self.testView addSubview:sample.view]; //you already have this..

 [sample didMoveToParentViewController:self]; 

在您 viewWillDisappear: 或您需要像這樣清理的其他拆卸方法之一:

//we'll need another pointer to sample, make it an iVar / property..
   [sample willMoveToParentViewController:nil];  // 1
   [sample removeFromSuperview];            // 2
   [sample removeFromParentViewController];      // 3

您可以在此處閱讀有關包含子視圖控制器的 Apple 文檔https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

SampleViewController * sample = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height);

[self addChildViewController:sample];
[self.testView addSubview:sample.view];

我正在嘗試在子視圖中添加 UIViewController,這項工作。 在 UIViewController 上,我在 .h 文件中添加了 2 個按鈕:

-(IBAction)matchButtonAction:(id)sender;
-(IBAction)closeButtonAction:(id)sender;

在 .m 文件中:

-(IBAction)matchButtonAction:(id)sender
{
    NSLog(@"matchButtonAction");
}
-(IBAction)closeButtonAction:(id)sender
{
    NSLog(@"closeButtonAction");
}

但我沒有看到日志。

我需要在 UIViewController instantiateViewControllerWithIdentifier 上添加相同的參數嗎?

如何解決問題?

我的 UIViewController 初始化是:

AlertDialogViewController *alertDialogView = (AlertDialogViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"alertDialogView"];         
        [alertDialogView willMoveToParentViewController:self];
        [viewController.view addSubview:alertDialogView.view];
        [viewController addChildViewController:alertDialogView];
        [alertDialogView didMoveToParentViewController:viewController];

由於您的視圖控制器在情節提要中,因此您應該使用instantiateViewControllerWithIdentifier從情節提要中獲取 VC。

SampleViewController * sample = [self.storyboard instantiateViewControllerWithIdentifier:@"IdOfSampleViewController"];
sample.view.frame = CGRectMake(0, 0, self.testView.bounds.size.width, self.testView.bounds.size.height);
[self.testView addSubview:sample.view];

不要忘記在故事板中為SampleViewController添加標識符

我嘗試使用此代碼:

SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

 secondViewController.view.frame = CGRectMake(0, 0, self.mySubView.bounds.size.width, self.mySubView.bounds.size.height);

 [self addChildViewController:secondViewController];
 [self.viewDialogSolution addSubview:secondViewController.view];

但我只看到 myview 而不是 secondViewController 的布局。

何來解決問題?

非常感謝

讓 controller:SecondViewController = self.storyboard!.instantiateViewController(withIdentifier: "secondViewController") as! 第二個視圖控制器

controller.view.frame = self.view.bounds

controller.willMove(toParent: self)

self.view.addSubview(controller.view)

self.addChild(控制器)

controller.didMove(toParent: self)

暫無
暫無

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

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