Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法使用分段控件显示UICollectionView_Ios_Swift_Uisegmentedcontrol - Fatal编程技术网

Ios 无法使用分段控件显示UICollectionView

Ios 无法使用分段控件显示UICollectionView,ios,swift,uisegmentedcontrol,Ios,Swift,Uisegmentedcontrol,在我的主视图控制器中,下面有一个分段控件和一个容器视图。我想在切换分段控件时更改容器视图。切换选项卡时,我可以看到正在加载的视图(在viewDidload中打印),但是,我发现它与collectionViewDataSource的数据源不兼容。有什么想法吗 当我将PhotoViewController作为初始控制器时,它会工作 我使用过UIContainerView和UISegmentControl,如果使用ContainerView,则显示已发布的单个UIViewController,下面

在我的主视图控制器中,下面有一个分段控件和一个容器视图。我想在切换分段控件时更改容器视图。切换选项卡时,我可以看到正在加载的视图(在viewDidload中打印),但是,我发现它与collectionViewDataSource的数据源不兼容。有什么想法吗

当我将PhotoViewController作为初始控制器时,它会工作


我使用过UIContainerView和UISegmentControl,如果使用ContainerView,则显示已发布的单个UIViewController,下面给出了链接

-(IBAction)indexChanged:(UISegmentedControl *)sender
{
    if (sender.selectedSegmentIndex == 0) {
        UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
        newViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
        [self cycleFromViewController:self.currentViewController toViewController:newViewController];
        self.currentViewController = newViewController;
    }
    else if (sender.selectedSegmentIndex == 1)
    {
        UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
        newViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
        [self cycleFromViewController:self.currentViewController toViewController:newViewController];
        self.currentViewController = newViewController;
    }
}

或者,如果在任意单击UISegmentControl时使用ContainerView显示两个UIViewController,则显示单个viewcontroller代码如下所示

-(IBAction)indexChanged:(UISegmentedControl *)sender
{
    if (sender.selectedSegmentIndex == 0) {
        UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
        newViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
        [self cycleFromViewController:self.currentViewController toViewController:newViewController];
        self.currentViewController = newViewController;
    }
    else if (sender.selectedSegmentIndex == 1)
    {
        UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
        newViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
        [self cycleFromViewController:self.currentViewController toViewController:newViewController];
        self.currentViewController = newViewController;
    }
}

如果创建故事板,则转到MainViewController类并创建插座

@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) UIViewController *currentViewController;
ViewDidLoad方法:

UIFont *font = [UIFont boldSystemFontOfSize:16.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                           forKey:UITextAttributeFont];
    [segmentControl setTitleTextAttributes:attributes
                                    forState:UIControlStateNormal];

    _currentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
    _currentViewController.view.layer.cornerRadius = 8.0f;
    _currentViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self addChildViewController:_currentViewController];
    [self addSubview:_currentViewController.view toView:_containerView];
下面给出了UISegmentController方法

-(IBAction)indexChanged:(UISegmentedControl *)sender
{
    if (sender.selectedSegmentIndex == 0) {
        UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
        newViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
        [self cycleFromViewController:self.currentViewController toViewController:newViewController];
        self.currentViewController = newViewController;
    }
    else if (sender.selectedSegmentIndex == 1)
    {
        UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
        newViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
        [self cycleFromViewController:self.currentViewController toViewController:newViewController];
        self.currentViewController = newViewController;
    }
}
与视图相关的两种方法是

- (void)addSubview:(UIView *)subView toView:(UIView*)parentView {
    [parentView addSubview:subView];

    NSDictionary * views = @{@"subView" : subView,};
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView]|"
                                                                   options:0
                                                                   metrics:0
                                                                     views:views];
    [parentView addConstraints:constraints];
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView]|"
                                                          options:0
                                                          metrics:0
                                                            views:views];
    [parentView addConstraints:constraints];
}

- (void)cycleFromViewController:(UIViewController*) oldViewController
               toViewController:(UIViewController*) newViewController {
    [oldViewController willMoveToParentViewController:nil];
    [self addChildViewController:newViewController];
    [self addSubview:newViewController.view toView:self.containerView];
    [newViewController.view layoutIfNeeded];

    // set starting state of the transition
    newViewController.view.alpha = 0;

    [UIView animateWithDuration:0.5
                     animations:^{
                         newViewController.view.alpha = 1;
                         oldViewController.view.alpha = 0;
                     }
                     completion:^(BOOL finished) {
                         [oldViewController.view removeFromSuperview];
                         [oldViewController removeFromParentViewController];
                         [newViewController didMoveToParentViewController:self];
                     }];
}
如果您使用这些代码,请在单击SegmentControl时从UIContainerView中交替打开ViewController,如果从FirstViewController中查看work CollectionView及其对我有效


希望对您有所帮助。

我认为在将视图添加到容器的子视图之前,
collectionView
没有作为视图加载到addsubview上。请尝试
activeVC.collectionView.reloadData()
方法,然后将其视图添加到容器的子视图中。但是,我可以使用分段控制器切换容器中的两个视图,如果我添加的视图是collectionviewController,我收到一条错误消息。addsubView中出现错误消息:**由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView必须使用非nil布局参数初始化”如果在使用uitableview时切换容器中的两个具有分段控件的视图,它对我来说工作正常,我认为您collectionview出现了一些错误,它的布局问题,我给出了以下解决方案