[英]uiscrollview not switching image subviews
我正在構建一個漫畫查看器應用程序,該應用程序由兩個視圖控制器組成,根視圖控制器基本上顯示了一個視圖,用戶可以在其中通過按下按鈕來決定要閱讀的漫畫。 第二個viewController實際上將漫畫顯示為uiscrollview,頂部帶有工具欄和標題。
因此,我遇到的問題是,如果在查看第一個漫畫后選擇了另一個漫畫,漫畫圖像面板本身就不會改變。 我設置它的方式,但我承認它並不完全是mvc,所以請不要討厭,無論如何,我設置它的方式是每個漫畫uiscrollview包含x個jpg圖像,其中每個漫畫集的圖像名稱都有一個通用前綴然后是數字,例如“ funny1.jpg”,“ funny2.jpg”,“ funny3.jpg”和“ soda1.jpg”,“ soda2.jpg”,“ soda3.jpg”,等等。
因此,當用戶選擇漫畫在根控制器中查看時,它會調用委托並在屬於委托的comicviewcontroller實例(mainDelegate.comicViewController.property)上設置ivars,我設置了該漫畫中的面板數,標題標簽的漫畫名稱和圖像前綴。
圖片數量發生變化(或至少可以滾動瀏覽的數量),標題也會發生變化,但由於某些原因,這些圖片與您最初單擊的漫畫相同。
我將整個應用程序都基於蘋果的“滾動”代碼示例。
我以為,如果用戶每次單擊可修復該問題的按鈕時都會向comicViewController添加一個viewWillAppear:(BOOL)動畫調用,但實際上並沒有,畢竟這是滾動視圖的布局。
無論如何,這是來自兩個控制器的一些代碼:
RootController:
-(IBAction) launchComic2{
AppDelegate *mainDelegate = [(AppDelegate *) [UIApplication sharedApplication] delegate];
mainDelegate.myViewController.comicPageCount = 3;
mainDelegate.myViewController.comicTitle.text = @"\"Death by ETOH\"";
mainDelegate.myViewController.comicImagePrefix = @"etoh";
[mainDelegate.myViewController viewWillAppear:YES];
[mainDelegate.window addSubview: mainDelegate.myViewController.view];
comicViewController:
-(void) viewWillAppear:(BOOL)animated {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor whiteColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= self.comicPageCount; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@%d.jpg", self.comicImagePrefix, i];
NSLog(@"%@%d.jpg", self.comicImagePrefix, i);
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((self.comicPageCount * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
任何幫助將不勝感激。
缺口
因此,我最終要做的就是在委托中創建喜劇控制器的多個實例,而不是僅僅重復使用一個。 如果遇到問題,將對此進行更新。
缺口
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.