Search code examples
iosobjective-cios7

multiple view controllers involved, how to send a value back to starting view controller?


I am very new to iOS and overwhelmed with resources I find online. My use case is simple

a.) ViewController parent has label called categoryLabel. A tap on label category opens a new view View 1
b.) View 1, shows all groups. Lets says A, B, C. This will be shown on table
c.) when user click on any group A, B or C, a new view View 2 appears with all categories in that group. For example, user clicks on A and on View 2 user sees categories A1, A2, A3. d.) Now when user clicks on any specific category, how does that goes back to ViewController parent and assigns to categoryLabel?

I do not know what is the best way to approach this design. Any guidance is very much appreciated


Solution

  • hope this will help

    let take an example , your are going from A -> B and want send some data from B to A , there are many technique to do that but using delegate method and block are nicer way.

    delegate way :-

    in your B.h file

    @protocol yourDelegate <NSObject>
    
    -(void)whichCategoryClicked:(NSString *)categoryName;
    
    @end
    
    @interface B : UIView
    @property(nonatomic, assign)id<yourDelegate> delegate;
    

    in your B.m

    just call this delegate method after Clicking particular category.

    [self.delegate whichCategoryClicked :@"Category_name"];
    

    in your A.h

    assign it as delegate and import the above class

    @interface A.h : UIViewController<yourDelegate>
    

    and in Implement this method in A.m

    first in your viewdidload

    {


     B *objB = [[B alloc]init];
        objB.delegate = self;
    

    }

    -(void)whichCategoryClicked:(NSString *)categoryName
    {
    categoryLabel.text = categoryName;
    }