如何禁用在 iOS objective c 中以编程方式创建的 UIButton

How to disable UIButton that created programmatically in iOS objective c

我在一个函数中以编程方式创建了 unbutton,现在我想在 iOS Objective C 中的 ViewController 中的另一个函数中禁用它。

-(void)makeButton{
self.markerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *markerImage = [UIImage imageNamed:markerImageName];
    self.markerButton.frame = CGRectMake((self.mView.bounds.size.width/2)-(markerImage.size.width/2), (self.mView.bounds.size.height/2)-(markerImage.size.height), markerImage.size.width, markerImage.size.height);
    self.markerButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
    [self.markerButton setTitle:@"" forState:UIControlStateNormal];
    self.markerButton.btnData = self.markerButton;
    [self.markerButton setBackgroundImage:[UIImage imageNamed:markerImageName] forState:UIControlStateNormal];
    [self.markerButton addTarget:self action:@selector(markerButtonClicked:forEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.mView addSubview:self.markerButton]; 

-(void)anotherFunction{
     self.markerButton.enable = NO;
}

您只需将 self.markerButton.enable = NO; 更改为 self.markerButton.userInteractionEnabled=NO;

要在 UIButton 上禁用用户交互,您需要通过方法 userInteractionEnabled

停止用户交互

所以你的代码应该是

-(void)anotherFunction {
     self.markerButton.userInteractionEnabled = NO;
}