UIMenuItem禁用方法上的自动选择器

时间:2016-03-31 18:21:59

标签: ios swift uimenucontroller uimenuitem

我遇到创建新UIMenuItem并为其指定选择器的问题。问题是它是否自动调用其选择器而不点击它。 这是我的代码:

[XmlType(AnonymousType = true)]
public class Children: XmlEntityBase, IChildren
{
 [XmlElement("Children", IsNullable = false)]
 public List<ChildrenItems> ChildrenItems { get; } = new List<ChildrenItems>();
}

[XmlType(AnonymousType = true)]
public class ChildrenItem: XmlEntityChoice, IChildrenItem
{
 [NotMapped]
 [XmlElement(nameof(Activity), Type = typeof(Activity), IsNullable = false)]
 [XmlElement(nameof(Container), Type = typeof(Container), IsNullable = false)]
 public object SelectedValue
 {
  get { return _selectedValue; }
  set 
  { 
   if(_selectedValue == value) return;
    SetValue(value);
  }
 }

 [XmlIgnore]
 public Activity Activity 
 {
  get 
  {
   if(ValueType == typeof(Activity))
   {
    return (Activity)SelectedValue;
   }
   throw new ChoiceTypeException($"Invalid Type: {nameof(Activity)}!");
  }
 }

 [XmlIgnore]
 public Container Container
 {
  get
  {
   if(ValueType == typeof(Container)
   {
    return (Container)SelectedValue;
   }
   throw new ChoiceTypeException($"Invalid Type: {nameof(Container)}!");
  }
 }
}

这是出现menuitem的方法:

let customMenuItem1 = UIMenuItem(title: "Salvează", action: Selector(showNote()))
    menuController.menuItems = NSArray(array: [customMenuItem1]) as? [UIMenuItem]

谢谢大家。

1 个答案:

答案 0 :(得分:1)

在前两行代码中存在swift中的错误:

let customMenuItem1 = UIMenuItem(title: "Salvează", action: Selector(showNote()))
menuController.menuItems = NSArray(array: [customMenuItem1]) as? [UIMenuItem]

听到我们在方法上有Selector,这个选择器意味着他将在不等待用户点击的情况下通过电话方式调用方法,只有我们可以这样解决这个问题

 let customMenuItem1 = UIMenuItem(title: "Salvează", action: #selector(RulesDetailViewController.showNote))
    menuController.menuItems = NSArray(array: [customMenuItem1]) as? [UIMenuItem]

因为#selector这个参数等待用户的触摸和事件。