Objective C UIBarButtonItem作为切换按钮,用于编辑和保存

时间:2015-07-14 07:02:43

标签: objective-c uinavigationbar uibarbuttonitem uinavigationitem ios8.3

我已经编写了一个代码来放置一个带有customview的右边栏按钮项目。选择编辑它会更改为保存选项。现在我选择保存它会保存值。如果我再次尝试选择编辑动作而不是触发。代码我写在下面,

item = [[UINavigationItem alloc]init];    
btnn = [UIButton buttonWithType:UIButtonTypeCustom];
[btnn setFrame:CGRectMake(0, 0, 40, 40)];
btnn.layer.masksToBounds = NO;
btnn.layer.cornerRadius = 8.0;
[btnn addTarget:self action:@selector(rightBarButtonCustomPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnn setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnn];   
item.rightBarButtonItem = rightBtn;
self.navigationBar.items = @[item];

- (IBAction为)rightBarButtonCustomPressed:(的UIBarButtonItem *)BTN {

item = [[UINavigationItem alloc]init];
btnn = [UIButton buttonWithType:UIButtonTypeSystem];
[btnn setFrame:CGRectMake(0, 0, 40, 40)];
btnn.layer.masksToBounds = NO;
btnn.layer.cornerRadius = 8.0;
[btnn addTarget:self action:@selector(saveBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnn setTitle:@"save" forState:UIControlStateNormal];
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnn];
item.rightBarButtonItem = rightBtn;       
self.navigationBar.items = @[item];

[self.firstNameText setEnabled:YES];
[self.lastNameText setEnabled:YES];
[self.dateOfBirthTxt setEnabled:YES];
[self.contactNumberlbl setEnabled:YES];

}

- (IBAction)saveBtnClicked:(UIBarButtonItem *)btn {

[self.firstNameText setEnabled:NO];
[self.lastNameText setEnabled:NO];
[self.dateOfBirthTxt setEnabled:NO];
[self.contactNumberlbl setEnabled:NO];
item = [[UINavigationItem alloc]init];
btnn = [UIButton buttonWithType:UIButtonTypeCustom];
[btnn setFrame:CGRectMake(0, 0, 40, 40)];
btnn.layer.masksToBounds = NO;
btnn.layer.cornerRadius = 8.0;
[btnn addTarget:self action:@selector(rightBarButtonCustomPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnn setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnn];
item.rightBarButtonItem = rightBtn;   
self.navigationBar.items = @[item];

我需要实现barbuttonitem切换以保存和编辑选项。请任何人帮我修复此问题。谢谢

1 个答案:

答案 0 :(得分:0)

代替alloc init evry time,为什么不尝试更改buttonTitle然后你可以根据它执行操作