如何使用Nav Bar区分XIB视图?

时间:2014-03-06 14:31:45

标签: objective-c ios7 uiscrollview xib segue

我的故事板中有一个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

2 个答案:

答案 0 :(得分: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]

然后在ViewController上创建方法

-(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

答案 1 :(得分:0)

太糟糕了,没有跨故事板和跨故事板 - xib segues。 每个人都抱怨说,在大型项目中,故事板变得太慢,并希望切入小故事板。但苹果还没有实现它。剩下的就是以旧方式实例化并呈现xib的视图控制器。