Objective C:如何重构导航控制器的代码

发布于 2024-11-15 04:45:22 字数 613 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-11-22 04:45:22

在 UserProfileViewController 上创建一个静态选择器,如下所示:

@interface UserProfileViewController {
...
}

+ (void)pushNewUserProfileViewControllerWithPropertyA:(id)pa 
         propertyB:(id)pb     
         ontoNavigationController:(UINavigationController*)nav;

@end

并在其实现中放入您问题中的四行代码,但请确保自动释放创建的 UserProfileViewController。

Make a static selector on the UserProfileViewController, like this:

@interface UserProfileViewController {
...
}

+ (void)pushNewUserProfileViewControllerWithPropertyA:(id)pa 
         propertyB:(id)pb     
         ontoNavigationController:(UINavigationController*)nav;

@end

and in its implementation put those four lines of code from your question, but make sure you autorelease the created UserProfileViewController.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文