Is there a way to hold a button in the UINavigationController stack?

综合技术 Hello, buddy! (源链)

I know how to add a button to each controller I push into the navigation controller stack. But I need to have a settings button on the upper right of the navigation bar, at all times, regardless of which controller gets pushed or popped. The button should present a settings viewController modally.

To me, having each controller’s viewDidLoad include code for the button plus including the settings controller header seems like a hack.

Seems like I should be able to insert this button from the navigation controller itself, pointing to a selector in the navigation controller. And this method would then present the settingsController modally through whichever controller happens to be active in the stack at that point.

Any ideas on how to achieve this?

Ok this is what I did:

1 – subclass UINavigationController and include the header for my settings controller

2 – override -pushViewController:aniamted:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
    UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]initWithTitle:@"Settings"
                                                                      style:UIBarButtonItemStylePlain
                                                                     target:self
                                                                     action:@selector(shouldPresentSettingsView)];
    [viewController.navigationItem setRightBarButtonItem: settingsButton];
    [settingsButton release];
}

3 – add two methods:

- (void)shouldPresentSettingsView
{
    SetupViewController *settingsView = [[SetupViewController alloc] init];
    UINavigationController *tempController = [[UINavigationController alloc]initWithRootViewController:settingsView];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                     style:UIBarButtonItemStylePlain
                                                                    target:self
                                                                    action:@selector(shouldDismissSettingsView)];
    settingsView.navigationItem.leftBarButtonItem = cancelButton;
    [self.visibleViewController presentModalViewController:tempController animated:YES];

    [cancelButton release];
    [settingsView release];
    [tempController release];
}

- (void)shouldDismissSettingsView
{
    [self dismissModalViewControllerAnimated:YES];
}

您可能感兴趣的

前端iOS打点统计的AOP技术实践 前言 近期前端移动组因项目需求,需要在用户行为上进行打点统计,但由于部分早期SDK在初始设计时并未考虑到日志记录这一功能,临时去变更代码所花费的成本也较高,所以架构组决定针对这一需求进行一次AOP开发实践,用面向切面统计来代替部分传统代码埋入打点。 AOP介绍 AOP的全称是Asp...
Development of applications for iPhone – mat... I am quite the beginner - but I have a lot of experience with respect to Electrical Engineering and formulas - over 30 years worth! Trying to construc...
iOS面试技巧及问题最全梳理,让BAT的Offer不再难拿!... 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了。今年,找过工作人可能会更深刻地体会到今年的就业形势不容乐观,随着各大公司秋招的开始,很多小伙伴都行动起来了,我也有幸获得了一份不错的offer并和大家分享自己的经验心得。由于我面试公司比较多,所以自然也是做了这方面的准备,因...
iOS 框架详解—「AFN 源码3:Serialization 序列化」... AFNetWorking 基本是 iOS 开发中使用网络通信框架的标配,这个框架本身比较庞大,也很复杂,但是使用起来非常非常简单。 本篇文章主要从【 Serialization 序列化 】学习总结,该模块学习将续更 ~ 在「时间 & 知识 」有限内,总结的文章难免有「未...
iOS WebviewJavascriptBridge 源码研读笔记 这两天接近元旦,事情稍微少些,有些时间,索性写点什么,就从最擅长的iOS混合开发写起了,由于iOS开发经验不到四年吧,期间还搞了一年半的前端,有些知识可能还是积累的不足,能力不足,水平有限,可能有谬误希望各位读者发现的话及时指正,感激不尽。 至于 WebviewJavascriptBridge...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Is there a way to hold a button in the UINavigationController stack?



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录