IOS開發(fā)中UIBarButtonItem上按鈕切換或隱藏實(shí)現(xiàn)案例
IOS開發(fā)中UIBarButtonItem上按鈕切換或隱藏案例實(shí)現(xiàn)案例是本文要介紹的內(nèi)容,這個(gè)代碼例子的背景是:導(dǎo)航條右側(cè)有個(gè) edit button,左側(cè)是 back button 和 add button。代碼實(shí)現(xiàn)的按鈕切換/隱藏功能具體就是:點(diǎn)擊 edti button 的話,back button 隱藏,同時(shí)顯示 add button。
用戶編輯完以后則顯示 back button 隱藏 add button。這一功能在很多應(yīng)用里都會(huì)用到,而且適當(dāng)隱藏掉無用按鈕對(duì)保持界面簡(jiǎn)潔以及引導(dǎo)用戶操作都是有意義的。
代碼
- - (void)viewDidLoad {
- [super viewDidLoad];
- selfself.navigationItem.rightBarButtonItem = self.editButtonItem;
- }
- - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
- [super setEditing:editing animated:animated];
- // Don't show the Back button while editing.
- [self.navigationItem setHidesBackButton:editing animated:YES];
- if (editing) {
- self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
- target:self action:@selector(insertMe)] autorelease];
- }else {
- self.navigationItem.leftBarButtonItem = nil;
- //self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
- target:self action:@selector(backButton) ] autorelease];
- }
- }
其中 back button 是系統(tǒng)默認(rèn)的,去掉 else 里面的注釋,就可以加入其他按鈕。
小結(jié):IOS開發(fā)中UIBarButtonItem上按鈕切換或隱藏案例實(shí)現(xiàn)案例的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對(duì)你有所幫助!