Need help for pushing a view in UIScrollView / UIPageControl on storyboard?

The unrecognized selector error is happening because you are redeclaring your local variable "controller" within the if statement in loadScrollViewWithPage: Because you are doing so, the new controller variable only exists within the scope of the if statement, and when you exit the if statement the controller variable outside that scope is still NSNull.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm trying to implement UIScrollView with UIPageControl on storyboard to display multiple views. All the examples, tutorials or informations I found are using xib and not storyboard. I tried to adapt Apple's sample code but I'm missing something.

As you can see, it is quite simple. However, either I get an error in ContentController. M -NSNull view: unrecognized selector sent to instance 0x129acd8 when I test if (controller.view.

Superview == nil) where I push the view (see below after the first half of code). Or only the UIScrollView and UIPageControl are working and I am not able to push the view in the UIScrollView: I know it's a simple question, however I spent many hours on this and any help will be appreciated. I post the full code as it may help someone when resolved like a kind of tutorial.

I will correct the code if/when needed according to contributions. Thanks in advance AppDelegate. H // Nothing special as I don't want to manage it through application delegate @interface AppDelegate : UIResponder @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 @interface ContentController : UIViewController { 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) return; // replace the placeholder if necessary MyViewController *controller = viewControllers objectAtIndex:page; if ((NSNull *)controller == NSNull null) // @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 ios uiscrollview storyboard uipagecontrol link|improve this question edited Jan 6 at 16:41 asked Jan 3 at 15:17l_r276 75% accept rate.

If anyone knows a link to a tutorial using storyboard I'll appreciate. – l_r Jan 4 at 21:16.

The unrecognized selector error is happening because you are redeclaring your local variable "controller" within the if statement in loadScrollViewWithPage: Because you are doing so, the new controller variable only exists within the scope of the if statement, and when you exit the if statement the controller variable outside that scope is still NSNull. You can fix by removing the variable definition inside the if statement: if ((NSNull *)controller == NSNull null) { controller = self. Storyboard instantiateViewControllerWithIdentifier:@"MVC"; controller initWithPageNumber:page; viewControllers replaceObjectAtIndex:page withObject:controller; }.

Thank you for your time and answer. However, I'm pretty sure that the error does not come from here. The controller variable is defined in the if statement if it is null.

When exiting the if statement, it is not null anymore. The first reference to the variable in MyViewController *controller = viewControllers objectAtIndex:page; Is not really a declaration (correct me if I'm wrong). This statement works perfectly well in Apple sample code.

Thanks again. – l_r Jan 4 at 21:14 Double check the code in the Apple PageControl example: developer.apple.com/library/ios/#samplec... You'll see that they are not declaring the controller variable inside the if statement. When you do this you are overriding the existing variable within the scope of the if statement.

(And yes, prefacing the variable name with MyViewController * does constitute a declaration) – jonkroll Jan 4 at 23:04 Yes, I double checked, you are right. Thank you. However, I still can not make it work using Storyboard.

I have to work on it an get more experience. I give-up for the moment as I spent too much time on this. I will manage it using XIB.

Thanks again. – l_r Jan 5 at 18:42.

So I've been having exactly the same problem you were having but I just did something that might have worked just fine. Short answer is an addition to Jonkroll's answer about initiating the controller inside the if statement. For some reason I can't explain the empty placeholder is not null so the if statement doesn't execute as you pointed out in your code.

So what I did was modify the if and add an else to that same if and load the ViewController from the storyboard. If ((NSNull *)controller == NSNull null) { controller = self. Storyboard instantiateViewControllerWithIdentifier:@"MVC"; viewControllers replaceObjectAtIndex:page withObject:controller initWithPageNumber:page; } else { controller = self.

Storyboard instantiateViewControllerWithIdentifier:@"MVC"; viewControllers replaceObjectAtIndex:page withObject:controller initWithPageNumber:page; } The rest of my code is exactly as yours. I know is not the best code ever and I might be ok by just initiating the ViewController without an if/else statement all together but I don't have the answer as to why this is needed or worst why is not actually working as I expect. If anyone knows please share.

By the way, I know this is horrible programming and I could just get rid of the if statement. One thing that is actually happening is that the memory usage goes to the roof every time you scroll pages and I get memory warning that leads to a crash. If anyone knows about how to use this with storyboard I'm all ears.

– Joel Cloralt Jan 29 at 16:21.

So I've been having exactly the same problem you were having but I just did something that might have worked just fine.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions