0

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • You can use the delegate method pattern from iOS. check the link http://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-delegate-to-communicate-between-two-view-controllers – user2071152 Nov 20 '14 at 04:56
  • If you're using a storyboard with segues, then you can use an unwind segue to go back to the first controller, and pass data to it. – rdelmar Nov 20 '14 at 05:16

2 Answers2

1

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;
}
Anurag Bhakuni
  • 2,379
  • 26
  • 32
0

You can use Local notification for this purpose names as NSNotificationCenter in iOS. Which works as follows:

To send a notification that is from the view on which you are and want to send some value from that view, use below code:

NSDictionary *dict;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationKey" object:nil userInfo:dict];

and now on any of the view controller, you can add observer on viewDidLoad of that class as:

[[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(methodToCall:)
                                                 name:@"NotificationKey"
                                               object:nil];

Now call method written in above line:

- (void)updateImageFromArray:(NSNotification *)notification {
    // your dict
    NSDictionary *dictUserInfo = [notification userInfo];

}
Ashutosh
  • 2,215
  • 14
  • 27