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