2

I have been trying to implement scroll view all day and watched as many as 30 tutorials (ok not that many but enough) and many text tutorials later but still won't work as everyone has different methods... I'm going insane here, can someone help? Here is the line of code that apparently works with scroll view... plus the steps I've taken:

created my second controller (named it View Controller2), dragged scroll view(named scroller) onto it and then started coding.

.h:

@interface ViewController2 : UIViewController {

    IBOutlet UIScrollView *scroller;

}
@end

.m:

@implementation ViewController2
{
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];
    [super viewDidLoad];
}

@end

I then after dragged the scroller from the attributes onto the UI scroll View and when I try running it I get errors with regards to the .m code? I also put two buttons on the view controller one at the top and another at the bottom to see if it would scroll once I run it as the screen would then go from 4 inch to 3.5.

Robert
  • 5,278
  • 43
  • 65
  • 115
  • i get three errors, funny thing before trying someone else's method of adding delegate to the .h... it did run fine but the scroll still wouldnt work. but now that ive used undo to revert back to where i was, i get three erros – user2459150 Jul 10 '13 at 20:56
  • did you initilize the scroll view? – apollosoftware.org Jul 10 '13 at 20:57
  • initilize the scroll view ? no mention of that, how do i go about doing that ? – user2459150 Jul 10 '13 at 20:58
  • Maybe: `UIScrollView *scroller = [[UIScrollView alloc] init]`? – Robert Jul 10 '13 at 21:01
  • 1
    Also you have to create it as a delegate in your .h file. Then you can in viewdidload use scoller.delegate = self; – apollosoftware.org Jul 10 '13 at 21:01
  • yes i did that which was someone else's way of doing it but it gave me a green error? (dont laugh im trying here) so it run but when i click on my buttons to go to the page to where i should have the scroll, it just takes me back to the .h file – user2459150 Jul 10 '13 at 21:06
  • Robert... i just tried your method but i get an error – user2459150 Jul 10 '13 at 21:06
  • Amit i just tried your method here is my code for both files now. for the .h @ interface ViewController2 : UIViewController { IBOutlet UIScrollView *scroller; } @ end – user2459150 Jul 10 '13 at 21:09
  • 1
    Put your .m code in `viewDidLoad` ? – Robert Jul 10 '13 at 21:12
  • this is my .m @implementation ViewController2 { [scroller setScrollEnabled:YES]; [scroller setContentSize:CGSizeMake(320, 600)]; [super viewDidLoad]; scoller.delegate = self; } @ end – user2459150 Jul 10 '13 at 21:13
  • im having to space the @ end because i cant use two in one comment – user2459150 Jul 10 '13 at 21:14
  • The three errors im getting are in the .m file and state "the type name requires a specifier or qualifier" – user2459150 Jul 10 '13 at 21:16

2 Answers2

3

I just ran this and it compiles and runs without error (although it doesn't do anything of note):

.h:

@interface ViewController2 : UIViewController {

    IBOutlet UIScrollView *scroller;

}    

.m:

@implementation ViewController2

- (void)viewDidLoad
{
    [super viewDidLoad];
    scroller = [[UIScrollView alloc] init];
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];
}
Robert
  • 5,278
  • 43
  • 65
  • 115
  • 1
    This is good. I'd much rather create the UIScrollView runtime rather than in IB. You can then add controls as necessary. Either this or mine below should work. +1 – apollosoftware.org Jul 10 '13 at 21:17
  • 1
    Youre a half genius @Robert however that brings me back to where i was which is good... still no sign of the scroll when i go to the screen i inserted the scroll.... – user2459150 Jul 10 '13 at 21:21
  • As @AmitApollo said, did you connect the scrollView to a property in IB? And which text based tutorial have you tried? – Robert Jul 10 '13 at 21:27
  • yes i connected/dragged the circular thing under outlets in the attributes of the viewcontroller2 and dragged it onto the UIscrollview but still nothing – user2459150 Jul 10 '13 at 21:29
  • Try changing the background colour to something garish in IB. you should be able to see it then. – Robert Jul 10 '13 at 21:35
  • still wont work, remember i have buttons top and bottom so the button at the top doesnt move when i try to scroll and the scroll doesnt come up either – user2459150 Jul 10 '13 at 21:55
  • 1
    FIXED i explained how i did it in the above anser. a BIG thnkyou to you sir too. both of you are very kind for taking your time to try things. im throwing money at the screen now – user2459150 Jul 10 '13 at 22:05
  • 1
    =) Glad you got there eventually. – Robert Jul 10 '13 at 22:06
