2014-05-20 55 views
0

我有问题让我的委托方法在MainViewController.m执行。当MenuView.xib中的Logout按钮被按下时,它会进入MenuView.m中的IBAction logoutButton(这是有效的)。这将调用LogoutDelegate中的logoutButton委托函数。 MainViewController.h接受LogoutDelegate,MainViewController.m定义了logoutButton方法(非常底层代码)。按钮触摸代表不工作

我错过了什么?

MenuView.h

#import <UIKit/UIKit.h> 

@protocol LogoutDelegate 
    -(void)logoutButton; 
@end 

@interface MenuView : UIView 
    @property (nonatomic, retain) id <LogoutDelegate> logoutDelegate; 
    - (IBAction)logoutButton:(UIButton *)sender; 
@end 

MenuView.m

#import "MenuView.h" 
#import "AlertStatus.h" 

@implementation MenuView 

    - (id)initWithFrame:(CGRect)frame 
    { 
     self = [super initWithFrame:frame]; 
     if (self) { 
      // Initialization code 
     } 
     return self; 
    } 

-(IBAction) logoutButton:(UIButton *)sender { 
    NSLog(@"In the logoutButton IBAction method"); 
    [_logoutDelegate logoutButton]; 
} 

@end 

MainViewController.h(不是整个文件)

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


@interface MainViewController : UIViewController 
    <UITableViewDelegate, UITableViewDataSource, LogoutDelegate> 
@property (strong, nonatomic) IBOutlet MenuView *menuView; 

- (IBAction)menuButtonClicked:(UIBarButtonItem *)sender; 

@end 

MainViewController.m(不是整个文件)

#import "MainViewController.h" 
#import "AlertStatus.h" 

@interface MainViewController() 
@end 

@implementation MainViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    //Gather profiles and populate table after view loads 
    [self gatherProfiles];  
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)menuButtonClicked:(UIBarButtonItem *)sender { 
    //This is for the menu which is encapsulated in a transparent view 
    if (self.menuView){ 
     [self.transView removeFromSuperview]; 
     self.menuView = Nil; 
     self.transView = Nil; 
    } 
    else { 

     //Create a transparent view that covers the whole window. This is made to 
     //destroy the view when the users touches the outside of the menuView 
     //that will be encapsulated in this. 
     self.transView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     self.transView.backgroundColor = [UIColor clearColor]; 

     //Create menu view and encapsulate it in the transparent view 
     self.menuView = [[[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil] objectAtIndex:0]; 
     self.menuView.layer.cornerRadius = 20.0f; 
     self.menuView.layer.borderWidth = 3.0f; 
     self.menuView.layer.borderColor = [UIColor whiteColor].CGColor; 
     self.menuView.frame = CGRectMake(0, 64, self.menuView.frame.size.width, 
            self.menuView.frame.size.height); 
     [self.transView addSubview:self.menuView]; 

     //Create a UITapRecognizer and make sure that button touches are counted. Non button touches will 
     //destroy the view as it was touched outside the menuView 
     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]  
      initWithTarget:self action:@selector(singleTapGestureCaptured:)]; 
     //singleTap.cancelsTouchesInView = NO; 
     [self.transView addGestureRecognizer:singleTap]; 

     //Add the transparent view that encapsulated the menuView in the main view. 
     [self.view addSubview: self.transView]; 
    } 
} 

//Removed Sub Views from MainView when tapped 
-(void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture{ 
    //This is for the menu which is encapsulated in a transparent view 
    if(self.menuView) { 
     [self.transView removeFromSuperview]; 
     self.menuView = Nil; 
     self.transView = Nil; 
    } 
} 

//THIS IS A DELEGATED METHOD FROM THE MenuView 
//When the logout button in MenuView is pressed this method will be called 
- (void)logoutButton{ 
    NSLog(@"In the logoutButton method in MainViewController"); 
} 


@end 
+0

你还会在实际设置'logoutDelegate'财产? – Isaac

+0

老实说,我不知道。这是我与代表的第一次经历。我以为我设置了我需要设置的所有东西,但显然有些东西是不对的:) – lr100

回答

1

您没有设置委托正确

@implementation MainViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [self.menuView setLogoutDelegate:self]; 
    } 

    ..... 
    ..... 

    @end 
+0

我把这条线放在里面,没有改变。你有没有看到其他的错误?这是我与代表的第一次经历。 :) – lr100

+0

所以你得到日志'NSLog(@“在logoutButton IBAction方法”);'“???也尝试添加[self.menuView setLogoutDelegate:self];行'self.menuView = [[[NSBundle mainBundle] loadNibNamed:@“MenuView”owner:self options:nil] objectAtIndex:0];' –

+0

我把它从viewDidLoad中放到了这里,它工作了!!!!非常感谢 另一个问题,如果你想要回答.....如果我在菜单视图(modifyProfile)中有另一个按钮,是否需要创建一个全新的委托,或者我可以使用这个委托吗? – lr100