Objective C:如何重构导航控制器的代码
我有一个使用 UITabBarController
的应用程序,其中包含 4 个不同的导航控制器。例如,
1) “Feed”视图导航控制器
2)“最受欢迎”视图导航控制器
3)“新闻”视图导航控制器
4) “更多”视图导航控制器
对于每个导航控制器,可能有一些公共视图控制器需要推送到其现有堆栈上。例如,如果我单击“Feed”和“News”视图控制器中显示的用户个人资料图片,他们应该将 userProfile 视图控制器推送到其堆栈上。
目前,我看到自己在不同的导航控制器中重复这样的代码:
UserProfileViewController *user = [[UserProfileViewController alloc]init];
user.propertyA = XXX;
user.propertyB = YYY;
[self.navigationController pushViewController:user animated:YES];
我担心这会变得过于重复和混乱,特别是你有多个导航控制器。
我的问题是如何重构代码,以便所有导航控制器每次需要加载公共视图控制器时都不需要重复代码。
提前致谢
I have an app which uses a UITabBarController
which contains 4 different navigation controllers. For example,
1) "Feed" view Navigation Controller
2) "Most Popular" view Navigation Controller
3) "News" view Navigation Controller
4) "More" view Navigation Controller
For each of the navigation controller, there might be a few common viewcontrollers that will need to be pushed onto their existing stack. For example, if I click on the user profile pic displayed in both 'Feed' and 'News' viewcontrollers, they should push the userProfile viewcontroller onto their stacks.
Currently I see myself repeating codes like this across different navigation controllers:
UserProfileViewController *user = [[UserProfileViewController alloc]init];
user.propertyA = XXX;
user.propertyB = YYY;
[self.navigationController pushViewController:user animated:YES];
I am afraid this will become too repetitive and confusing especially you have multiple navigation controllers in place.
My question will be how to re-factor the code such that all nav controllers will not need to repeat the code everytime it needs to load a common view controller.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 UserProfileViewController 上创建一个静态选择器,如下所示:
并在其实现中放入您问题中的四行代码,但请确保自动释放创建的 UserProfileViewController。
Make a static selector on the UserProfileViewController, like this:
and in its implementation put those four lines of code from your question, but make sure you autorelease the created UserProfileViewController.