Implementing UIScrollView programaticamente
Nopage control sample da apple, existe um ScrollView no construtor de interfaces. Ele está vinculado ao IBOutlet correspondente. Eu quero alterar o código para que tudo isso seja feito através de programação. Excluo o objeto construtor de interface, excluo a palavra-chave IBOutlet. Aloco e inicio o scrollView, mas nada aparece quando executo o program
Presumo que isso ocorra porque preciso atribuí-lo como um subView à visualização principal. Ou eu? Ainda não entendo como todas as visualizações funcionam e interagem entre si. Se eu fizer[self.view addSubView:ScrollView];
Eu recebo um erro de tempo de execução (ou algo assim, geralmente diz algo como BAD ACCESS ou SIGABRT
O que estou fazendo de errado? Estou no caminho errado completamente? (apenas dois dias na programação do ios, ainda um pouco perdida na floresta)
awakeFromNib no controlador de conteúdo do telefone:
- (void)awakeFromNib
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.si,ze.height)];
// load our data from a plist file inside our app bundle
NSString *path = [[NSBundle mainBundle] pathForResource:@"content_iPhone" ofType:@"plist"];
self.contentList = [NSArray arrayWithContentsOfFile:path];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
//
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
arquivo de cabeçalho
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ContentController.h"
@interface PhoneContentController : ContentController <UIScrollViewDelegate>
{
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)changePage:(id)sender;
@end
appDelegate:
#import "AppDelegate.h"
#import "ContentController.h"
@implementation AppDelegate
@synthesize window, contentController;
- (void)dealloc
{
[window release];
[contentController release];
[super dealloc];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSString *nibTitle = @"PadContent";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
nibTitle = @"PhoneContent";
}
[[NSBundle mainBundle] loadNibNamed:nibTitle owner:self options:nil];
[self.window addSubview:self.contentController.view];
[window makeKeyAndVisible];
}
@end
e o scrollView foi excluído do arquivo xib. Nota: esta é uma nova versão do programa baixado, onde tudo o que alterei foi excluir a palavra-chave IBOutlet para o scrollView, excluí-lo do xib e adicionar a alocação, a linha init, acordada do nib.
Eu tive sugestões para alterar o appDelegate e alterar o awakeFromNib para um método init, eu tentei tudo isso, mas ainda não funciona.