2012-06-06 125 views
0

我想从一个TabBarViewController传回数据到我的MasterViewController,但是当tyring传递数据回到MasterViewController时,我的协议/委托方法永远不会被访问。委托和协议工作不正常

这是我做了什么 - 我有一个TabBarViewcontroller和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被添加为子视图..所以我w if如果这是搞砸事情..但如果是这样我不知道如何解决...

任何帮助将不胜感激。

+0

你有没有分配loadActionView.delegate = self;在MasterViewController中? –

+0

哦,不,我没有..但只是试图做到这一点在MasterViewController的viewDidLoad方法,它给了我这个错误**使用未声明的标识符'loadActionView'** – HurkNburkS

+0

@ xingzhi.sg不知道如果你看到我的最后一个味精但是尝试执行你所说的代码似乎并不奏效。 – HurkNburkS

回答

1

您可以通过添加调试问题:

NSAssert (nil != [self delegate]); 

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

tbVS.delegate = self; 

你再有一个委托使用loadViewsAction。

+0

好吧很酷!非常感谢!这工作完美! – HurkNburkS