ios - 从 UIViewController 添加 View

标签 ios objective-c uitableview uiviewcontroller

我创建了自己的自由格式第一个 View Controller ,并在其中放置了一个 TableView 。我正确地设置了它的类并为此表创建了一个 IBOutlet。

在我的第二个 View Controller 中,我想将第一个 View Controller 的 View 显示为窗口中的幻灯片,因此我创建了 IBAction 来执行此操作:

- (IBAction)optionsButtonTapped:(id)sender {
    OptionsViewController *optionsVC = [[OptionsViewController alloc] init];

    // Custom CGRect:
    CGFloat width = self.view.frame.size.width - 10.0;
    CGFloat height = [optionsVC.optionsTableView numberOfRowsInSection: 0] * 50.0;
    CGFloat x = self.view.frame.origin.x + ((self.view.frame.size.width - width) / 2);
    CGFloat y = self.view.frame.origin.y + self.view.frame.size.height + (1.2 * height);
    CGRect hiddenFrame = CGRectMake(x, y, width, height);

    // Custom UIView:
    UIView *view = optionsVC.view;
    view.frame = hiddenFrame;
    [self.view addSubview: view];
    [self addChildViewController: optionsVC];
    [optionsVC didMoveToParentViewController: self];
    //NSLog(@"Frame for the view is: %@", NSStringFromCGRect(hiddenFrame));

    // Animation:
    [UIView animateWithDuration: 0.3 animations:^(void){
        CGRect visibleFrame = hiddenFrame;
        visibleFrame.origin.y = self.view.frame.origin.y + self.view.frame.size.height - 5.0 - height;
        view.frame = visibleFrame;
    } completion:^(BOOL finished) {
        NSLog(@"Animation completed");
    }];
}

我使用 NSLog 时的高度是 0,那是因为当我初始化 optionsVC(我设置了数据源和委托(delegate))时,tableview 没有被加载。即使我将高度设置为例如。 200.0 View 仍然没有出现,但是我有来自第二个 View Controller 的日志,其中执行了 viewDidLoad。

我做错了什么?

最佳答案

您应该使用 ContainerView 来显示 Controller 内部的 Controller 。您可以为其位置设置动画并使用委托(delegate)来相互通信。

也许你应该考虑你的应用程序的其他设计(请添加你如何看待你的用户界面,因为我不清楚),你应该避免使用 ContainerView。引用Implementing a Container View Controller

关于ios - 从 UIViewController 添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481843/

相关文章:

ios - 获取应用程序的主窗口

ios - VMware 上的 macOS 无法识别 iOS 设备

ios - 区分耳机 Remote 和控制中心音乐控件?

ios - 单击新选项卡 iOS 时如何杀死 NSThread?

ios - 定义一个常数

ios - 在 github 之外创建私有(private) cocoapods 仓库

ios - 无法在 cloudant DB 中更快地获取数据

ios - UITableView tableView(_ :didEndEditingRowAt:) not called if rapidly tapping during swipe

ios - 在ios中从UITableView、Plist生成PDF

swift - 在 TableView 中删除后更新行