开发者

Objective C: How to re-factor code for Navigation Controllers

开发者 https://www.devze.com 2023-03-12 13:12 出处:网络
I have an app which uses a UITabBarController which contains 4 different navigation controllers. For example,

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 sta开发者_StackOverflowcks.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消