按钮触摸代表不工作

时间:2014-05-20 22:48:43

标签: objective-c ios7 delegates

我在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

1 个答案:

答案 0 :(得分:1)

您尚未正确设置代理

 @implementation MainViewController

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

  .....
  .....

  @end
相关问题