新聞中心
當(dāng)一個(gè)項(xiàng)目發(fā)現(xiàn)每個(gè)返回的按鈕都是一樣的,并且標(biāo)題的字體也不是系統(tǒng)的字體,如果每個(gè)頁面都去設(shè)置返回按鈕,重新設(shè)置標(biāo)題字體,這樣代碼看著繁雜,而且會(huì)浪費(fèi)很多時(shí)間,這時(shí)候就有必要封裝一下了。。。
創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè),為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開發(fā),成都品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。
首先返回按鈕,需要在當(dāng)前頁面pop 到上一個(gè)頁面的話,有兩種方式:一 寫一個(gè)點(diǎn)擊代理,在用到的頁面實(shí)現(xiàn)它,二 就是獲取button所在的當(dāng)前控制器,然后pop出去。 但是第一個(gè)方法,還需要到用到的頁面去實(shí)現(xiàn)代理,也比較麻煩,那就來說第二種
首先獲取當(dāng)前控制器的方法:
UINavigationController *vc = [[UINavigationController alloc] init]; for (UIView* next = [sender superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { vc = (UINavigationController*)nextResponder; [vc.topViewController.navigationController popViewControllerAnimated:YES]; return; } }
因?yàn)槲疫@里的按鈕在navigationController上所以,這里的控制器變量都是 UINavigationController,如果需要獲取的是一般的UIViewController,那就把上面所有的UINavigationController 改成 UIViewController
獲取完之后,我們就使用這個(gè)來封裝自己的簡(jiǎn)單的導(dǎo)航欄,示例代碼:
+ (void)setNavigationBarWithTitle:(NSString *)title controller:(UIViewController *)controller{ controller.title = title; [controller.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName:kMainTextColor,NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Light" size:18]}]; //返回按鈕 UIButton *btn = [[UIButton alloc] init]; [btn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)]; [btn setTitleColor:kMainTextColor forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:13]; [btn addTarget:self action:@selector(back:) forControlEvents:(UIControlEventTouchUpInside)]; controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; } + (void)back:(UIButton *)sender{ UINavigationController *vc = [[UINavigationController alloc] init]; for (UIView* next = [sender superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { vc = (UINavigationController*)nextResponder; [vc.topViewController.navigationController popViewControllerAnimated:YES]; return; } } }
以上這篇iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前標(biāo)題:iOS封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例
當(dāng)前路徑:http://fisionsoft.com.cn/article/psedos.html