Objective C UIBarButtonItem as toggle button for edit and save - objective-c

I have written a code to place a right bar button item with customview.While selecting edit it changes to save option.Now I selecting save it saves the values.If again I try to select edit the action not firing.The code I written below,
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];
I need to achieve barbuttonitem as toggle to save and edit option.Please anybody help me to fix this.Thanks

In place of alloc init evry time , why don't you try changing buttonTitle and then you can perform action on basis of it

Related

Programatically created UIDatepicke done and cancle button is not working

This is my UIDatePicker code but in that picker, done and cancle button is not working ...please help me.... i also add here my done and cancle button methods here..Please give me a proper suggessions for how it works properly.
if (textField.tag==Jobdatetxt.tag)
{
[Jobdatetxt resignFirstResponder];
[data.toolbar removeFromSuperview];
UITextField *lagFreeField = [[UITextField alloc] init];
[self.view.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
//////////////////////////////////////////////// DATE OF JOB////////////////////////////////////////////////
pickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,result.height - 216, result.width, 216)];
[pickerDate setDatePickerMode:UIDatePickerModeDate];
pickerDate.backgroundColor = [UIColor whiteColor];
[self.view.window addSubview:pickerDate];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,result.height - 266,result.width,50);
cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancel setTitle:#"Cancel" forState:UIControlStateNormal];
cancel.backgroundColor = [UIColor clearColor];
[cancel.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
cancel.titleLabel.textColor = [UIColor whiteColor];
[cancel addTarget:self action:#selector(CancelClicked:) forControlEvents:UIControlEventTouchUpInside];
cancel.tag = textField.tag;
cancel.frame = CGRectMake(10, 10, 70, 30);
[toolbar addSubview:cancel];
doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.backgroundColor = [UIColor clearColor];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
doneButton.titleLabel.textColor = [UIColor whiteColor];
[doneButton addTarget:self action:#selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
doneButton.tag = textField.tag;
doneButton.frame = CGRectMake(result.width - 80, 10, 70, 30);
[toolbar addSubview:doneButton];
[self.view.window addSubview:toolbar];
}
// Done Button method
-(IBAction)DoneClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}
//Cancle Button Method
-(IBAction)CancelClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}

How do I change the leftbarbuttonitem color in ios7.0

self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
I have done above,but it doesn't work.The color is still as same as the background color!
Create button and substitute
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.leftBarButton];
self.navigationItem.leftBarButtonItem = leftBarButton;
or
+ (UIBarButtonItem*)itemWithNormalImage:(UIImage*)normalImage
pressedImage:(UIImage*)pressedImage
target:(id)target
action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:normalImage
forState:UIControlStateNormal];
[button setImage:pressedImage
forState:UIControlStateSelected];
[button setImage:pressedImage
forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item=[[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
Text color:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"done" style:UIBarButtonItemStyleBordered target:self action:nil];
[item setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItems = #[item];

Button action doesn't respond when switched viewcontroller

I have an app where the viewcontroller is switched if a button is pressed. I switch like this:
NewViewController *nv = [[NewViewController alloc] init];
[nv NewButtonTapped:self];
[self.view addSubview:nv.view];
Then in my NewViewController.m:
- (void)NewButtonTapped:(id)sender
{
backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
backgroundImage.image = [UIImage imageNamed:#"cubePage.png"];
[self.view addSubview:backgroundImage];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(964, 100, 60, 150);
button.enabled = YES;
button.userInteractionEnabled = YES;
[button setTitle:#"D" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped:(id)sender
{
NSLog(#"Tapped");
}
All images and buttons are loaded and appears fine in the new viewcontroller but the button action buttonTapped: doesn't respond when I'm tapping the button. This code works fine in the first viewcontroller.What's the problem?

iPhone UIButton action is not working

I create UIView and then I add UIButton as subview with action, but this action is never called.
Does anybody know why?
Here is my code:
- (void)viewDidLoad{
UIView *roundResultView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 150)];
UIImageView *_popUpBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NotificationBg.png"]];
_popUpBg.frame = CGRectMake(0, 0, roundViewWidth, roundViewHeight);
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 100, 30)];
[closeButton addTarget:self action:#selector(closeAndBack) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Back") forState:UIControlStateNormal];
[roundResultView addSubview:_popUpBg];
[roundResultView addSubview:closeButton];
[self.view addSubview:roundResultView];
}
-(void)closeAndBack{
NSLog(#"closeAndBack"); //never called
}
I have checked this:
NSLog(#"%#", [closeButton allControlEvents]);
NSLog(#"%#", [closeButton allTargets]);
and it prints:
... Sedmica[2192:14f03] 64
... Sedmica[2192:14f03] {(
<ViewController: 0x8631e10>
)}
Update your code with this and kindly let me know :
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[closeButton addTarget:self
action:#selector(closeAndBack:)
forControlEvents:UIControlEventTouchDown];
[closeButton setTitle:#"Back"
forState:UIControlStateNormal];
closeButton.frame = CGRectMake(30.0, 30.0, 100.0, 30.0);
[self.view addSubview:closeButton];
- (IBAction)closeAndBack:(UIButton *)button{
NSLog(#"Button clicked.");
}

How to add an image to UIBarButtonItem?

I created a custom button and applied it to the UIbarbuttonitem.
There is no error but nothing is shown on the navigation bar:(
This is my code-
//create a custom button
UIImage *image = [UIImage imageNamed:#"TESTButton.png"];
UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myCustomButton setImage:image forState:UIControlStateNormal];
[myCustomButton addTarget:nil action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[button release];
[myCustomButton release];
[image release];
[navID release];
Anybody who can fix my code? :)
From the docs:
initWithImage:style:target:action:
Try that.
To set ANY image with right coloring, use UIButton, set "image for state" and then create UIBarButtonItem with customView
UIImage *btnImage = [UIImage imageNamed:#"button"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 0, 0, btnImage.size.width, btnImage.size.height );
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchDown];
[btn setImage:btnImage forState:UIControlStateNormal];
_buttonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
One of the Simplest Way is :
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backk.png"] style:UIBarButtonItemStylePlain target:self action:#selector(backBtn:)];
self.navigationItem.leftBarButtonItem = button2;
Action Method:
- (IBAction)backBtn:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}