如何使用UIButton更改容器的内容?

时间:2013-06-14 05:09:23

标签: ios objective-c

我正在尝试将容器内的嵌入内容从tableviewcontroller更改为map。我试过搜索它,但所有的问题都与tabview有关,我没有这样做。我该如何开始这个过程?

我也试过

- (IBAction)changer:(id)sender {
    UIViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
    container.view = viewController1;
}

但这不起作用

2 个答案:

答案 0 :(得分:5)

尝试

MyViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
viewController1.view.frame = self.container.bounds;

[viewController1 willMoveToParentViewController:self];
[self.container addSubview:viewController1.view];
[self addChildViewController:viewController1];
[viewController1 didMoveToParentViewController:self];

答案 1 :(得分:0)

这是我在Swift中的实现:

func navigateTo(vc: NSViewController){
    vc.view.frame = self.containerView.bounds;
    if let currentSubview = containerView.subviews.first {
        self.containerView.replaceSubview(currentSubview, with: vc.view)
    } else {
        self.containerView.addSubview(vc.view)
    }
    self.addChildViewController( vc )
}

对于动画过渡,我实现了awakeFromNib

override func awakeFromNib() {
    let animation: CATransition = CATransition()
    animation.type = kCATransitionMoveIn
    animation.subtype = kCATransitionFromRight
    animation.duration = 0.75
    animation.delegate = self
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    if let _ = containerView {
        containerView.wantsLayer = true
        containerView.animations = NSDictionary(object: animation, forKey: "subviews") as! [String : AnyObject]
    }
}

并将行 self.containerView.replaceSubview 替换为:

self.containerView.animator().replaceSubview(currentSubview, with: vc.view)
相关问题