• 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

     

     

     

    运行图

  • 相关阅读:
    二叉树中序遍历 2021.4.14
    简单题 好数对题目 2021.4.13
    2021.4.3 在排序数组中查找元素的第一个和最后一个位置
    力扣刷题记录2021.4.13 最富有客户的资产总量
    iOS开源项目阅读整理
    Missing iOS Distribution signing identity解决方案
    微信票选项目遇到的坑
    [转]从此爱上iOS Autolayout
    [转]iOS应用性能调优的25个建议和技巧
    iOS中用json接收图片的二进制流
  • 原文地址:https://www.cnblogs.com/liuhong/p/3276841.html
Copyright © 2020-2023  润新知