• Presenting Sharing Options with UIActivityViewController


    需要实例化UIActivityViewController这个类,通过initWithActivityItems:applicationActivities: 方法:

    initWithActivityItems:要分享的东西,可以是NSString, UIImage, 自己定义的符合UIActivityItemSource 协议的类的实例。

    applicationActivities:代表这项分享的动作你自动的应用可以支持

    将用户输入的文字发送到UIActivityViewController类的实例化

    头文件

    #import <UIKit/UIKit.h>

     

    @interface ViewController : UIViewController<UITextFieldDelegate>

     

    @end

     

    实现文件

    #import "ViewController.h"

     

    @interfaceViewController ()

     

    @property(nonatomic,strong) UITextField *textField;

    @property(nonatomic,strong) UIButton *buttonShare;

    @property(nonatomic,strong) UIActivityViewController *acticityViewController;

     

    @end

     

    @implementation ViewController

     

    - (void) createTextField {

        self.textField = [[UITextFieldalloc] initWithFrame:CGRectMake(20.0f, 30.0f, 280.0f, 30.0f)];

        self.textField.translatesAutoresizingMaskIntoConstraints = NO;

        self.textField.borderStyle = UITextBorderStyleRoundedRect;

        self.textField.placeholder = @"Enter Text to share";

        self.textField.delegate = self;

        [self.view addSubview:self.textField];

    }

     

    - (void) createButton {

        self.buttonShare = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];                //子类用这个方法不能返回一个button

        self.buttonShare.translatesAutoresizingMaskIntoConstraints = NO;

        self.buttonShare.frame = CGRectMake(20.0f, 80.0f, 280.0f, 35.0f);

        [self.buttonSharesetTitle:@"Share"forState:UIControlStateNormal];

        [self.buttonShareaddTarget:selfaction:@selector(handlerShare:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:self.buttonShare];

    }

     

    //UITextFieldDelegate中的方法,用于dismiss keyboard

    - (BOOL) textFieldShouldReturn:(UITextField *)textField {

        [textField resignFirstResponder];

        returnYES;

    }

    - (void) handlerShare: (id)paramSender {

        if ([self.textField.text length] == 0) {

            NSString *message = @"please enter a text and then press the share button";

            UIAlertView *alertView =[[UIAlertViewalloc] initWithTitle:nilmessage:message delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];

            [alertView show];

            

            return;

        }

        

        self.acticityViewController = [[UIActivityViewControlleralloc]initWithActivityItems:@[self.textField.text]applicationActivities:nil];

        [selfpresentViewController:self.acticityViewControlleranimated:NOcompletion:^{

            

        }];

    }

    - (void)viewDidLoad

    {

        [superviewDidLoad];

        [selfcreateTextField];

        [selfcreateButton];

    }

     

    - (void)didReceiveMemoryWarning

    {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

     

     

     

    运行图

  • 相关阅读:
    代码片段:ASCII纯数字文本数据转二进制格式
    Caffe使用CMake编译:Could Not find Boost
    Cannot uninstall '***'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
    Ubuntu 14.04避雷系列:升级python到最新2.x版本
    过程记录:安装原版windows7
    代码片段:Shell脚本实现重复执行和多进程
    代码片段:基于STL实现的读取纯数字文本文件
    E: GPG 错误:http://developer.download.nvidia.com Release: 下列签名无效: NODATA 1 NODATA 2
    软件项目管理复习题要
    html转义字符表
  • 原文地址:https://www.cnblogs.com/liuhong/p/3276841.html
Copyright © 2020-2023  润新知