2011-02-02 57 views
2

我正在添加一個UIView到窗口,而一些線程後臺更新正在發生,然後使用委託方法刪除視圖。一切都按預期發生,但在hideActivityViewer被調用後,視圖仍會持續幾秒鐘。不知道它是否重要,但應用程序使用UITabBarController。UIView緩慢地從超級視圖中刪除

更新方法在一個單獨的類中,但目前在AppDelegate.m中用於調試目的。正如我所說,一切正常。更新完成後,它將「Foo」寫入日誌,但視圖持續幾秒鐘。任何幫助,將不勝感激。多餘的代碼已被省略:

AppDelegate.h

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { 
    UIWindow *window; 
    UITabBarController *tabBarController; 
    UIView *activityView; 
    id _delegate; 
} 
@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) UITabBarController *tabBarController; 
- (void)showActivityViewer; 
- (void)updateComplete; 
- (void)updateRemoteDataThreadedWithDelegate:(id)aDelegate; 
- (id)delegate; 
- (void)setDelegate:(id)new_delegate; 
@end 

AppDelegate.m

- (void)updateRemoteDataThreadedWithDelegate:(id)aDelegate { 
    [self setDelegate:aDelegate]; 
    NSOperationQueue *queue = [NSOperationQueue new]; 
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateRemoteDataWithDelegate:) object:aDelegate]; 
    [queue addOperation:operation]; 
    [operation release]; 
} 

- (void)updateRemoteDataWithDelegate:(id)aDelegate { 
    [self setDelegate:aDelegate]; 
    ...do stuff... 
    if ([_delegate respondsToSelector:@selector(updateComplete)]) { 
    [_delegate updateComplete]; 
    } else { 
    [NSException raise:NSInternalInconsistencyException format:@"Delegate doesn't respond to updateComplete"]; 
    } 
} 

-(void)showActivityViewer { 
    [activityView release]; 
    activityView = [[UIView alloc] initWithFrame: CGRectMake(window.bounds.size.width-50, 60, 50, 50)]; 
    ...formatting... 
    [window addSubview: activityView]; 
    [activityView release]; 
} 
-(void)hideActivityViewer { 
    [activityView removeFromSuperview]; 
    activityView = nil; 
    NSLog(@"Foo"); 
} 

- (id)delegate { 
    return _delegate; 
} 

- (void)setDelegate:(id)new_delegate { 
    _delegate = new_delegate; 
} 

回答

3

它看起來像你對我是導致一個線程調用哪個做的東西,在一個UIView的委託方法 - 你不能這樣做,因爲UIView不是線程安全的。

使用performSelectorOnMainThread安全地執行此操作 - 您的委託方法可以通過這種方式調用主線程上的另一個方法。

+0

謝謝。我想我應該在這裏浪費兩天時間來問這裏。發現。 =] – SalsaShark 2011-02-02 22:49:12

3

UI操作應該在主線程上完成;你的例子將UIView推到一個單獨的線程上,這是不推薦的。

相關問題