在下面的代碼我有一個UIScrollView中的實例的視圖對象,如果我運行下面的代碼,我得到警告說,"UIView might not respond to -setContentSize etc."
投射指定未知對象類型?
UIImage *image = [UIImage imageNamed:@"Snowy_UK.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[[self view] addSubview:imageView];
[[self view] setContentSize:[image size]];
[[self view] setMaximumZoomScale:2.0];
[[self view] setMinimumZoomScale: [[self view] bounds].size.width/[image size].width];
我已經檢查了對象的類型和[self view]
確實是一個UIScrollView
。我猜測這只是編譯器對這個類型做出錯誤的猜測,而解決方案只是簡單地將對象轉換爲正確的類型,我是否正確?
UIScrollView *scrollView = (UIScrollView *)[self view];
UIImage *image = [UIImage imageNamed:@"Snowy_UK.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[[self view] addSubview:imageView];
[scrollView setContentSize:[image size]];
[scrollView setMaximumZoomScale:2.0];
[scrollView setMinimumZoomScale: [scrollView bounds].size.width/[image size].width];
乾杯加里。
嗨,馬克,我在IB中設置了UIViewController,刪除了視圖並用UIScrollView替換了它。當我將fileOwner重新連接到新的UIScrollView時,我必須使用view來完成,這基本上是爲什麼後來編譯器認爲它是視圖而不是scrollView。感謝您的提示,這很好地工作。 – fuzzygoat 2010-06-16 16:24:30