ios - 无法在我的 iOS 应用程序中使用共享按钮?

标签 ios objective-c xcode button share

我无法让共享按钮在我当前的应用程序中工作,我在我以前的应用程序中成功地拥有一个,但我无法在我的新应用程序中工作。我认为这可能是因为我的第一个应用程序只有一个 ViewController 而这个应用程序有多个。我试图在不同的 View Controller 中执行此操作,而不是默认的主视图 Controller 。不确定我做错了什么。

//  MoreMenuViewController.h

#import <UIKit/UIKit.h>

@interface MoreMenuViewController : UIViewController

- (IBAction)tellFriend:(id)sender;


//  MoreMenuViewController.m

#import "MoreMenuViewController.h"

@implementation MoreMenuViewController

- (IBAction)tellFriend:(id)sender 
{   
    NSString *shareText = [NSString stringWithFormat:@"Check out Stories With Friends, a new word game for iPhone!"]; // Share message
    NSArray *itemsToShare = @[shareText];
    UIActivityViewController *activityVC =
    [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = @[];

    [MoreMenuViewController:activityVC animated:YES completion:nil];
}

对于最后一行代码,我得到一个错误:没有已知的类方法用于选择器 'animated:completion:.'

非常感谢您的帮助,谢谢!

最佳答案

您需要从 self 而不是 MoreMenuViewController:

调用此方法
[self presentViewController:activityVC
                   animated:YES
                 completion:nil];

如果不需要排除类型,也不需要调用此 setter :

activityVC.excludedActivityTypes = @[];

更新:

Despite now being able to click the button the app crashes: -[MoreMenuViewController tellAFriend:]

这是因为你的方法的名称是 tellFriend: 而不是 tellAFriend: 你一定是在没有重构的情况下从代码中重命名了你的方法,所以链接的 IBAction 不知道改变。您需要做的是删除从 Storyboard到您的实现文件的链接。

单击您的按钮,然后单击操作链接到 tellAFriend 的十字:

enter image description here

关于ios - 无法在我的 iOS 应用程序中使用共享按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28634772/

相关文章:

objective-c - 更改 UITextField 键盘的色调

ios - 使用 UIAlertController 的项目。如何使用 Xcode 5.1 构建 IPA 文件?

ios - 在消息应用程序中以编程方式撰写消息时是否可以换行?

iphone - 让 UITableViewController 在旋转时刷新其 tableview 的最佳方法是什么?

ios - 如何在 iOS 中将父类(super class)转换为子类

objective-c - uisearchbar uisearchdisplaycontroller tableview 字幕错误

xcode - GCDAsyncSocket重新启动readDataToData内存增加

c++ - 如何摆脱警告 : Implicit conversion shortens 64-bit value into a 32-bit value

ios - 使用 CoreBluetooth 读取长特征值

ios - 如何在 iOS 中从 UITableView 播放选定的媒体项目?