2014-10-04 27 views
0

提取從self.window視圖在我:在Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions: 

我設置以下scrollView

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect]; 
[self.window addSubview:scrollView]; 

在以後的方法,我怎麼會那麼毀了這個觀點,也就是從self.window中刪除它。

+0

我假設你已經嘗試存儲對它的引用並調用它'removeFromSuperview'? – Undo 2014-10-04 20:55:33

回答

1

通常我不會在application:didFinishLaunchingWithOptions:中添加scrollView。相反,請在視圖控制器中創建UIScrollView,或者在viewDidLoad方法中或在故事板中創建UIScrollView

無論哪種方式,刪除scrollView將看起來像這樣。首先創建一個屬性來存儲參考UIScrollView

@property (nonatomic, weak) UIScrollView *scrollView; // Use a weak reference to the scrollview, that way once its removed from the superview (which keeps a strong reference to its subviews), the scrollview will be deallocated. 

然後初始化屬性,並將其添加爲子視圖:

self.scrollView = [[UIScrollView alloc] initWithFrame:screenRect]; 
[self.window addSubview:scrollView]; 

之後,你可以將其刪除:

[self.scrollView removeFromSuperview];