0

I am a new coder in iOS. When I was trying to code ScrollView with paging, there is an issue to be fixed. I really don't know what is the problem in my code. I already set pageEnable, and set the page number with 4 current 0. In xib.file, I created 4 views, make scrollview connect "delegate" to "file's owner". My reference link http://www.iosdevnotes.com/2011/03/uiscrollview-paging/ When I run the app, the view cannot be paged. Thank you for your help. Here is my code:

////////// I think there is a problem in the for loop in viewDidLoad.

import

@interface ThirdViewController : UIViewController {

IBOutlet UIScrollView *scrollView; 
IBOutlet UIPageControl *pageControl;
IBOutlet UIView *contentOne;
IBOutlet UIView *contentTwo;
IBOutlet UIView *contentThree;
IBOutlet UIView *contentFour;

}

- (IBAction)pageValueChange:(id)sender;

@property(nonatomic,strong) IBOutlet UIScrollView *scrollView;
@property(nonatomic,strong) IBOutlet UIView *contentOne;
@property(nonatomic,strong) IBOutlet UIView *contentTwo;
@property(nonatomic,strong) IBOutlet UIView *contentThree;
@property(nonatomic,strong) IBOutlet UIView *contentFour;
@property(nonatomic,strong) IBOutlet UIPageControl *pageControl;
@property(nonatomic,strong) NSArray *viewArray;

@end




#import "ThirdViewController.h"

@interface ThirdViewController ()<UIScrollViewDelegate>

@end

@implementation ThirdViewController

    @synthesize scrollView,contentOne, contentTwo,contentThree,contentFour,pageControl,viewArray;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
             self.title = NSLocalizedString(@"Profile", @"Third");// Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        viewArray = [[NSArray alloc]initWithObjects:@"contentOne",@"contentTwo",@"contentThree", @"contentFour",nil];
        for (int i =0; i < [viewArray count]; i++) {
            CGRect frame;
            frame.origin.x = self.scrollView.frame.size.width * i ;
            frame.origin.y = 0;
            frame.size = self.scrollView.frame.size;
            UIView *subview =[[UIView alloc]initWithFrame:frame];

            [self.scrollView addSubview: subview]; //////how to pass the array object to subview
            [subview removeFromSuperview];

        }
        // Do any additional setup after loading the view from its nib.

        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *[viewArray count], scrollView.frame.size.height);


    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    - (IBAction)dismissKeyboard :(id) sender{
       [sender resignFirstResponder];
    }
    - (IBAction)pageValueChange:(id)sender{

        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

    }
    #pragma mark -UIScrollView Delegate
    -(void)scrollViewDidScroll:(UIScrollView *)sender{
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
        self.pageControl.currentPage = page;


}

@end        
HelenWang
  • 193
  • 2
  • 2
  • 10

1 Answers1

0

Did you put a breakpoint next to the line it crashed on?

Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70