Search code examples
iphoneuiviewcontrollerpushviewcontroller

iPhone : pushing on UIViewController from where it pushed


I've two UIViewControllers say ViewController_A and ViewController_B

My flow is, AViewController_A ---> BViewController_B ---> CViewController_A

For A, B and C navigations I have the following situation,

A - In ViewController_A I've 100 of records which are in UITableViewCell, user select any one, and it will pushed to ViewController_B which will showing that selected record from ViewController_A.

B - Will show selected data from ViewController_A, and having Back button to go back (I can pop). Another thing, have a UIButton, If user tap this, it will again showing ViewController_A but with only that single record

C - Either we'll pop from ViewController_B then there's no issue, but I again want to push, so I need to #import ViewController_A in ViewController_B (as I've already imported ViewController_B in ViewController_A so I can't reimport ViewController_A in ViewController_B, right?) will create collision for those UIViewControllers.

What should be the better way to solve the problem in C, one suggestion is to make another `ViewController_D like and show the same as in ViewController_A but I think its not proper way, as I've already UI and coded for the functionality.

Suggestion needed. Let me know if you've any doubt!


Solution

  • What I understand is you have trouble with #import directive ? I guess you are importing header in the .h file ? If so, do an #import in the .m file; and in the .h you should use @class YouViewControllerA.

    Ex with AViewController:

    .h

    @class BViewController
    
    @interface AViewController : UIViewController 
    {
      //Your attributes here
    }
    
    @end
    

    .m

    #import "BViewController.h"
    
    @implementation AViewController
    
    //Some AViewController methods here
    
    @end
    

    Do this for both AViewController and BViewController and it should work.

    EDIT : #import directives are designed to avoid recursive error, so anyway you shouldn't get error. If you know a bit of preprocessing, the #import do the following (automatically) :

    #ifndef TOTO_HEADER
    #define TOTO_HEADER
    
    //your methods here
    
    #endif
    

    More explanation :

    When you write #import "toto.h" , at compile time the compiler will do the following check :

    • Is the preprocessoring variable "TOTO_HEADER" defined ? (this corresponding to the line ifndef "TOTO_HEADER")
    • If it isn't we define it (this is the #define "TOTO_HEADER" line) AND we "include" the methods and functions.
    • If it has been already defined, no need to include the methods (since they already get included).

    This way we prevent header file from being included if they were already included. (and by extension it should not make any recursive error).

    In other words : #import ensures that a header file is only included once so that you never have a problem with recursive includes.