Brauchen Sie Hilfe bei der betätigung ein Blick in UIScrollView / UIPageControl auf storyboard
Ich versuche, umzusetzen UIScrollView mit UIPageControl auf storyboard anzeigen mehrerer Ansichten. Alle Beispiele, tutorials oder Informationen die ich fand sind mit xib-und storyboard nicht. Ich habe versucht, die Anpassung, die Apple-sample-code, aber ich bin etwas fehlt. Wie Sie sehen können, ist es ganz einfach.
Aber entweder bekomme ich eine Fehlermeldung im ContentController.m -[NSNull view]: unrecognized selector geschickt Instanz 0x129acd8 wenn ich testen if (controller.Blick.superview == null) wo Schiebe ich die Ansicht (siehe unten, nach der ersten Hälfte des Codes).
Oder nur das UIScrollView und UIPageControl arbeiten und ich bin nicht in der Lage, schieben Sie die Ansicht in der UIScrollView:
Ich weiß, es ist eine einfache Frage, aber ich verbrachte viele Stunden auf diesem und jede Hilfe wird geschätzt.
Ich poste den vollständigen code, so kann es helfen, wenn sich jemand gelöst wie eine Art von tutorial. Ich werde korrigieren Sie den code, wenn erforderlich, nach Beiträge.
Vielen Dank im Voraus
AppDelegate.h
//Nothing special as I don't want to manage it through application delegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
//Nothing special as I don't want to manage it through application delegate
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application { }
- (void)applicationDidEnterBackground:(UIApplication *)application { }
- (void)applicationWillEnterForeground:(UIApplication *)application { }
- (void)applicationDidBecomeActive:(UIApplication *)application { }
- (void)applicationWillTerminate:(UIApplication *)application { }
@end
ContentController.h
//From Apple's PageControl sample code
#import <UIKit/UIKit.h>
@interface ContentController : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
BOOL pageControlUsed;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)changePage:(id)sender;
@end
ContentController.m
//From Apple's PageControl sample code
#import "AppDelegate.h"
#import "ContentController.h"
#import "MyViewController.h"
//Private methods
@interface ContentController (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end
@implementation ContentController
@synthesize scrollView, pageControl, viewControllers;
static NSUInteger kNumberOfPages = 6;
- (void)viewDidLoad {
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
self.navigationController.navigationBar.hidden=YES;
//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;
//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];
}
- (void)viewDidUnload {
[super viewDidUnload];
//Release any retained subviews of the main view.
//e.g. self.myOutlet = nil;
self.pageControl=nil;
self.pageControl = nil;
self.viewControllers = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//Release any cached data, images, etc that aren't in use.
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
//replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) //<== *** HERE SEEMS TO BE THE ERROR ***
{
controller = [[MyViewController alloc] initWithPageNumber:page
initWithNibName:nil
bundle:nil];
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
//add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
//We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
//which a scroll event generated from the user hitting the page control triggers updates from
//the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
//do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
//Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
//load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
//A possible optimization would be to unload the views+controllers which are no longer visible
}
//At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
//load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
//update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
//Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
@end
MyViewController.h
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
UILabel *pageNumberLabel;
int pageNumber;
}
@property (nonatomic, retain) IBOutlet UILabel *pageNumberLabel;
- (id)initWithPageNumber:(int)page initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
@end
MyViewController.m
#import "MyViewController.h"
@implementation MyViewController
@synthesize pageNumberLabel;
- (void)loadView {
}
- (void)viewDidLoad {
[super viewDidLoad];
//Set the label and background color when the view has finished loading
pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
}
- (void)viewDidUnload {
[super viewDidUnload];
//Release any retained subviews of the main view.
//e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//Load the view nib and initialize the pageNumber ivar
//classic initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//class adapted to init the page number
///!\ be careful, I'm not sure it's working properly
- (id)initWithPageNumber:(int)page initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
pageNumber = page;
NSLog(@"pageNumber = %i", pageNumber);
}
- (void)didReceiveMemoryWarning {
//Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
//Release any cached data, images, etc that aren't in use.
}
@end
Hier ist ein gutes tutorial : raywenderlich.com/10518/...
InformationsquelleAutor l_r | 2012-01-03
Du musst angemeldet sein, um einen Kommentar abzugeben.
Den unrecognized selector Fehler ist passiert, weil Sie redeclaring Ihre lokale variable "controller" innerhalb der if-Anweisung in loadScrollViewWithPage: Weil Sie so tun, die neue controller-variable existiert nur innerhalb des Geltungsbereichs der if-Anweisung, und beim verlassen der if-Anweisung die controller-variable außerhalb dieses Bereichs ist noch NSNull.
Können Sie beheben, durch das entfernen der Variablen definition innerhalb der if-Anweisung:
MyViewController *controller = [viewControllers objectAtIndex:page];
Ist nicht wirklich eine Erklärung (korrigiert mich wenn ich falsch Liege). Diese Anweisung funktioniert sehr gut, im Apple-Beispiel-code. Nochmals vielen Dank.Überprüfen Sie den code in die Apple PageControl Beispiel: developer.apple.com/library/ios/#samplecode/PageControl/... Sie werden sehen, dass Sie nicht deklarieren, die controller-variable in die if-Anweisung. Wenn Sie dies tun, überschreiben Sie die vorhandene variable wird im Rahmen der if-Anweisung. (Und ja, vor den Variablen-Namen mit
MyViewController *
stellt eine Erklärung)Ja, ich doppelt überprüft, Sie sind richtig. Danke. Aber ich kann immer noch nicht machen Sie arbeiten mit dem Storyboard. Ich habe zu arbeiten, eine mehr Erfahrung bekommen. Ich gebe für den moment, als ich verbrachte zu viel Zeit auf dieser. Ich Wills es mit XIB. Nochmals vielen Dank.
InformationsquelleAutor jonkroll
Also habe ich genau das gleiche problem Sie hatte, aber ich habe gerade etwas, das vielleicht funktionierte einwandfrei.
Kurze Antwort ist eine Ergänzung zu Jonkroll Antwort über die Einleitung der controller im inneren der if-Anweisung. Aus irgendeinem Grund kann ich nicht erklären, die leeren Platzhalter nicht null, so wird die if-Anweisung nicht ausgeführt wird, wie Sie wies darauf hin, in Ihrem code. Also, was ich Tat, war ändern Sie die wenn und fügen Sie eine else, dieselbe, ob und laden Sie die ViewController aus dem storyboard.
Den rest von meinem code ist genau wie deine. Ich weiß ist nicht der beste code immer und ich könnte ok sein, nur durch die Einleitung der ViewController ohne eine if/else-Anweisung, alle zusammen aber ich habe nicht die Antwort auf die Frage, warum dies erforderlich ist oder der Schlimmste, warum auch eigentlich nicht, wie ich erwarten. Wenn jemand weiß, bitte teilen.
InformationsquelleAutor Joel Cloralt
Zunächst alle, vielen Dank für diesen thread, es in der Tat, diente als eine Art tutorial oder eine Referenz für mich.
Ich habe alles fast genau wie Sie es Tat, außer in der content-controller lege ich folgenden code (basierend auf @jonkroll Antwort):
MyViewController (hat verschiedene Namen, in meinem Fall, aber es ist nicht von Bedeutung) erbt von UITableViewController, denn es war die Art von Sachen, die ich brauchte.
Meine initWithPageNumber Methode sieht wie folgt aus:
}
Hinzugefügt zurück.selbst, feststellen, dass.
Eine Letzte Sache ist, ich embedded ContentController in der Navigation-controller, über editor->embed-in->Navigations-controller.
Das ist so ziemlich alles. Später habe ich es sogar geschafft, sich die navigation von MyViewController zu einer anderen Ansicht und alles funktioniert einwandfrei.
Wenn Sie weitere Hilfe benötigen, sind Sie frei, hier Fragen.
Grüße.
InformationsquelleAutor Dimmy3
Ich hatte das gleiche problem mit Ihnen, wenn ich habe versucht, Folgen den Beispiel-code von UIPageController von Apple-Dokumente. Ich hatte meine MyViewController programmgesteuert, so dass ich direkt den folgenden code verwenden
Und ich nichts geändert MyViewController.
InformationsquelleAutor littlelydia