简体   繁体   中英

delegate and protocol not working correctly

I am trying to pass data back from one TabBarViewController to my MasterViewController however when tyring to pass data back to the MasterViewController the method from my protocol/Delegate never gets accessed.

This is what I have done - I have a TabBarViewcontroller and a MasterViewController, the TabBarController is added to the MasterViewController as a subview... what I am trying to do is then load another subview onto the MasterViewController which I plan to do when a tabbarbutton is selected in the tabBarViewController i call my protocol/delegate method. for this I am using the following code. (I hope this makes sense so far)

TabBarViewController.h

@class MasterViewController;

@protocol LoadActionView <NSObject>
@required
- (void) loadViewsAction;
@end

@interface TabBarViewController : UIViewController <UITabBarDelegate> {


    __weak id <LoadActionView> delegate;

//..
}

@property (weak, nonatomic) id delegate;

//..

TabBarViewController.m

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    switch (item.tag) {
        case 0:
        {
             NSLog(@"item 1 selected");
            [[self delegate] loadViewsAction]; //the thread defiantly makes it here as I have debugged to this point
        }
//..

then, MasterViewController.h

 #import <UIKit/UIKit.h>
    #import "TabBarViewController.h"

    @interface MasterViewController : UIViewController <UINavigationControllerDelegate, LoadActionView> {


TabBarViewController *tbVC;

}

@property (nonatomic, strong) TabBarViewController *tbVC;
    //..

MasterViewController.m

#import "TabBarViewController.h"

@synthesize tbVC;
    //..

- (void) viewDidLoad {
//..

tbVC = [[TabBarViewController alloc] initWithNibName:@"TabBarViewController" bundle:[NSBundle mainBundle]];
    UIView *tbView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 367.0, 320.0, 49.0)];
    [tbView addSubview:tbVC.view];
    [otherNav.view addSubview:tbView];


}

    - (void) loadViewsAction
    {
        NSLog(@"HITME!"); //threads not making it here.
    }

the only thing different form what I normally do here is the fact that this TabBarViewController is being added as a subview.. so I am woundering if thats screwing things up.. but if so I have no idea how to fix...

any help would be greatly appreciated.

You can debug the problem by adding:

NSAssert (nil != [self delegate]);

just before the call to loadViewsAction . That assert will fail because you have not assigned the TabBarController's delegate. After creating the TabVarController, perform:

tbVS.delegate = self;

You'll then have a delegate to use for loadViewsAction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM