委托和协议无法正常工作

时间:2012-06-06 01:58:20

标签: iphone ios uiviewcontroller delegates protocols

我正在尝试将数据从一个TabBarViewController传递回我的MasterViewController,但是当将数据传递回MasterViewController时,我的协议/代理中的方法永远不会被访问。

这就是我所做的 - 我有一个TabBarView控制器和一个MasterViewController,TabBarController被添加到MasterViewController作为子视图...我想要做的是然后将另一个子视图加载到MasterViewController上,我计划在tabBarViewController中调用tabbarbutton时调用它我的协议/委托方法。为此,我使用以下代码。 (我希望到目前为止这是有道理的)

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
        }
//..

然后, 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.
    }

唯一不同于我通常在这里做的事情是这个TabBarViewController被添加为子视图的事实..所以我想知道如果那搞砸了......但是如果是这样我不知道如何修复..

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以通过添加以下内容来调试问题:

NSAssert (nil != [self delegate]);

在致电loadViewsAction之前。该断言将失败,因为您尚未分配TabBarController的委托。创建TabVarController后,执行:

tbVS.delegate = self;

然后您将拥有一个用于loadViewsAction的委托。

相关问题