簡體   English   中英

從具有滾動視圖的視圖中打開新的視圖控制器

[英]open new view controller from view with scroll view

我正在開發的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

如果您將viewControllers添加/刪除為每個按鈕的子視圖,請單擊而不是按入或呈現。您可以對rootView Controller上顯示的項目進行更多控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM