Implementing UIScrollView Programmatically

In the control page sample from the apple, there is a ScrollView in the interface builder. It is associated with the corresponding IBOutlet. I want to change the code so that all this is done programmatically. I delete the object of the interface builder, I delete the IBOutlet keyword. I select and run scrollView, but nothing appears when the program starts.

I assume this is due to the fact that I need to assign it as a subView for the main view. Or me? I still do not understand how all views work and interact with each other. If I do [self.view addSubView:ScrollView]; I get a runtime error (or something, usually it just says something like BAD ACCESS or SIGABRT).

What am I doing wrong? Am I on the wrong track? (only two days in ios programming, still a little lost in the forest)

awakeFromNib in the phone content controller:

 - (void)awakeFromNib { scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.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]; } 

header file:

 #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 

and scrollView has been removed from the xib file. Note. This is a new version of the downloaded program, in which all I changed is removing the IBOutlet keyword for scrollView, removing the scroll from xib and adding a selection line, initialization in awake from nib.

I had suggestions for changing appDelegate and changing awakeFromNib to the init method, I tried all this, but it still doesn't work.

+4
source share
3 answers

Since you are not loading the interface from the nib file, you must configure your UIScrollView in the PhoneContentController init method:

 - (id)init { [super init]; if (self) { scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 440)]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)]; // Place it where you want it. viewControllers = [[NSMutableArray alloc] init]; // 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]; } return self; } 

In AppDelegate make the following changes:

 - (void)applicationDidFinishLaunching:(UIApplication *)application { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { contentController = [[PhoneContentController alloc] init]; } else { contentController = [[PadContentController alloc] init]; } [self.window addSubview:contentController.view]; [window makeKeyAndVisible]; } 
+2
source

you just put this inside the awakeFromNib method

 scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 440)]; 

there is no need to add as a peep, because in phoneContentController xib there are no views in it. As you can add as [self.view addSubView: ScrollView], because phoneContentController is not a type of the UIViewController class. this is a subclass of the ContentController class, which is a subclass of NSObject inside this project.

0
source

Simple method: you can create several times if you need funds

 - (void)viewDidLoad { [super viewDidLoad]; int x = 0; int y = 10; for(int i=0; i<5; i++) { UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)]; scrollview.showsVerticalScrollIndicator=YES; scrollview.scrollEnabled=YES; scrollview.userInteractionEnabled=YES; scrollview.backgroundColor = [UIColor greenColor]; [self.view addSubview:scrollview]; //scrollview.contentSize = CGSizeMake(50,50); x=x+55; //[self myscrollView]; } } 
0
source

Source: https://habr.com/ru/post/1391619/


All Articles