iOS開發json
在作發佈做品的數據框時,須要自定義一個TextView,由於自帶的不能知足需求:api
UITextField : 不能換行緩存
UITextView :沒有提示文字app
IWComposeViewController.m 發微博工具
// // IWComposeViewController.m // ItcastWeibo // // Created by apple on 14-5-19. // Copyright (c) 2014年 itcast. All rights reserved. // /** UITextField : 不能換行 UITextView :沒有提示文字 */ #import "IWComposeViewController.h" #import "IWTextView.h" @interface IWComposeViewController () @property (nonatomic, weak) IWTextView *textView; @end @implementation IWComposeViewController - (void)viewDidLoad { [super viewDidLoad]; // 設置導航欄屬性 [self setupNavBar]; // 添加textView [self setupTextView]; } /** * 添加textView */ - (void)setupTextView { // 1.添加 IWTextView *textView = [[IWTextView alloc] init]; textView.font = [UIFont systemFontOfSize:15]; textView.frame = self.view.bounds; [self.view addSubview:textView]; // 讓textView成爲第一響應者,彈出鍵盤 [textView becomeFirstResponder]; self.textView = textView; // 2.監聽textView文字改變的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:textView]; } /** * 監聽文字改變 */ - (void)textDidChange { self.navigationItem.rightBarButtonItem.enabled = (self.textView.text.length != 0); // self.navigationItem.rightBarButtonItem.enabled = self.textView.text.length; } /** * 設置導航欄屬性 */ - (void)setupNavBar { self.view.backgroundColor = [UIColor whiteColor]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"發送" style:UIBarButtonItemStyleDone target:self action:@selector(send)]; self.navigationItem.rightBarButtonItem.enabled = NO; self.title = @"發微博"; } /** * 取消 */ - (void)cancel { [self dismissViewControllerAnimated:YES completion:nil]; } /** * 發微博 */ - (void)send { } @end
2. 在IWAppDelegate.m代理中監聽內存警告,並清除緩存的圖片:atom
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application { // 中止下載全部圖片 [[SDWebImageManager sharedManager] cancelAll]; // 清除內存中的圖片 SDWebImageManager會作內存緩存和硬盤緩存 [[SDWebImageManager sharedManager].imageCache clearMemory]; }
三、編輯文本框,當用戶進入到發微博頁面時,須要彈出鍵盤,而不是須要用戶點擊纔出現鍵盤。spa
/** * 添加textView */ - (void)setupTextView { // 1.添加 IWTextView *textView = [[IWTextView alloc] init]; textView.font = [UIFont systemFontOfSize:15]; textView.frame = self.view.bounds; [self.view addSubview:textView]; // *********讓textView成爲第一響應者,彈出鍵盤******** [textView becomeFirstResponder]; self.textView = textView; // 2.監聽textView文字改變的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:textView]; }
咱們能夠看到,在點擊+號進入發佈微博界面,有一些稍微的卡頓,這是由於view未所有加載完而彈出鍵盤,爲了用戶體驗,這裏這樣作:等textView加載完後,在彈出鍵盤。代理
// 等textview加載完畢後,讓textView成爲第一響應者,彈出鍵盤 -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.textView becomeFirstResponder]; }
效果展現:code
控制器加載完後,鍵盤才彈出:server
好的,接下來查詢新浪微博的開放接口文檔http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
找到發微博的接口:
發佈一條新微博 https://api.weibo.com/2/statuses/update.json
在發佈微博的控制器IWComposeViewController.m實現發微博功能:
#import "IWComposeViewController.h" #import "IWTextView.h" #import "AFNetworking.h" // 新增 #import "IWAccount.h" // 新增 #import "IWAccountTool.h" // 新增 #import "MBProgressHUD+MJ.h" // 新增 /** * 發微博 */ - (void)send { // AFNetworking\AFN // 1.建立請求管理對象 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; // 2.封裝請求參數 NSMutableDictionary *params = [NSMutableDictionary dictionary]; // 發送內容 params[@"status"] = self.textView.text; // 根據以前封裝的帳號工具類IWAccountTool,登錄受權的帳號信息被保存在本地,而後經過帳號屬性獲取access_token params[@"access_token"] = [IWAccountTool account].access_token; // 3.發送請求 [mgr POST:@"https://api.weibo.com/2/statuses/update.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { [MBProgressHUD showSuccess:@"恭喜,發送成功"]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 隱藏提醒框 [MBProgressHUD showError:@"抱歉,發送失敗"]; }]; // 4.關閉控制器。當用戶點擊發送微博按鈕後,須要將發微博界面關掉,由於發微博有時可能須要很長時間 [self dismissViewControllerAnimated:YES completion:nil]; }
OK,到目前爲止,發送文字的微博功能,完美實現 ^_^