Domanda

I have a ViewController in my storyboard with a navigation bar, and an UIScrollView. Each individual page of the scrollView is an XIB file. In one of the XIB files, there is a button that I want to transition to another XIB file, and have a back button appear in my navigation bar. I can't quite figure out how this is done.

Images:

Storyboard: Click Here

XIB (One of two similar): Click Here

Error: i.stack.imgur.com/49eHT.png

Code Files:

ViewController.m (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 Subclass for use with 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
È stato utile?

Soluzione

Edit: Ok, I can understand what you are trying to do better now. So you need to add a target and an action to the button you want to execute this action. If that's in iPhoneFirstPage create an outlet on the class header so you can reference it from somewhere else. Include the header on your ViewController.m file and replace the lines:

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

with:

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]

Then create the method on ViewController

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

That's the idea. More information (a lot more of it) can be found at: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

Altri suggerimenti

Too bad there is no cross-storyboards and cross-storyboard-xib segues. Everybody complain that in big projects storyboard becomes too slow and wish to cut in little storyboards. But Apple haven't implemented it yet. All is left is to instantiate and present xib's view controllers by old way.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top