2014-03-06 103 views
0

我的故事板中有一個ViewController,帶有一個導航欄和一個UIScrollView。 scrollView的每個單獨頁面都是一個XIB文件。在其中一個XIB文件中,有一個按鈕用於轉換到另一個XIB文件,並在我的導航欄中顯示後退按鈕。我無法弄清楚這是如何完成的。如何使用導航欄在XIB視圖之間切換?

圖片:

故事板:Click Here

廈門國際銀行(其中兩個相似):Click Here

錯誤:i.stack.imgur.com/49eHT.png

代碼文件: (Storyboard)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Set up View for scrollview with nav controller 
    self.automaticallyAdjustsScrollViewInsets = NO; 

    self.title = @"Cazenovia Central School District"; 

    self.bottomBar.backgroundColor = [UIColor colorWithRed:9.0f/255.0f green:49.0f/255.0f blue:102.0f/255.0f alpha:1.0f]; 

    //do some set up on the scrollview 
    self.theScrollView.pagingEnabled = YES; 
    self.theScrollView.showsHorizontalScrollIndicator = NO; 
    self.theScrollView.showsVerticalScrollIndicator = NO; 

    //load the nibs that contain the views you want to page thru 
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"iPhoneFirstPage" owner:self options:nil]; 
    UIView *firstPageView = (UIView *)[nibArray objectAtIndex:0]; 

    NSArray *nibArray2 = [[NSBundle mainBundle] loadNibNamed:@"iPhoneSecondPage" owner:self options:nil]; 
    UIView *secondPageView = (UIView *)[nibArray2 objectAtIndex:0]; 

    NSArray *pageArray = @[firstPageView, secondPageView]; 

    //add each view to the scroll view 
    for(int i=0; i<pageArray.count; i++){ 
     CGRect frame; 
     frame.origin.x = self.theScrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = self.theScrollView.frame.size; 

     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     [subview addSubview:[pageArray objectAtIndex:i]]; 
     [self.theScrollView addSubview:subview]; 
    } 

    self.theScrollView.contentSize = CGSizeMake(self.theScrollView.frame.size.width * pageArray.count, self.theScrollView.frame.size.height); 
    self.theScrollView.delegate = self; 
    self.thePageControl.numberOfPages = pageArray.count; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - UIScrollViewDelegate 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    CGFloat pageWidth = self.theScrollView.frame.size.width; 
    int page = floor((self.theScrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    self.thePageControl.currentPage = page; 
} 

@end 

iPhoneFirstPage.m(UIView子類與XIBS使用)

@implementation iPhoneFirstPage 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     UIViewController *newController = [[UIViewController alloc] initWithNibName:@"twitterView" bundle:nil]; 
     [self.navigationController pushViewController:newController animated:YES]; 

    } 
    return self; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 
+2

像你這樣的縫隙做錯了。如果你想使用導航控制器瀏覽你的應用程序,你不應該用xib填充你的滾動視圖,你應該在你的故事板中創建新的視圖控制器,然後像你想要的那樣鏈接它們。這就是故事板的全部用途 – AntonijoDev

回答

4

編輯:好吧,我能理解你正在嘗試更好地盡現。所以你需要添加一個目標和一個動作到你想要執行這個動作的按鈕。如果在iPhoneFirstPage中,在類頭上創建一個出口,以便您可以從其他位置引用它。包括你的ViewController.m文件頭和更換線路:

NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"iPhoneFirstPage" owner:self options:nil]; 
UIView *firstPageView = (UIView *)[nibArray objectAtIndex:0]; 

有:

NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"iPhoneFirstPage" owner:self options:nil]; 
iPhoneFirstPage *firstPageView = (iPhoneFirstPage *)[nibArray objectAtIndex:0]; 
[firstPageView.button addTarget:self action:@selector(executeSegue) forControlEvents:UIControlEventTouchUpInside] 

然後在視圖控制器

-(void)executeSegue 
{ 
    UIViewController *newController = [[UIViewController alloc] initWithNibName:@"twitterView" bundle:nil]; 
    [self.navigationController pushViewController:newController animated:YES]; 
} 

這就是理念創建方法。更多信息(更多信息)可以在以下網址找到: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

+0

我寫這個的類是UIView的一個子類。我相信這是因爲我得到一個錯誤,指出在'iPhoneFirstage *'類型的對象上找不到屬性'viewController'。如果有問題,類的名稱和XIB是iPhoneFirstPage。 – SuperAdmin

+0

我們可以看到一些代碼嗎?我很難理解你的設置。 – Murillo

+0

我更新了問題以包含代碼和一些圖片。圖中的XIB將繼續進入另一個XIB。 iPhoneFirstPage是我用於圖片XIB的子類。 – SuperAdmin

0

太糟糕了,沒有跨故事板和跨故事板xib賽段。每個人都抱怨說,在大型項目中,故事板變得太慢,並希望切入小故事板。但是蘋果還沒有實現它。剩下的就是用舊的方式實例化和呈現xib的視圖控制器。