做微博項目時候遇到的
首先上圖,看看需求是什么樣的

微博首頁
點擊下方的橘黃色?按鈕,即modal出一個控制器,即發(fā)微博的界面控制器

發(fā)微博界面
要求navigationbar上面的左右兩個barbuttonitem字體顏色是橘黃色,但是在編輯界面沒有文字的時候,右上角的發(fā)送按鈕不可用,且顏色為灰色
下面是我的做法
由于發(fā)微博界面控制器被一個導航控制器包裝,所以選擇在自定義的導航控制器里統(tǒng)一設置UIBarButtonItem的顏色,下面是設置顏色的代碼部分
YLNavigationController.h
#import "YLNavigationController.h"
@implementation YLNavigationController
+(void)initialize
{
//設置整個項目的item狀態(tài)
UIBarButtonItem *item = [UIBarButtonItem appearance];
//設置item普通狀態(tài)
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
//設置item不可用狀態(tài)
NSMutableDictionary *disabledAttrs = [NSMutableDictionary dictionary];
disabledAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
disabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
[item setTitleTextAttributes:disabledAttrs forState:UIControlStateDisabled];
}
在首頁部分,底部的tabbar也是自定義,點擊按鈕之后,由tabbar的 代理YLTabBarController來modal發(fā)微博控制器,下面是YLTabBarController中關于modal的一部分代碼
YLTabBarController.m
#pragma mark - YLTabBarDelegate method
-(void)tabBarDidClickButton:(YLTabBar *)tabBar
{
YLComposeViewController *composeViewController = [[YLComposeViewController alloc] init];
YLNavigationController *navi = [[YLNavigationController alloc] initWithRootViewController:composeViewController];
[self presentViewController:navi animated:YES completion:nil];
}
下面則是在發(fā)微博控制器YLComposeViewController中設置左右兩個barbuttonitem,關于這部分代碼展示如下
YLComposeViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"發(fā)送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
那么這個時候問題就來了,顯示的時候右上角的barbuttonitem的顏色一直為橘黃色,按鈕也確實點擊之后沒有反應,一般出現(xiàn)這個問題,就是YLComposeViewController的view提前加載,但是我們能看到,設置顏色是在+(void)initialize中進行的,這個方法是在調用對象第一個方法之前就會調用的,而我所有有關YLComposeViewController的代碼全部展示出來了,當YLNavigationController的+(void)initialize方法調用時,YLComposeViewController壓根毛都沒有,并且我也分別在+(void)initialize方法和YLComposeViewController的viewDidLoad方法下斷點,結果表明+(void)initialize方法確實先加載。沒辦法我只能嘗試將self.navigationItem.rightBarButtonItem.enabled = NO;這段方法往后放,放在了viewWillAppear方法中,代碼如下
YLComposeViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
結果居然可行?。?!右邊發(fā)送按鈕不可點,且顏色為灰色,這個時候我就很困惑了,我甚至也有些懷疑是不是代碼結構太復雜中間有什么方法我漏掉了,于是我決定把這兩個控制器抽出來,單獨寫一個小demo,測試一下,代碼不多,我就羅列在下面,由于.h文件中很干凈,故只羅列.m文件。
YLNavigationController.m
+(void)initialize
{
//設置整個項目的item狀態(tài)
UIBarButtonItem *item = [UIBarButtonItem appearance];
//設置item普通狀態(tài)
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
//設置item不可用狀態(tài)
NSMutableDictionary *disabledAttrs = [NSMutableDictionary dictionary];
disabledAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
disabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
[item setTitleTextAttributes:disabledAttrs forState:UIControlStateDisabled];
}
YLComposeViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"發(fā)送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
ViewController.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
YLComposeViewController *com = [[YLComposeViewController alloc] init];
YLNavigationController *nav = [[YLNavigationController alloc] initWithRootViewController:com];
[self presentViewController:nav animated:YES completion:nil];
}
AppDelegate中并沒添加代碼,就不羅列了,基本就是將上面展示的代碼復制一遍,這也算是干了這么多年的機械養(yǎng)成的習慣,控制變量來排查問題,運行之后發(fā)現(xiàn)沒有效果,第一次寫博客不知道怎么錄屏,只能截圖了。

demo截屏
當然,放到viewWillAppear里面是好使得。
這下問題嚴重了,可能就牽扯到蘋果內部封裝的一些代碼問題了,這個我也確實不清楚,那以后遇到這種情況就直接放到viewWillAppear里面唄,我開始也是這么想的,不過事實證明還是too young啊,上面是將控制器modal出來,而我決定試一下另一種情況,就是將控制器push出來,看看效果,稍微改改代碼就好。
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *vc = [[ViewController alloc] init];
YLNavigationController *navi = [[YLNavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = navi;
return YES;
}
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"發(fā)送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
運行的結果是這樣的

demo截屏