2012-12-26 184 views
2

設置UIScrollView的位置,我嘗試使用contentOffset這樣設置一個UIScrollView的位置:從另一個視圖控制器

- (void) navigateToTableViewPosition:(CGPoint)contentOffset { 
    NSLog(@"Position set method gets called..."); 
    NSLog(@"%@", NSStringFromCGPoint(contentOffset)); 
    [mainScrollView setContentOffset:contentOffset animated:YES]; 
} 

我從另一個調用視圖控制器此方法之前,我駁回,並一切正常。我正確地傳遞了參數,並調用了方法(使用NSLog檢查它),但滾動視圖不會移動...

有趣的是,當我從視圖控制器調用此方法時,位於,它工作正常。只有當我從另一個視圖控制器調用它時,它纔會停止工作。

只是以供將來參考,這裏是調用方法:

MainViewController *mainView = [[MainViewController alloc] init]; 
[mainView navigateToTableViewPosition:contentOffset]; 

內容偏移一個CGPoint我預先設定。這裏沒關係;此外,它反正正確地通過。

回答

2

試試這個,如果你想改變你必須send notification其他viewcontroller ..

[[NSNotificationCenter defaultCenter] postNotificationName:@"changepostion"  object:NSStringFromCGPoint(CGPointMake(contentOffset.x, contentOffset.y))]; 

mainviewcontroller

-(void)viewDidLoad 

    { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(navigateToTableViewPosition:) name:@"changepostion" object:nil]; 

    } 

- (void) navigateToTableViewPosition:(NSNotification *)notification 
    { 
     contentOffset =CGPointFromString([notification object]); 

    NSLog(@"Position set method gets called..."); 
    NSLog(@"%@", NSStringFromCGPoint(contentOffset)); 
    [mainScrollView setContentOffset:contentOffset animated:YES]; 
} 
+0

工程就像一個魅力! –

0

您無法設置不可見的視圖的屬性。如果您使用的是iOS5 +,則可以在視圖關閉完成塊中完成偏移設置。

0

使用代表在視圖控制器落後的消息。

請參閱Basic Delegate Example鏈接以獲取更多參考。

您正在製作viewcontroller的新實例,它將調用方法,但不起作用。

+0

這似乎是答案,但我無法弄清楚如何設置委託。幾個月前,我做了類似的事情,我記得它非常簡單。我將UIView的委託設置爲另一個視圖控制器,然後只調用[delegate method:argument];等方法。但我不記得我是如何做到的,而且我不知道如何在這種情況下應用它。 –

相關問題