私は奇妙な問題に遭遇しています。UISegmentedControl を使用して 2 つのビューを切り替えています。UISegmentedViewController が画面に表示されると、デフォルトのビュー (view1) が読み込まれます。view1 に戻ると、同じ動作を示します。この UISegmentedViewController に使用しているコードを以下に添付します - SO 投稿で見つけました。わかりやすくするために写真を添付します。
編集:また、コースセクションの最後のテーブルビューセルに正しくスクロールできません。視界が遮られるのはなぜ?
コード:
//
// PCFSegmentedRateViewController.m
// Purdue Course Sniper
//
// Created by Kamran Pirwani on 2/5/13.
// Copyright (c) 2013 Kamran Pirwani. All rights reserved.
//
#import "PCFSegmentedRateViewController.h"
@interface PCFSegmentedRateViewController ()
// Array of view controllers to switch between
@property (nonatomic, copy) NSArray *allViewControllers;
// Currently selected view controller
@property (nonatomic, strong) UIViewController *currentViewController;
@end
@implementation PCFSegmentedRateViewController
@synthesize classRating,professorRating,segmentedControl;
- (void)viewDidLoad
{
[super viewDidLoad];
[segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
classRating = [[self storyboard] instantiateViewControllerWithIdentifier:@"ClassRate"];
professorRating = [[self storyboard] instantiateViewControllerWithIdentifier:@"Professor"];
// Add A and B view controllers to the array
self.allViewControllers = [[NSArray alloc] initWithObjects:classRating, professorRating, nil];
[self cycleFromViewController:self.currentViewController toViewController:[self.allViewControllers objectAtIndex:self.segmentedControl.selectedSegmentIndex]];
}
#pragma mark - View controller switching and saving
- (void)cycleFromViewController:(UIViewController*)oldVC toViewController:(UIViewController*)newVC {
// Do nothing if we are attempting to swap to the same view controller
if (newVC == oldVC) return;
// Check the newVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (newVC) {
// Set the new view controller frame (in this case to be the size of the available screen bounds)
// Calulate any other frame animations here (e.g. for the oldVC)
newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
// Check the oldVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (oldVC) {
// Start both the view controller transitions
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Swap the view controllers
// No frame animations in this code but these would go in the animations block
[self transitionFromViewController:oldVC
toViewController:newVC
duration:0.25
options:UIViewAnimationOptionLayoutSubviews
animations:^{}
completion:^(BOOL finished) {
// Finish both the view controller transitions
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}];
} else {
// Otherwise we are adding a view controller for the first time
// Start the view controller transition
[self addChildViewController:newVC];
// Add the new view controller view to the ciew hierarchy
[self.view addSubview:newVC.view];
// End the view controller transition
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
}
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
[self cycleFromViewController:self.currentViewController toViewController:incomingViewController];
}
}
@end