簡體   English   中英

不調用UIButton目標動作

[英]UIButton target action not being called

如果這很簡單,請原諒我,我仍然在磨練我的編程技巧。 我正在嘗試將此按鈕設置為目標。 應該很容易,但是我不知道為什么它不起作用! 我插入了NSLog進行測試,但未調用該方法! 謝謝你的幫助。

//ShareView.h

@property (strong,nonatomic) UIButton *cancelBtn;


//ShareView.m
- (id)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code

            UIImage *shareImage = [UIImage imageNamed:@"shareBox.png"];
            [self setFrame:CGRectMake(10, 170, shareImage.size.width, shareImage.size.height)];

            self.shareIV = [[UIImageView alloc] initWithImage:shareImage];
            self.shareIV.userInteractionEnabled = YES;

            [self addSubview:self.shareIV];  

            [self shareBtnsInit];
            [self.shareIV addSubview:self.cancelBtn];

            self.userInteractionEnabled = YES;
        }
        return self;
}


-(void)shareBtnsInit{

        UIImage *cancelImg = [UIImage imageNamed:@"cancel27.png"];
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cancelBtn setImage:cancelImg forState:UIControlStateNormal];
        [self.cancelBtn setFrame:CGRectMake(277, 3, cancelImg.size.width, cancelImg.size.height)];

}

//MainViewController.m

-(IBAction)settingsButtonPressed:(id)sender{
    self.shareVC = [[ShareView alloc] initWithFrame:CGRectMake(0, 0, 0,0)];
    [self.view addSubview: self.shareVC];

    [self.shareVC.cancelBtn addTarget:self action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];

}

-(IBAction)settingsCancel:(id)sender{
    NSLog(@"TEST!!!");
    [self.shareVC removeFromSuperview];
}

基於注釋,沒有調用settingsButtonPressed:方法,因此從未設置要查找操作的按鈕。

嘗試添加[self.cancelBtn addTarget:self action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside]; 到您的shareBtnsInit方法。

以下是有關以編程方式制作UIButton的更多信息: 如何以編程方式創建基本的UIButton?

[self.cancelBtn addTarget:MainViewController action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];

在ShareView中,您必須導入要觸發事件的類。 在ShareView中,您可以導入MainViewController類並可以使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM