Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建标准的iOS共享按钮?_Ios_Cocoa Touch_Ios7_Uikit_Share - Fatal编程技术网

如何创建标准的iOS共享按钮?

如何创建标准的iOS共享按钮?,ios,cocoa-touch,ios7,uikit,share,Ios,Cocoa Touch,Ios7,Uikit,Share,: 使用系统提供的共享按钮。用户熟悉此按钮的含义和行为,因此最好在可能的情况下使用它。主要的例外情况是,如果你的应用程序不包含工具栏或导航栏[,因为]共享按钮只能在工具栏或导航栏中使用 好的,但是如何“使用系统提供的共享按钮”?没有发现有用的东西 我已经收集到了,但是我首先应该如何创建标准共享按钮呢?标准共享按钮是一个UIBarButtonItem(因此它只能放在导航栏或工具栏上)。你需要;明确地“操作”栏按钮项是共享按钮。以下是代码。我假设您在ViewController中,因此self具有n

:

使用系统提供的共享按钮。用户熟悉此按钮的含义和行为,因此最好在可能的情况下使用它。主要的例外情况是,如果你的应用程序不包含工具栏或导航栏[,因为]共享按钮只能在工具栏或导航栏中使用

好的,但是如何“使用系统提供的共享按钮”?没有发现有用的东西


我已经收集到了,但是我首先应该如何创建标准共享按钮呢?

标准共享按钮是一个UIBarButtonItem(因此它只能放在导航栏或工具栏上)。你需要;明确地“操作”栏按钮项是共享按钮。

以下是代码。我假设您在ViewController中,因此self具有navigationItem属性

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                     target:self
                     action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;

系统提供的操作按钮也可以使用界面生成器完全创建。要执行此操作,只需将UIToolbar拖放到视图中即可。然后将UIBarButtonim拖放到UIToolbar中。下一步,在视图层次结构中选择uiBarButtonim,转到属性检查器并选择“操作”作为系统项完成

此外,还可以将UIBarButtonItem与类连接为IBOutlet或IBAction


请注意:我使用Xcode 7作为参考。

这将出现在您的viewDidLoad中:

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                target:self
                                action:@selector(compartir:)];
self.navigationItem.rightBarButtonItem = shareButton;
并将选择器方法定义为action(在我的示例中称为“compartir”):


Main.storyboard->Bar按钮项->检查器->系统项
选择“操作”

以下是Swift 3的代码

func addShareBarButtonItem() {

    let shareButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: #selector(MyViewController.shareButtonPressed))

    self.navigationItem.rightBarButtonItem = shareButton
}

func shareButtonPressed() {
    //Do something now!
}

对于swift 4/Xcode 10,只有我的两美分:

1) 操作的初始字符已更改:

let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareButtonPressed))
2) 添加@obj:

@objc func shareButtonPressed() {
    //Do something now!
}

感谢您引用HIG,b/c链接被重定向,副本丢失:(
@objc func shareButtonPressed() {
    //Do something now!
}