Ezimet
Ezimet

Reputation: 5078

How to set delegate of viewControllers in TabbarController?

EDit:

I have tabbarcontroller with two tabs, first tab I have a viewController , second tab I have navigationViewController with two stack of ViewControllers with tableView.

Tab1--->VC1

Tab2--->NVC-->fistVC1----push to----->secondVC2.

my code to push:

fistVC1.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
       if (indexPath.section == 0 & (indexPath.row == 0)) {

    _secondVC2 = [[secondVC2 alloc] init];
    [self.navigationController pushViewController:_secondVC2 animated:YES];
    [_secondVC2 release];

what I need to do is set VC1 as a delegate of secondVC2, when secondVC2 cell selected i Want to send message to the VC1.

how can i do this ,please give me some advise.

i tried like blow:

secondVC2.h

 @protocol secondVC2Delegate <NSObject>

   - (void)someMethod;

 @end
 #import <UIKit/UIKit.h>

  @interface secondVC2 :UIViewController<UITableViewDelegate,UITableViewDataSource>
 {
     id<secondVC2Delegate>delegate;
 }

@property (nonatomic ,assign) id<secondVC2Delegate>delegate;

 @end;

secondVC2.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
     NSLog(@"delegate%@",self.delegate);
     if (indexPath.row == 0) {
      [self.delegate someMethod];
     }

VC1.h

    #import <UIKit/UIKit.h>
    #import"secondVC2"
    @interface VC1 :UIViewController<secondVC2Delegate>{

     secondVC2 *tvc2;

    }
    @property (nonatomic ,retain) secondVC2 *tvc2;

    - (void)someMethod;

    @end;

VC1.m

   - (void)viewDidLoad
    {
       tvc2 = [secondVC2 alloc] init];
       tvc2.delegate = self;
    }

but the delegate kept released and i got nil delegate in the secondVC2 which i dont know why . So how could i achive this ?

Upvotes: 3

Views: 851

Answers (1)

runmad
runmad

Reputation: 14886

ViewController1 *viewController1 = [[ViewController1 alloc] init];
ViewController2 *viewController2 = [[ViewController2 alloc] init];
viewController1.delegate = viewController2;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
[self.tabBarController setViewControllers:@[viewController1, navigationController]];

You could also look into NSNotificationCenter.

Upvotes: 4

Related Questions