【发布时间】:2014-04-09 06:20:28
【问题描述】:
我的故事板中有一个带有导航栏的 ViewController 和一个 UIScrollView。 scrollView 的每个单独页面都是一个 XIB 文件。在其中一个 XIB 文件中,有一个按钮,我想转换到另一个 XIB 文件,并在我的导航栏中显示一个后退按钮。我不太明白这是怎么做到的。
图片:
故事板:Click Here
XIB(两个相似之一):Click Here
错误:i.stack.imgur.com/49eHT.png
代码文件:
ViewController.m(故事板)
- (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(用于 XIBS 的 UIView 子类)
@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
【问题讨论】:
-
接缝好像你做错了。如果你想使用导航控制器来浏览你的应用程序,你不应该用 xibs 填充你的滚动视图,你应该在你的故事板中创建新的视图控制器,然后像你想要的那样链接它们。这就是故事板的全部目的
标签: objective-c ios7 uiscrollview xib segue