• 新浪微博客户端(37)-发布新微博


    DJComposeViewController.m

    #import "DJComposeViewController.h"
    #import "DJAccountTool.h"
    #import "DJTextView.h"
    #import "AFHTTPSessionManager.h"
    #import "MBProgressHUD+MJ.h"
    
    
    @interface DJComposeViewController()
    
    @property (nonatomic,weak) DJTextView *inputView;
    
    @end
    
    @implementation DJComposeViewController
    
    
    
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
        [self initNavigationView];
        [self initInputView];
    
    }
    
    
    - (void)viewDidAppear:(BOOL)animated {
    
        [super viewDidAppear:animated];
         self.navigationItem.rightBarButtonItem.enabled = NO;
        
    }
    
    
    /** 初始化NavigationView */
    - (void)initNavigationView {
    
        self.view.backgroundColor = [UIColor whiteColor];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(finish)];
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(send)];
        
        
        
        
        UILabel *titleView = [[UILabel alloc] init];
        titleView.width = 200;
        titleView.height = 44;
        titleView.numberOfLines = 0; // 设置titleView 为多行显示
        titleView.textAlignment = NSTextAlignmentCenter;
        
        DJAccount *account = [DJAccountTool account];
        NSString *nickName = account.screen_name;
        NSString *prefix = @"发微博";
        NSString *str = [NSString stringWithFormat:@"%@
    %@",prefix,nickName];
        NSRange nick_name_range = [str rangeOfString:nickName];
        NSRange prefix_range = [prefix rangeOfString:prefix];
        
        NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:str];
        [titleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:nick_name_range];
        [titleStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:prefix_range];
        
        
        titleView.attributedText = titleStr;
        self.navigationItem.titleView = titleView;
        
    }
    
    
    
    /** 初始化输入区域 */
    - (void)initInputView {
    
        DJTextView *inputView = [[DJTextView alloc] init];
        inputView.frame = self.view.bounds;
        inputView.font = [UIFont systemFontOfSize:14];
        inputView.placeholder = @"请输入微博内容";
        inputView.placeholderColor = [UIColor grayColor];
        [self.view addSubview:inputView];
        self.inputView = inputView;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textHasChange) name:UITextViewTextDidChangeNotification object:inputView];
    
    }
    
    
    
    - (void)finish {
    
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
    
    
    /** 监听TextView文本改变 */
    - (void)textHasChange {
        
        // 若用户已经为textView输入了文本,则发送按钮可点击
        self.navigationItem.rightBarButtonItem.enabled = self.inputView.hasText;
    
    }
    
    
    /** 发微博 */
    - (void)send {
    
        [self sendStatusRequest];
        
    }
    
    
    
    /** 发微博 */
    - (void)sendStatusRequest {
    
        AFHTTPSessionManager *RequestManager = [AFHTTPSessionManager manager];
        NSString *urlString = @"https://api.weibo.com/2/statuses/update.json";
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"access_token"] = [DJAccountTool account].access_token;
        params[@"status"] = self.inputView.text;
        
        [RequestManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [MBProgressHUD showSuccess:@"发送成功"];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [MBProgressHUD showError:@"发送失败"];
        }];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    
    - (void)dealloc {
    
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    }
    
    
    
    @end

    最终效果:

  • 相关阅读:
    设计一种配置文件格式(草稿)
    linux shell控制语句
    CuteC 发布(2011519)
    国外兼职外包项目大全
    寻找第K大的数的方法总结
    IEEE PDF eXpress 使用
    找出一个不在文件中的整数 编程珠玑
    BM模式匹配算法实现(C语言)
    一种可做特殊用途的字符串匹配算法
    字符设备驱动程序
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/6099784.html
Copyright © 2020-2023  润新知