在page control sample来自苹果的界面生成器中有一个 ScrollView。它与相应的 IBOutlet 链接。我想更改代码,以便全部以编程方式完成。我删除了界面生成器对象,我删除了 IBOutlet 关键字。我分配并初始化了 scrollView,但是当我运行程序时什么也没有出现。
我假设这是因为我需要将它作为 subview 分配给主视图。或者我呢?我仍然不太了解所有 View 如何工作以及如何相互作用。如果我执行 [self.view addSubView:ScrollView];
我会收到一个运行时错误(或者其他什么,它通常只是说 BAD ACCESS 或 SIGABRT)。
我做错了什么?我完全走错了路吗? (只接触 ios 编程两天,仍然有点迷失在树林里)
手机内容 Controller 中的 awakeFromNib:
- (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];
}
头文件:
#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
应用委托(delegate):
#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
并且 scrollView 已从 xib 文件中删除。注意:这是下载程序的新版本,我所做的所有更改是删除了 scrollView 的 IBOutlet 关键字,从 xib 中删除了 scroll 并添加了 alloc,init line in awake from nib。
我有建议更改 appDelegate 并将 awakeFromNib 更改为 init 方法,我已经尝试了所有这些但它仍然不起作用。
最佳答案
由于您不是从 nib 文件加载界面,因此您应该在 PhoneContentController
的 init
方法中设置您的 UIScrollView
:
- (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;
}
在您的 AppDelegate
中,进行以下更改:
- (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];
}
关于iphone - 以编程方式实现 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8909347/