2012-02-13 73 views
1

全部。切换视图时,tabBar消失。为什么?

我使用两个视图(ScrollView和TableView)制作应用程序TabBar。

当应用程序启动时,出现TabBar按钮,并可以更改两个视图。

我在TableView中编程的按钮来改变ScrollView,并且视图正在改变。 但TabBar消失。

有人请帮忙!告诉我。

应用图像: http://www.0502.me/help/xcode_why.png

项目视图控制器(TableView中)

#import "FirstViewController.h" 
#import "SecondViewController.h" 

@implementation FirstViewController 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } else { 
     return YES; 
    } 
} 

- (IBAction)pageOne:(id)sender { 
//Below code is switch View 
    SecondViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondView"]; 
    [self presentModalViewController:secondView animated:YES]; 
} 

- (IBAction)pageTwo:(id)sender { 
} 

- (IBAction)pageThree:(id)sender { 
} 
@end 

第二视图控制器(滚动型)

#import "SecondViewController.h" 

@implementation SecondViewController 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 


#pragma mark - View lifecycle 

-(void)pageLoad:(UIScrollView *)scrollView { 
    currentPage = scrollView.contentOffset.x/scrollView.bounds.size.width; 
    int pageWidth = self.view.frame.size.width; 
    int pageHeight = self.view.frame.size.height; 

    prevPage.frame = CGRectMake(
           pageWidth * (currentPage - 1), 
           0, 
           pageWidth, 
           pageHeight 
           ); 
    if (currentPage > 0) { 
     [prevPage setImage:[NSString stringWithFormat:@"%d", (currentPage - 1) % kPageNum]]; 
     prevPage.hidden = NO; 
    } else { 
     prevPage.hidden = YES; 
    } 

    currPage.frame = CGRectMake(
           pageWidth * currentPage, 
           0, 
           pageWidth, 
           pageHeight 
           ); 

    [currPage setImage:[NSString stringWithFormat:@"%d", currentPage % kPageNum]]; 
    currPage.hidden = NO; 

    nextPage.frame = CGRectMake(
           pageWidth * (currentPage + 1), 
           0, 
           pageWidth, 
           pageHeight 
           ); 


    if (currentPage < (kPageNum - 1)) { 
     [nextPage setImage:[NSString stringWithFormat:@"%d", (currentPage + 1) % kPageNum]]; 
     nextPage.hidden = NO; 
    } else { 
     nextPage.hidden = YES; 
    } 

} 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIScrollView *scrollView = [[UIScrollView alloc] init]; 

    scrollView.frame = self.view.bounds; 
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * kPageNum, self.view.frame.size.height); 
    scrollView.pagingEnabled = YES; 
    [self.view addSubview:scrollView]; 

    scrollView.delegate = self; 

    prevPage = [[PageView alloc] initWithFrame:self.view.bounds]; 
    [scrollView addSubview:prevPage]; 

    currPage = [[PageView alloc] initWithFrame:self.view.bounds]; 
    [scrollView addSubview:currPage]; 

    nextPage = [[PageView alloc] initWithFrame:self.view.bounds]; 
    [scrollView addSubview:nextPage]; 

    [self pageLoad:scrollView]; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    CGFloat position = scrollView.contentOffset.x/scrollView.bounds.size.width; 
    CGFloat delta = position - (CGFloat)currentPage; 

    if (fabs(delta) >= 1.0f) { 
     [self pageLoad:scrollView]; 
    } 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

回答

2

您正在将其作为模态视图呈现。如果我理解你的问题,你只想切换UITabBar中的选定项目。你可以是这样做的:

self.tabBarController.selectedIndex = index; 

其中指数是代表你想切换到UIViewController的指数的整数。在你的情况下,它看起来像索引应该是0.

+0

谢谢edc1591,我重写代码,然后切换collectry! – 2012-02-13 06:08:39