Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Demo/LCActionSheetDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ - (void)viewDidLoad {
make.top.equalTo(self.view).offset(30.0 + ([[UIDevice currentDevice] lc_isX] ? 14.0 : 0));
make.height.offset(44.0);
}];

[NSTimer scheduledTimerWithTimeInterval:5 repeats:YES block:^(NSTimer * _Nonnull timer) {
printf("App's windows: %s\n", [[[UIApplication sharedApplication].windows description] cStringUsingEncoding:NSUTF8StringEncoding]);
printf("LCActionSheet isShowing: %d\n", (int)[LCActionSheet isShowing]);
printf("Showingsheet: %s\n", [[[LCActionSheet showingSheet] description] cStringUsingEncoding:NSUTF8StringEncoding]);
}];
}

- (void)viewWillAppear:(BOOL)animated {
Expand Down
22 changes: 22 additions & 0 deletions Sources/LCActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInte
*/
@property (nonatomic, assign) BOOL autoHideWhenDeviceRotated;

/**
The window, in which LCActionSheet is showing.
*/
@property (nullable, nonatomic, strong, readonly) UIWindow *window;

/**
LCActionSheet clicked handler.
Expand Down Expand Up @@ -458,6 +462,11 @@ typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInte
*/
- (void)show;

/**
Hide the instance of LCActionSheet.
*/
- (void)hide;

/**
Get button title with index

Expand All @@ -466,6 +475,19 @@ typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInte
*/
- (NSString *)buttonTitleAtIndex:(NSInteger)index;

/**
Is there an instance of LCActionSheet showing?
*/
+ (BOOL)isShowing;

/**
Get an instance of LCActionSheet, which is showing.
If there is no sheet showing, return nil.

@return An instance of LCActionSheet.
*/
+ (instancetype)showingSheet;

@end

NS_ASSUME_NONNULL_END
Expand Down
50 changes: 46 additions & 4 deletions Sources/LCActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ @interface LCActionSheet () <UITableViewDataSource, UITableViewDelegate, UIScrol

@property (nonatomic, weak) UIView *lineView;

@property (nullable, nonatomic, strong) UIWindow *window;

@end

@implementation LCActionSheet
Expand Down Expand Up @@ -771,7 +769,7 @@ - (void)show {
}
window.rootViewController = viewController;
[window makeKeyAndVisible];
self.window = window;
_window = window;

[viewController.view addSubview:self];
[self mas_makeConstraints:^(MASConstraintMaker *make) {
Expand Down Expand Up @@ -806,6 +804,10 @@ - (void)show {
}];
}

- (void)hide {
[self hideWithButtonIndex:0];
}

- (void)hideWithButtonIndex:(NSInteger)buttonIndex {
if ([self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self willDismissWithButtonIndex:buttonIndex];
Expand Down Expand Up @@ -836,7 +838,7 @@ - (void)hideWithButtonIndex:(NSInteger)buttonIndex {

strongSelf.window.rootViewController = nil;
strongSelf.window.hidden = YES;
strongSelf.window = nil;
_window = nil;

if ([strongSelf.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) {
[strongSelf.delegate actionSheet:strongSelf didDismissWithButtonIndex:buttonIndex];
Expand All @@ -854,6 +856,46 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
}

+ (UIWindow *)sheetWindow
{
/**
[UIApplication sharedApplication].windows is empty, when applicationWillEnterForeground.
*/
NSArray *array = [UIApplication sharedApplication].windows;
if (array.count > 0) {
for (int i = (int)array.count - 1; i >= 0; i--) {
UIWindow *window = [array objectAtIndex:i];
if ([window.rootViewController isKindOfClass:[LCActionSheetViewController class]]) {
return window;
}
}
} else {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if ([window.rootViewController isKindOfClass:[LCActionSheetViewController class]]) {
return window;
}
}

return nil;
}

+ (BOOL)isShowing
{
return ([LCActionSheet sheetWindow] != nil);
}

+ (instancetype)showingSheet
{
UIWindow *window = [LCActionSheet sheetWindow];
for (UIView *view in window.rootViewController.view.subviews) {
if ([view isKindOfClass:[LCActionSheet class]]) {
return (LCActionSheet *)view;
}
}

return nil;
}

#pragma mark - LCActionSheet & UITableView Delegate

- (void)darkViewClicked {
Expand Down