IOS Uimenucontroller using an instance of the menu component _ios

Source: Internet
Author: User

The use of Uimenucontroller

Uimenucontroller's presentation needs to be based on a view, whose interaction needs to be based on the responder of the view in which it resides. For example, if a uimenucontroller is displayed on the current Viewcontroller view, the Uimenucontroller interaction logic is managed by the current Viewcontroller.

3 conditions are required to display Uimenucontroller in the interface:

1. The current responder is in the first response.

The 2.UIMenuController object invokes the Menuvisible method.

3. The current responder implements the following two methods:

Can be the first corresponding
-(BOOL) canbecomefirstresponder{return
  YES;
Can I receive some of the interactive operations of some menus
-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{return
    YES;

Implemented the above two methods, use the following code to call out the Uimenucontroller control:

  [Self becomefirstresponder];
  Set the location of the menu display frame set its civilian InView to its view
  [[Uimenucontroller Sharedmenucontroller] settargetrect:frame InView: Self.view];
  Set the menu control to visible
  [Uimenucontroller sharedmenucontroller].menuvisible = YES;

After executing the above code, the system calls the Canperformaction:withsender the first time: The method does not display the menu bar detection, and if returned as no, the menu bar cannot be displayed, and if you return Yes, The system then calls the Canperformaction:withsender: method to detect whether the current responder object implements the trigger method for an option on the menu bar, and if it does, the corresponding button on the menu bar appears, otherwise it is not displayed. In this method, the developer can determine the type of button that is displayed in the menu control by judging the action. The system defaults to a series of menu buttons for developers, such as menu buttons to display cut and assign operations, with the sample code as follows:

-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
  if (action = @selector (cut:) | | Action = = @selector (copy:)) {return
    YES
  }
  return NO;
}

The effect is shown in the following illustration:

The button-triggering methods provided by the system default support are listed as follows:

Method of cutting button
-(void) Cut: (Nullable ID) sender Ns_available_ios (3_0);
Method of Copying a button
-(void) copy: (Nullable ID) sender Ns_available_ios (3_0);
The method of pasting a button
-(void) paste: (Nullable ID) sender Ns_available_ios (3_0);
Select the method of the button
-(void) Select: (Nullable ID) sender Ns_available_ios (3_0);
The method for selecting all buttons
-(void) SelectAll: (Nullable ID) sender Ns_available_ios (3_0);
Method of Deleting a button
-(void) Delete: (Nullable ID) sender Ns_available_ios (3_2);
Change the writing mode to trigger from left to right button
-(void) Maketextwritingdirectionlefttoright: (Nullable ID) sender Ns_available_ios (5_0);
Change the writing mode to trigger from right to left button
-(void) Maketextwritingdirectionrighttoleft: (Nullable ID) sender Ns_available_ios (5_0);

The method listed above is stated in the Uiresponder header file, and in fact, in addition to the above method, there are many private methods in the system, listed below, on the Uimenucontroller button above:

Replace button
-(void) _promptforreplace: (ID) arg1{
  NSLog (@ "Promptforreplace");
}
Simplified traditional conversion button
-(void) _transliteratechinese: (ID) sender{
  NSLog (@ "Transliteratechinese");
}
Text Style button
-(void) _showtextstyleoptions: (ID) sender{
  NSLog (@ "showtextstyleoptions");
}
Definition button
-(void) _define: (ID) sender{
  NSLog (@ "define");
}
-(void) _addshortcut: (ID) sender{
  NSLog (@ "Addshortcut");
}
-(void) _accessibilityspeak: (ID) sender{
  NSLog (@ "Accessibilityspeak");
}
Language selection button
-(void) _accessibilityspeaklanguageselection: (ID) sender{
  NSLog (@) Accessibilityspeaklanguageselection ");
}
Pause Pronunciation button
-(void) _accessibilitypausespeaking: (ID) sender{
  NSLog (@ "accessibilitypausespeaking");
}
Share button
-(void) _share: (ID) sender{
  NSLog (@ "share");
}

Instance Advanced

In actual development, development does not require the use of these proprietary methods, the UIMenuItem class provides developers with custom menu buttons and triggering methods, examples are as follows:

[Self becomefirstresponder];
  UIMenuItem * item = [[UIMenuItem alloc]initwithtitle:@ ' Custom ' action: @selector (Newfunc)];
  [[Uimenucontroller Sharedmenucontroller] settargetrect:[sender frame] inView:self.view];
  [Uimenucontroller sharedmenucontroller].menuitems = @[item];
  [Uimenucontroller sharedmenucontroller].menuvisible = YES;
-(BOOL) canbecomefirstresponder{return
  YES;
-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
  if (action = = @selector (newfunc)) {return
    YES;
  } return
  NO;
}
-(void) newfunc{
  NSLog (@ "Custom method");
}

The effect is shown in the following illustration:

Uimenucontroller also has the following properties to set the location of the display:

The position shown
@property (nonatomic) uimenucontrollerarrowdirection arrowdirection;
Enumerated below:
/* typedef ns_enum (Nsinteger, uimenucontrollerarrowdirection) {
  //default based on current screen state
  Uimenucontrollerarrowdefault,//up or down based in screen location
  //arrow on display mode
  uimenucontrollerarrowup ns_ Enum_available_ios (3_2),//
  arrow in the following display mode
  Uimenucontrollerarrowdown Ns_enum_available_ios (3_2),
  // Arrow in left display mode
  uimenucontrollerarrowleft Ns_enum_available_ios (3_2),
  //arrow in right display mode
  Uimenucontrollerarrowright Ns_enum_available_ios (3_2),
};
*/


Note Point Summary
to display the menu normally, you must do the following:
1.-(BOOL) Canbecomefirstresponder must return Yes

2.-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender
In this function, the method to display the menu item (including the system's menu item) must return Yes

3. Before displaying the menu, you must call:

[Self Becomefirstresponder] 

Be the first responder

4. In order to immediately display the second menu correctly, you must use:

  [Menucontroller Setmenuvisible:no];

Close it first, or it won't show!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.