2

Ok, this is what you do.

In your .h file add the delegate.

@interface ViewController2 : UIViewController <UIScrollViewDelegate> {
     IBOutlet UIScrollView *scroller;
}

@property (nonatomic, retain) UIScrollView *scroller;
@end

In your implementation .m file be sure to add the synthesize statement for your scroller

#import "ViewController2.h"

@implementation ViewController2
@synthesize scroller;

Then in your viewDidLoad method override as follows:

 - (void)viewDidLoad {
        [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];
    scroller.maximumZoomScale = 4.0;
    scroller.minimumZoomScale = 0.75;
    scroller.clipsToBounds = YES;
    //here is the delegate portion below
         scroller.delegate = self;
        [self.view bringSubviewToFront:scroller];
        [super viewDidLoad];
  }

Do not forget to connect it properly in Interface builder. Link scroller to your control!

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • Ok i just tried that, run without any errors however no sign of the scroll on the second screen which is the viewcontroller2.... – user2459150 Jul 10 '13 at 21:28
  • did you bring the subview to the top? it may be placed lower in your z order. – apollosoftware.org Jul 10 '13 at 21:29
  • 1
    [self.view bringSubviewToFront:scroller]; – apollosoftware.org Jul 10 '13 at 21:30
  • i dragged the scroller under Outlets onto the UIscrollview already but still nothing... – user2459150 Jul 10 '13 at 21:30
  • in viewDidLoad. See the modified method above – apollosoftware.org Jul 10 '13 at 21:31
  • i just added that, but still no scroll on viewcontroller2 when i run it which is btw not my main screen as you probably already know... you guys are great but juts one ste, this part isnt working – user2459150 Jul 10 '13 at 21:35
  • what do you mean not on your main screen? – apollosoftware.org Jul 10 '13 at 21:36
  • It may be simply the background colour. Try changing it and you'll hopefully see it. – Robert Jul 10 '13 at 21:37
  • let me tell you what i have connected to the UIscrollview. in the "connections insoector" it says outlet-delegate and referencing outlets shows scroller-viewcontroller2. I also have two buttons on the viewcontroller2... one placed at the top right below the navigation bar and another at the bottom above the tool bar and thats how i know it doesnt scroll as i can only see the top button and ofcourse when i try to scroll it doesnt do anything... i did also change my background to lime green but nothing... – user2459150 Jul 10 '13 at 21:39
  • its running without any errors just that the scroll isnt working or showing up in viewcontroller2 when i run it. when i said its not my first screen i meant that i have viewcontroller as my first screen with buttons on there one of which takes me to viewcontroller2 which is where i need the scroll but so far not showing up or working.... – user2459150 Jul 10 '13 at 21:48
  • What is #import "ViewController2.h" because when i try adding it to the .m file right above @implementation it gives me errors but without it the build runs fine but still no scroll on viewcontroller2? Amit, Robert you there ? – user2459150 Jul 10 '13 at 21:53
  • FIXED ... not sure which one it was but changed it from 600 to 1000 in the .m file and also changedthe UIScroller from default to white. ok if i want another scroll on another Viewcontroller which i will name viewcontroller3 can i just copy and paste the same code from both .h and .m and just change the appropriate names to viewcontroller3 and whatever i would have named the scroller... maybe scroller2 ? is that ok? and A BIG thanks to @AmiApollo and Robert – user2459150 Jul 10 '13 at 22:04