zoukankan      html  css  js  c++  java
  • 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

     

     

     

    运行图

  • 相关阅读:
    CUDA实例练习(十二):矩阵相乘
    CUDA实例练习(十一):零拷贝内存
    CUDA实例练习(十):多个cuda流
    CUDA实例练习(九):页锁定主机内存
    CUDA实例练习(八):原子操作(直方图)
    算法实例(三):快速排序
    算法实例(二):冒泡排序
    算法实例(一):简单桶排序
    CUDA实例练习(七):点积运算
    CUDA实例练习(六):矢量求和
  • 原文地址:https://www.cnblogs.com/liuhong/p/3276841.html
Copyright © 2011-2022 走看看