zoukankan      html  css  js  c++  java
  • UI: 使用 UIActivityViewController 显示分享选项

    问题:创建一个 UIActivityViewController 类的实例对象,通过该类进行内容分享 
    在 iOS 中分享数据是很容易的。你需要做的就是使用 UIActivityViewController 类的方法 initWithActivityItems:applicationActivities:实例化一个对象,这个方法的参数如下: 
    initWithActivityItems

    这个 items 数组是你想要分享的内容。可以是 NSString,UIImage,或者任意遵循 UIActivityItemSource 协议的自定义类。 

    applicationActivities 

     这是一个 UIActivity 实例数组,代表程序中支持的 activities。 

     例子:

    创建文本框和按钮:

    - (void)createTextField{
        self.textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 35, 280, 30)];
        //?
        //self.textField.translatesAutoresizingMaskIntoConstraints = NO;
        self.textField.borderStyle = UITextBorderStyleRoundedRect;
        self.textField.placeholder = @"输入分享内容";
        self.textField.delegate = self;
        [self.view addSubview:self.textField];
        
    }
    - (void)createButton{
        self.buttonShare = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        //self.buttonShare.translatesAutoresizingMaskIntoConstraints = NO;
        self.buttonShare.frame = CGRectMake(20, 80, 280, 44);
        [self.buttonShare setTitle:@"Share`" forState:UIControlStateNormal];
        [self.buttonShare addTarget:self action:@selector(handleShare:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.buttonShare];
    }

    在viewDidLoad调用两个方法即可

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor whiteColor];
        [self createButton];
        [self createTextField];
    }

    点击键盘上的Return按钮隐藏键盘

    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
        [textField resignFirstResponder];
        return YES;
    }

    点击share按钮调用的方法:将内容通过UIActivityViewController进行分享:

    - (void)handleShare:(id)paramSender{
        if ([self.textField.text length]== 0) {
            NSString *message = @"Please enter a text and then press Share";
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertView show];
            return;
        }
    //重要
        self.activityViewController = [[UIActivityViewController alloc]initWithActivityItems:@[self.textField.text] applicationActivities:nil];
        [self presentViewController:self.activityViewController animated:YES completion:^{
            
        }];
    }
  • 相关阅读:
    出现socket:(10107)系统调用失败
    JS面向对象基础讲解(工厂模式、构造函数模式、原型模式、混合模式、动态原型模式)
    获取滚动条距离底部的距离
    linux常用命令使用方法
    Python:一
    【C++ Primer 第15章】定义派生类拷贝构造函数、赋值运算符
    【【C++ Primer 第15章】 虚析构函数
    ubuntu基本用法
    深度优先搜索(DFS)和广度优先搜索(BFS)
    【C++ Primer 第7章】定义抽象数据类型
  • 原文地址:https://www.cnblogs.com/safiri/p/4018431.html
Copyright © 2011-2022 走看看