【问题标题】:How to segue between XIB views with Nav Bar?如何使用导航栏在 XIB 视图之间切换?
【发布时间】: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


【解决方案1】:

编辑:好的,我可以理解你现在想要做得更好。所以你需要给你想要执行这个动作的按钮添加一个目标和一个动作。如果那在 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

【讨论】:

  • 我写这个的类是 UIView 的子类。我相信这是因为我收到一个错误,指出在“iPhoneFirstage *”类型的对象上找不到属性“viewController”。如果重要的话,类的名称和 XIB 是 iPhoneFirstPage。
  • 我们可以看一些代码吗?我很难理解你的设置。
  • 我更新了问题以包含代码和一些图片。图示的 XIB 将转入另一个 XIB。 iPhoneFirstPage 是我用于图示的 XIB 的子类。
  • 非常感谢您的帮助,我无法弄清楚这一点。我无法弄清楚的最后一件事是 iPhoneFirstPage.m 中“self.navigationController”的错误(链接到原始帖子的错误图片)。
  • 是的。 UIView 没有 navigationController 属性,只有 UIViewControllers 有。这两行代码不属于 iPhoneFirstPage。它属于 UIViewController 对象。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2015-08-16
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    相关资源
    最近更新 更多