IOS: pop-up menu does not behave according to the set of first responders

I have several objects inheriting UIViewin my application that track branches to them and present a Copy / Paste popup if they contain some specific data. When a popup is displayed, I also change the appearance of the object.

Here's how it is implemented:

- (void)viewDidReceiveSingleTap:(NSNotification *)n {
    MyObject *mo = (MyObject *)n.object;

    [mo becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu update];

    [menu setTargetRect:CGRectMake(...) inView:mo];

    [menu setMenuVisible:YES animated:YES];
}

MyObjectthe class, in turn, defines canBecomeFirstResponder:and canResignFirstResponder:as always returns YES. becomeFirstResponder:, resignFirstResponder:and are canPerformAction:withSender:also defined accordingly (here I change the appearance of the object).

Here's what went wrong:

I click object 1. The method viewDidReceiveSingleTap:above is called , and the object canBecomeFirstResponder:and is becomeFirstResponder:also called. A popup menu is displayed as expected.

2. viewDidReceiveSingleTap: , . -, canResignFirstResponder:, resignFirstResponder: 1 , , . canBecomeFirstResponder: becomeFirstResponder: 2 , . ( viewDidReceiveSingleTap: setMenuVisible:YES). , 2 ( ) - , 2 , , .

? ?

+3
1

, , , . . , , , ( , - ), , .

, , , . , , , . , :

[menu setMenuVisible:NO animated:NO];
[menu setMenuVisible:YES animated:YES];

, , :

@implementation MenuView

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blueColor];
    }
    return self;
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    UIMenuItem *item = [UIMenuItem alloc] initWithTitle:@"Test"
                                                 action:@selector(test)];
    NSArray *items = [NSArray arrayWithObject:item];
    [item release];
    [menu setMenuItems:items];

    [menu setTargetRect:self.bounds inView:self];

    [menu setMenuVisible:NO animated:NO];
    [menu setMenuVisible:YES animated:YES];
}

- (void)test {
    NSLog(@"Test!");
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return action == @selector(test);
}

- (BOOL)canBecomeFirstResponder {
    NSLog(@"Can become called");
    return YES;
}

- (BOOL)canResignFirstResponder {
    NSLog(@"Can resign called");
    return YES;
}

- (BOOL)becomeFirstResponder {
    [super becomeFirstResponder];
    NSLog(@"Become called");

    return YES;
}

- (void)dealloc {
    [super dealloc];
}


@end
+1

Source: https://habr.com/ru/post/1785476/


All Articles