[英]How do I prevent UIPopoverController passthroughViews from being reset after orientation change when presented from a UIBarButtonItem?
我有一個UIPopoverController,它是從UIBarButtonItem呈現的。 我想在popover之外觸摸以解除popover。 當從條形按鈕呈現彈出框時,其他條形按鈕將自動包含在彈出框直通視圖中。 為了防止我在呈現彈出窗口后將passthrough視圖設置為nil(或@ []),如下所示:
- (IBAction) consoleBarButtonHit:(id)sender {
UIViewController *consoleNavigationController=[self.storyboard instantiateViewControllerWithIdentifier:@"consoleNavigationController"];
_consolePopoverController=[[UIPopoverController alloc] initWithContentViewController:consoleNavigationController];
_consolePopoverController.delegate=self;
[_consolePopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// this must be done _after_ presenting the popover in order to work
_consolePopoverController.passthroughViews=nil;
}
這一切都很好,但我遇到的問題是,在彈出設備后,當彈出窗口可見時,條形按鈕會自動重新添加為直通視圖,並且不會導致彈出窗口被解除 。
如果我能以某種方式獲得條形按鈕視圖(或矩形),那么我可以使用彈出窗口
-presentPopoverFromRect:inView:permittedArrowDirections:animated:
這可能會解決這個問題,但我不知道從UIBarButtonItem找到rect / view的任何非hackish方式。
我真的不希望當其他按鈕被按下時調用的選擇器以編程方式關閉彈出窗口,這不是他們的責任,並且可能會在以后引起我的問題。
有任何想法嗎?
所以我提出了一個解決方案,這有點奇怪,但保持模塊化,運行良好。 我創建了一個名為PropertyEnforcer的類,它將自己注冊為對象屬性的KVO觀察者,並在其更改時重新設置該屬性。
PropertyEnforcer.h:
#import <Foundation/Foundation.h>
@interface PropertyEnforcer : NSObject
+ (void) enforceProperty:(NSString*)keyPath ofObject:(id)target toValue:(id)value;
@end
PropertyEnforcer.m:
#import "PropertyEnforcer.h"
#import <objc/runtime.h>
@interface PropertyEnforcer ()
@property (retain) NSString *keyPath;
@property (retain) id value;
@property (assign) id target;
@end
@implementation PropertyEnforcer
- (void) dealloc {
[_target removeObserver:self forKeyPath:_keyPath context:NULL];
[super dealloc];
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if( (([_target valueForKey:_keyPath] == nil) && (_value==nil)) || [[_target valueForKey:_keyPath] isEqual:_value]) {
return;
} else {
[_target setValue:_value forKeyPath:_keyPath];
}
}
+ (void) enforceProperty:(NSString*)keyPath ofObject:(id)target toValue:(id)value {
PropertyEnforcer *enforcer=[[PropertyEnforcer alloc] init];
enforcer.value=value;
enforcer.keyPath=keyPath;
enforcer.target=target;
[target addObserver:enforcer forKeyPath:keyPath options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:NULL];
objc_setAssociatedObject(target,
_cmd, // using this technique we can only attach one PropertyEnforcer per target
enforcer,
OBJC_ASSOCIATION_RETAIN);
[enforcer release];
}
@end
現在我可以將原始代碼的更改更改為:
- (IBAction) consoleBarButtonHit:(id)sender {
UIViewController *consoleNavigationController=[self.storyboard instantiateViewControllerWithIdentifier:@"consoleNavigationController"];
_consolePopoverController=[[UIPopoverController alloc] initWithContentViewController:consoleNavigationController];
_consolePopoverController.delegate=self;
[_consolePopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// make sure those passthroughViews are always nil !
[PropertyEnforcer enforceProperty:@"passthroughViews" ofObject:_consolePopoverController toValue:nil];
}
PropertyEnforcer將自身注冊為關聯對象,因此我們無需跟蹤它。 它會自動注銷自己作為KVO觀察者,並在UIPopoverController被銷毀時被銷毀。
這是我能提出的最好,最不具有解決方案的解決方案。
我使用的解決方案是單獨保留passthroughViews
,而是在UIPopoverPresentationController
根據其轉換呈現和解除時,禁用/重新啟用工具欄或導航欄中的各個按鈕( UIBarButtonItem
實例)。
(iOS 8: UIPopoverPresentationController
而不是UIPopoverController
。)
UIPopoverPresentationController + managedBarButtonItems.h
@interface UIPopoverPresentationController (managedBarButtonItems)
@property (nonatomic, retain) NSArray* managedBarButtonItems;
@end
UIPopoverPresentationController + managedBarButtonItems.m
#import "UIPopoverPresentationController+managedBarButtonItems.h"
#import <objc/runtime.h>
//
// scope: private, in-terms-of
//
@interface UIBarButtonItem (wasEnabled)
@property (nonatomic) BOOL wasEnabled;
@end
@implementation UIBarButtonItem (wasEnabled)
- (BOOL)wasEnabled
{
return [objc_getAssociatedObject(self, @selector(wasEnabled)) boolValue];
}
- (void)setWasEnabled:(BOOL)wasIt
{
objc_setAssociatedObject(self, @selector(wasEnabled), [NSNumber numberWithBool:wasIt], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// FYI: "Associated objects are released [automatically] after the dealloc method of the original object has finished."
@end
//
// scope: consumable
//
@implementation UIPopoverPresentationController (managedBarButtonItems)
- (NSArray*)managedBarButtonItems
{
return objc_getAssociatedObject(self, @selector(managedBarButtonItems));
}
- (void)setManagedBarButtonItems:(NSArray*)items
{
objc_setAssociatedObject(self, @selector(managedBarButtonItems), items, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// FYI: "Associated objects are released [automatically] after the dealloc method of the original object has finished."
- (void)presentationTransitionDidEnd:(BOOL)completed
{
[super presentationTransitionDidEnd:completed];
if (self.barButtonItem && self.managedBarButtonItems)
{
for (UIBarButtonItem* button in self.managedBarButtonItems)
{
if (button.action != /* actuator */ self.barButtonItem.action)
{
button.wasEnabled = button.enabled, button.enabled = NO;
}
}
}
}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
[super dismissalTransitionDidEnd:completed];
if (self.barButtonItem && self.managedBarButtonItems)
{
for (UIBarButtonItem* button in self.managedBarButtonItems)
{
if (button.action != /* actuator */ self.barButtonItem.action)
{
button.enabled = button.wasEnabled;
}
}
}
}
@end
用法:
UIAlertController* actionSheet = [UIAlertController
alertControllerWithTitle:@"Actions" message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIPopoverPresentationController* presenter = actionSheet.popoverPresentationController;
// chosen anchor UIBarButtonItem
presenter.barButtonItem = anchorButton;
// disabled UIViewController buttons
presenter.managedBarButtonItems = self.toolbarItems;
也可能:
// disabled UINavigationController buttons
presenter.managedBarButtonItems =
[[NSArray arrayWithArray:self.navigationItem.leftBarButtonItems]
arrayByAddingObject:self.navigationItem.rightBarButtonItem];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.