2015-04-03 18 views
2

我有以下代碼顯示一個動作片在當前可見視圖(self.view查找頂部所示視圖UIActionSheet

[actionSheet showInView:[self view]]; 

但是我'無法獲得在該應用這個動作表的引用使用委派:

UIView *topView = [[self.window subviews] lastObject]; 

回答

3

我相信,動作片是不是真的添加爲子視圖:

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    NSLog(@"%@, %@", actionSheet.superview, self.view); 
} 

因此,其中一種方法是在顯示操作表時顯示通知;例如:

// delegate = self; 
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ActionSheetPresented" object:nil userInfo:@{@"actionSheet": actionSheet}]; 
} 

而在AppDelegate

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayed:) name:@"ActionSheetPresented" object:nil]; 

- (void)displayed:(NSNotification *)notification 
{ 
    UIActionSheet *action = notification.userInfo[@"sheet"]; 
    NSLog(@"%@", action); 
} 

添加觀察員通知您也可能只是離開它是在AppDelegate視圖控制器和參考的公共屬性每當有didPresentActionSheet:委託API觸發。

+0

但如果添加新的操作表,我們應該關心添加通知,並且沒有像自動化過程那樣。因此,如果我們有另一個開發人員想要添加新的操作表,並且他不關心通知,他只想呈現該表,並且例如我們在什麼時候推動它應該工作而沒有任何問題。 – 2015-04-03 21:36:33

+0

也actionSheet.superview是零 – 2015-04-03 21:46:57

+0

所以'actionSheet.superview'是零暗示它沒有超級視圖;因此,如果您嘗試在'self.view'的某個子視圖中查找此操作表,則無法獲得任何結果。 如果您正在創建一個框架,那麼您可能會將其作爲委託API調用而不是通知。因此,不要發佈通知,請參閱您自己的委託調用。不幸的是,我似乎不知道一種不同的方式。 – p0lAris 2015-04-03 22:45:19