2012-11-13 31 views
0

我有一個具有ScrollView的iPad視圖,在下面我使用Page Control Object來顯示它在哪個頁面上。在屏幕上的其他地方,我的時間表介於上午12:00到上午5:00之間 - 隨着時間的推移,UIImage會在時間軸上變得更寬以指示一天的時間。隨着時間的推移,UIImage會變得更加寬廣,通過使用每分鐘左右開啓的NSTimer。重置UIPageControl重置視圖原始狀態

另外,我在頁面上有3個按鈕,它們會用一組新的圖像刷新滾動視圖。圖像的數量可以在3到7之間任意變化。因此,當按下按鈕時,我更新滾動視圖,並更新頁面控制對象以將「numberOfPages」屬性設置爲適當的數字。

好的,所以問題似乎是,當我單擊按鈕並且pageControl numberOfPages被更改時,UIImage會恢復到它最初在Interface Builder(故事板)中設計它時返回的任何大小。

我創建的希望有足夠的重建行爲的一個簡單的例子項目...

ViewController.h:

#import <UIKit/UIKit.h> 
@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UIButton *btnTest; 
@property (weak, nonatomic) IBOutlet UIImageView *imgTest; 
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl; 
- (IBAction)buttonPress:(id)sender; 
@end 

ViewController.m: #進口 「ViewController.h」

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize imgTest,btnTest,pageControl; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    // Set up an observer to handle updating the timeline 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateWidth:) 
               name:@"UpdateWidth" object:nil]; 
} 

-(void)viewDidAppear:(BOOL)animated { 
    // Send an updateTimeIndicator notification when screen loads. 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateWidth" object:@"sent"]; 
} 


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

- (IBAction)buttonPress:(id)sender { 
    pageControl.numberOfPages = 7; 
    pageControl.currentPage = 0; 

} 

-(void)UpdateWidth:(NSNotification *)notification{ 
    [imgTest setFrame:CGRectMake([imgTest frame].origin.x, [imgTest frame].origin.y, 
              450, [imgTest bounds].size.height)]; 

    imgTest.contentMode = UIViewContentModeScaleAspectFit; // This determines position of image 
    imgTest.clipsToBounds = YES; 
    [imgTest setNeedsDisplay]; 

    NSLog(@"Width Updated"); 
} 
@end 

因此,在這個例子中,我只有3個對象在窗口中 - PageControl,一個按鈕和一個UIImage,其背景設置爲藍色。我正在使用通知中心發送有關何時調整UIImage大小的消息(在這種情況下,我只會在視圖出現時執行一次)。 updateWidth例程將UIImage的大小設置爲450像素寬,並且該視圖顯示爲它應該。

但是,點擊該按鈕將更改numberOfPages值,並在此過程中將UIImageView重新設置爲最初在Interface Builder中佈置時的大小。

我有一個壓縮的項目文件,如果有人想看到它。此外,我沒有使用通知中心嘗試這一點,結果是相同的(我原本沒有使用通知中心,只是認爲它可能會給出不同的結果)。

我可以通過不使用PageControl對象,但我現在很好奇,爲什麼發生。謝謝!

+0

我沒有得到問題:(你會把我的壓縮項目發送到[email protected]嗎? –

+0

電子郵件發送 - 謝謝!這是一個相當難以解釋的問題,而沒有真正看到它。我希望有一種方法我可以在這裏在StackOverflow中附加項目文件。 – user1720817

回答

0

我似乎已經通過將ScrollView和PageControl對象放入它們自己的(新)UIView中解決了這個問題。爲此,我在界面構建器中打開故事板,從工具箱中將視圖對象拖到界面窗口中,然後將現有的ScrollView和PageControl對象放入新的UIView中。之後,更改PageControl的numberOfPages屬性不會影響窗口上的其他任何內容。

雖然我仍然對此行爲感到困惑。我猜想,因爲一旦頁面控件被放置在一個新的視圖中,並且問題停止了,PageControl想要更新numberOfPages屬性發生更改時所處的整個視圖。