I have an app which uses a UITabBarController
which contains 4 different navigation controllers. For example,
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.
精彩评论