当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - How do I remove or stop a button in table view from working after registered amount of taps

问题描述:

I have a button in a table view that adds rows. After a maximum of five rows I want to stop the user adding anymore. Currently I show an alert after button recives 5 taps.

How can I stop the user from using the button past this point? Setting to hidden wont work as its a custom subclass and property hidden is not found on the class

- (void)viewDidLoad

{

[super viewDidLoad];

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];

titleLabel.backgroundColor = [UIColor clearColor];

titleLabel.textColor = [UIColor whiteColor];

titleLabel.shadowColor = [UIColor darkGrayColor];

titleLabel.text = self.distributionBoard.dbRef;

titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];

[titleLabel sizeToFit];

self.navigationItem.titleView = titleLabel;

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];

// Add new appliance button to the table view's footer view

UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 0, 300.0f, 100.0f)];

footerView.backgroundColor = [UIColor clearColor];

UIButton *newBoardButton = [UIButton buttonWithType:UIButtonTypeContactAdd];

CGRect buttonFrame = newBoardButton.frame;

buttonFrame.origin.x = footerView.frame.size.width - buttonFrame.size.width;

newBoardButton.frame = buttonFrame;

[newBoardButton addTarget:self action:@selector(addCircuitButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

[footerView addSubview:newBoardButton];

self.tableView.tableFooterView = footerView;

}

.....

 ////limit to five appliances

- (void)addCircuitButtonPressed:(id)sender {

LogCmd();

Circuit *circuit = [[ICCircuitManager manager] newCircuit];

circuit.distributionBoard = self.distributionBoard;

circuit.circuitReference = [NSString stringWithFormat:@"%d", [self.circuits count] + 1];

circuit.createdAt = [NSDate date];

circuit.modifiedAt = [NSDate date];

[self.distributionBoard addCircuitsObject:circuit];

[self loadData];

[self.tableView reloadData];

{

m_buttonTouchCount++;

if ( m_buttonTouchCount == 4)

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iCertifi"

message:@"Maximum number of appliances reached"

delegate:nil

cancelButtonTitle:@"Ok"

otherButtonTitles:nil];

[alert show];

// m_buttonTouchCount = 0; // reset to 0 here if required.

}

}

}

网友答案:

Where you have AlertView you can type this code to disable button:

[(UIButton *)sender setEnabled:NO];

or to hide button:

 [(UIButton *)sender setHidden:YES];
分享给朋友:
您可能感兴趣的文章:
随机阅读: