How to connect to the action method for the iPad navigation bar button?

I use the split view template to create a simple split view, which of course is a popover in Portrait mode. I use the default code generated by a template that adds / removes a toolbar item and installs a popover controller and removes it. These two methods are splitViewController: willShowViewController: ... and splitViewController: willHideViewController: ...

I am trying to figure out how to make a popover disappear if the user clicks a button on the toolbar while the popover is displayed. You can make a popover disappear without selecting an element if you click anywhere outside the field, but I would also like it to disappear if the user clicks the button again.

Where I am stuck with this: There seems to be no obvious and easy way to connect to an action for a button on a toolbar. I can tell, using the debugger, that the action called on the button is showMasterInPopover. And I'm new to working with selectors programmatically, I admit.

Is there any way to write an action and install it on a toolbar item without overriding an action that already exists? for example, add an action that triggers the one that is now? Or do I need to write an action that popover itself shows / hides (behavior that is being performed behind the scenes, presumably using a split view controller now?).

Or did I skip the easy way to add this behavior to this button without modifying the existing behavior that is configured for me?

Thank!

+3
source share
6 answers

, , popover barButtonItem, SplitViewController willPresentViewController :

- (void) splitViewController:(UISplitViewController *)svc 
           popoverController: (UIPopoverController *)pc
   willPresentViewController: (UIViewController *)aViewController
{
    if (pc != nil) {
        [pc dismissPopoverAnimated:YES];
    }
}
+6

, barButtonItem UISplitViewController showMasterInPopover: . , , , , ( ) , :

- (void)showMasterInPopover:(id)sender {
    // ...insert custom stuff here...
    [splitViewController showMasterInPopover:sender];
}
+1

, .: - (

@Jann - , . , Notes, iPad, popover, .

0

. greenisus, UISplitViewController. , , popover . , , popover, , popover, UIPopoverControllerDelegate.

-, :

@interface LaunchScene : NSObject <UISplitViewControllerDelegate, UIPopoverControllerDelegate>
{
    UISplitViewController* _splitViewController;    //Shows list UITableView on the left, and details on the right
    UIToolbar* _toolbar;                            //Toolbar for the button that will show the popover, when in portrait orientation
    SEL _svcAction;                                 //The action from the toolbar
    id _svcTarget;                                  //The target object from the toolbar
    UIPopoverController* _popover;                  //The popover that might need to be dismissed
    BOOL _popoverShowing;                           //Whether the popover is currently showing or not
}

-(void) svcToolbarClicked: (id)sender;

_svcAction _svcTarget addess greenisus, , .

. , UISplitViewController subviews. / .

//the master view controller will be hidden so hook the popover
- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc 
{   
    _popoverShowing = FALSE;
    if(_toolbar == nil) 
    {
        //set title of master button
        barButtonItem.title = @"Title goes here";

        //Impose my selector in between the SVController, and the SVController default implementation
        _svcTarget = barButtonItem.target;
        _svcAction = barButtonItem.action;
        barButtonItem.target = self;
        barButtonItem.action = @selector(svcToolbarClicked:);

        //create a toolbar
        _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 1024, 44)];
        [_toolbar setItems:[NSArray arrayWithObject:barButtonItem] animated:YES];
    }

    //add the toolbar to the details view (the second controller in the splitViewControllers array)
    UIViewController* temp = [_splitViewController.viewControllers objectAtIndex:1];
    [temp.view addSubview:_toolbar];
}

, . , .

-(void) svcToolbarClicked: (id)sender
{
    if(_popoverShowing)
    {
        [_popover dismissPopoverAnimated:TRUE];
    }
    else 
    {
        //Perform the default SVController implementation
        [_svcTarget performSelector:_svcAction];
    }
    //Toggle the flag
    _popoverShowing = !_popoverShowing;
}

UISplitViewControllerDelegate

//the master view (non-popover) will be shown again (meaning it is going to landscape orientation)
- (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)button 
{
    //remove the toolbar
    [_toolbar removeFromSuperview];
}

// the master view controller will be displayed in a popover (i.e. the button has been pressed, and the popover is about to be displayed.  
//Unfortunately triggers when the popover is ALREADY displayed.
- (void)splitViewController:(UISplitViewController*)svc popoverController:(UIPopoverController*)pc willPresentViewController:(UIViewController *)aViewController 
{   
    _popover = pc; //Grab the popover object  
    _popover.delegate = self;
}

. , popover, , , _popoverShowing boolean , , popover. , UIPopoverControllerDelegate, , .

//UIPopoverControllerDelegate method
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    _popoverShowing = FALSE;
    _popover = nil;
}

, , ( ) UISplitViewController StackOverflow. , - . , .; -)

0

, - , , , ... , , , :

-(void)togglePopOverController {

if ([popOverController isPopoverVisible]) {

[popOverController dismissPopoverAnimated:YES];

} else {

[popOverController presentPopoverFromBarButtonItem:bbiOpenPopOver permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

}
0

:

popover , , - popover, , , .

, , , , - , . Human Interface ( , contstantly < > , , ). , .

, , UI- , , , Apple . UI-, , (: , //window. ).

, ( Apple). , , . : Apple ConvertBot The Odyssey: Trail of Tears ( ). (, , ):

"... iPhone/iPod , . iPhone, , iPhone, , .

, , : "?". , , , . , , . Apple 3 iPhoneOS, . , , , , , , . , -.

$.02

-1
source

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


All Articles