使用滚动视图从视图中打开新视图控制器

时间:2015-03-16 15:11:45

标签: ios objective-c iphone xcode viewcontroller

我正在开发一个带有可滚动标签的iphone应用程序,在初始视图中有4页。我有一个视图,BaseViewController,底部只有一个包含4个按钮的滚动视图。 在viewDidLoad {}我设置页面尺寸和方法,用按钮或滑动来调用页面。

现在,从第一页开始,我想用后退按钮打开一个新视图,但底部没有滚动视图。如果我使用推,滚动视图仍然在底部,如果我使用模态,顶部没有后退按钮。

我在网上尝试了很多解决方案,但没有人正常工作。

//#import "AppDelegate.h"
#import "BaseViewController.h"
#import "FirstTabController.h"
#import "SecondTabController.h"
#import "ThirdTabController.h"
#import "FourthTabBarController.h"

@interface BaseViewController (){
    NSInteger indicator;
    //AppDelegate *appDelegate;
}

@end

@implementation BaseViewController;
@synthesize tabBarScroller, storyBoard;
@synthesize firstTab, secondTab, thirdTab, fourthTab;
@synthesize btn1, btn2, btn3, btn4;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];

    //appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //appDelegate.base=self;

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]     initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;

    [self.view addGestureRecognizer:swipeRight];
    [self.view addGestureRecognizer:swipeLeft];
}

-(void)slideToRightWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer{
    if (indicator!=1) {
        [self tabCall:indicator-1];
    }
}

 -(void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer    *)gestureRecognizer{
    if (indicator!=4) {
        [self tabCall:indicator+1];
    }
}

-(void)setup {
    [tabBarScroller setContentSize:CGSizeMake(519, 49)];

    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

    if (iOSDeviceScreenSize.height == 480){
        storyBoard = [UIStoryboard    storyboardWithName:@"MainStoryboard_iphone_small" bundle:nil];
        rect = CGRectMake(0, 0, 320, 431);
    }else{
        storyBoard = [UIStoryboard     storyboardWithName:@"MainStoryboard_iphone_large" bundle:nil];
        rect = CGRectMake(0, 0, 320, 519);
    }

    [self tabCall:1];
    btn1.backgroundColor=[UIColor yellowColor];
    btn2.backgroundColor=[UIColor redColor]; 
    btn3.backgroundColor=[UIColor redColor];
    btn4.backgroundColor=[UIColor redColor];
}

-(IBAction)buttonTabClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    [self tabCall:button.tag];
}

-(void)tabCall:(NSInteger)tag {
    indicator=tag;

     switch (tag) {
        case 1:
            [tabBarScroller setContentOffset:CGPointMake(0,0) animated:YES];
            [self.view addSubview:self.navFirstTab.view];
            btn1.backgroundColor=[UIColor yellowColor];
            btn2.backgroundColor=[UIColor redColor];
            btn3.backgroundColor=[UIColor redColor];
            btn4.backgroundColor=[UIColor redColor];
            break;

        case 2:
            [tabBarScroller setContentOffset:CGPointMake(30,0) animated:YES];
            [self.view addSubview:self.navSecondTab.view];
            btn1.backgroundColor=[UIColor redColor];
            btn2.backgroundColor=[UIColor yellowColor];
            btn3.backgroundColor=[UIColor redColor];
            btn4.backgroundColor=[UIColor redColor];
            break;

        case 3:
            [tabBarScroller setContentOffset:CGPointMake(160,0) animated:YES];
            [self.view addSubview:self.navThirdTab.view];
            btn1.backgroundColor=[UIColor redColor];
            btn2.backgroundColor=[UIColor redColor];
            btn3.backgroundColor=[UIColor yellowColor];
            btn4.backgroundColor=[UIColor redColor];
            break;

        case 4:
            [tabBarScroller setContentOffset:CGPointMake(199,0) animated:YES];
            [self.view addSubview:self.navFourthTab.view];
            btn1.backgroundColor=[UIColor redColor];
            btn2.backgroundColor=[UIColor redColor];
            btn3.backgroundColor=[UIColor redColor];
            btn4.backgroundColor=[UIColor yellowColor];
            break;

        default:
            break;
    }
}

- (UINavigationController*) navFirstTab
{
    if (!_navFirstTab) {
        if (!firstTab) {
            firstTab = [storyBoard instantiateViewControllerWithIdentifier:@"FirstTabController"];
        }
        _navFirstTab = [[UINavigationController alloc] initWithRootViewController:self.firstTab];
        _navFirstTab.navigationBar.tintColor = [UIColor blackColor];
        _navFirstTab.view.frame = rect;
        _navFirstTab.navigationBar.topItem.title = @"First Controller";
    }
    return _navFirstTab;
}


- (UINavigationController*) navSecondTab
{
    if (!_navSecondTab) {
        if (!secondTab) {
            secondTab = [storyBoard instantiateViewControllerWithIdentifier:@"SecondTabController"];
        }
        _navSecondTab = [[UINavigationController alloc] initWithRootViewController:self.secondTab];
        _navSecondTab.navigationBar.tintColor = [UIColor blackColor];
        _navSecondTab.view.frame = rect;
        _navSecondTab.navigationBar.topItem.title = @"Second Controller";
    }
    return _navSecondTab;
}

- (UINavigationController*) navThirdTab
{
    if (!_navThirdTab) {
        if (!thirdTab) {
            thirdTab = [storyBoard instantiateViewControllerWithIdentifier:@"ThirdTabController"];
        }
        _navThirdTab = [[UINavigationController alloc] initWithRootViewController:self.thirdTab];
        _navThirdTab.navigationBar.tintColor = [UIColor blackColor];
        _navThirdTab.navigationBar.topItem.title = @"Third Controller";
        _navThirdTab.view.frame = rect;
    }
    return _navThirdTab;
}
- (UINavigationController*) navFourthTab
{
    if (!_navFourthTab) {
        if (!fourthTab) {
             fourthTab = [storyBoard instantiateViewControllerWithIdentifier:@"FourthTabBarController"];
         }
         _navFourthTab = [[UINavigationController alloc]     initWithRootViewController:self.fourthTab];
         _navFourthTab.navigationBar.tintColor = [UIColor blackColor];
         _navFourthTab.view.frame = rect;
         _navFourthTab.navigationBar.topItem.title = @"Fourth Controller";
    }
    return _navFourthTab;
}

@end

1 个答案:

答案 0 :(得分:0)

如果您添加/删除viewControllers作为每个按钮单击的子视图而不是推送或呈现。您可以对rootView Controller上显示的项目进行更多控制。