Handle popViewControllerAnimated like event in another class? - objective-c

I have to create a same button in every ViewController. So i decided to write a class having this functionality once. I have created a class name MyUtils and added a back button to UINavigationItem and it displays correctly but don't know how to add an event to it in that class.
It displays accurately but no event is perform. I mean popViewControllerAnimated:YES
I call myUtil like
MyUtil *utils = [[MyUtil alloc] init];
[utils NavAndBackBtn:self.navigationItem];
MyUtil.m
-(void)NavAndBackBtn:(UINavigationItem *)nav
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"icon_back.png"] forState:UIControlStateNormal];
//[btn setTarget:self action:#selector(popView) forControlEventTouchUPInside];
[button setFrame:CGRectMake(0,0,36,20);
UIBarButtonItem *barbtn = [[UIBarButton alloc] initWithCustomView:btn];
nav.leftBarButtonItem = barbtn;// this gives error
}
-(void)popView
{
// [self.navigationController popViewControllerAnimated:YES]; //not working no method in self
}
But don't know how to add an event to popViewControllerAnimated:YES in MyUtil class. because i don't have the reference of self there.
popView method
there must be a way to get the context of the class . i mean self in this class. and add event on its behalf. And there is no method of UINavigationItem which will popViewController.
In Simple Words
I simply want to popViewController event on that button which i created in MyUtils . But it must be implemented in MyUtils class. How can i popViewControllerAnimated in another class.

-(void)showBackBarButton:(UIViewController*)controller{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:#"Back" forState:UIControlStateNormal];
[btn addTarget:controller.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(0,0,36,30)];
UIBarButtonItem *barbtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
controller.navigationItem.leftBarButtonItem=barbtn;
}
On ViewDiDLoad
[utils showBackBarButton:self];

Related

How to hide button after the button is manual added in Cocoa Touch?

I have added button manually in for loop, after that, how to hide or change the button and hide label?
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[button setTitle:#"My Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UILabel *lblFileName = [[UILabel alloc] init];
lblFileName.text = [[objectArray objectAtIndex:i] valueForKey:#"fileName"];
lblFileName.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblFileName];
-(IBAction)myAction:(id)sender
{
// hide the button or change title button and hide label
}
If u are adding buttons & labels in for loop, then try following solution.
button.tag = i + 1;//0 is default tag for all views.
lblFileName.tag = i + 1;
-(IBAction)myAction:(id)sender
{
UIButton *btn= (UIButton *)sender;
[btn setHidden:YES];// hide the button
btn.titleLabel.text = [[objectArray objectAtIndex:[sender tag]] valueForKey:#"fileName"];
//Get the label of selected button using button tag.
[[self.view viewWithTag:[sender tag]] setHidden:YES];
}
-(IBAction)myAction:(id)sender
{
// hide the button or change title button
[button setHidden:YES];
}
In myAction:(id)sender sender is your Button.
- (IBAction)myAction:(id)sender {
[sender setHidden:YES];
}
But in this case you cannot make the button visible anymore, because you have no reference to it. I think it would be better to create a property for the button.
-(IBAction)btn:(id)sender
{
btn.hidden=YES;
}
try this.
Other answerers have already shown how sender is the button you want, butif you want to access the button in an different method (for example, if you want to make it visible again) you will need to create a property on the object that created the button.
To do so, put this in your class #interface:
#property (strong) UIButton *myButton;
And #synthesize it in your #implementation:
#implementation WhateverClass
#synthesize myButton = _myButton;
Then you can set the property to your custom button:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[self.myButton setTitle:#"My Button" forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
and access it anywhere (after it has been created) like so:
-(IBAction)myAction:(id)sender
{
[self.myButton setTitle:#"Pressed!" forState:UIControlStateNormal];
[self.myButton setHidden:YES];
}
Give unique tag for each button and label like Girish said.No two tags should not be same.
Consider you have 10 button and 10 label. Then give tag for button 1 to 10 and label 11 to 20.
Then on your method you can access any label or button using corresponding tag.
UIButton *button=(UIButton *)[self.view viewWithTag:1];
UILabel *label=(UILabel *)[self.view viewWithTag:11];

Creating custom buttons for UIBarButton, selector throwing exception

I am creating a custom button with its own image, and assigning it to the rightbarbutton of a nabvigationcontroller I have. The problem is that when the user clicks on the button I get an exceptoin about my controller not recognizing that selctor??
UIImage* image1 = [UIImage imageNamed:#"someImage.png"];
CGRect imgRect = CGRectMake(0, 0, image1.size.width/1.8, image1.size.height/1.8);
UIButton *myButton = [[UIButton alloc] initWithFrame:imgRect];
[myButton setBackgroundImage:image1 forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton =[[UIBarButtonItem alloc] initWithCustomView:myButton];
[self.navigationItem setRightBarButtonItem:myButton];
self.navigationController.navigationBarHidden = false;
May be this is the problem nextScreenButtonAction change it to
[myButton addTarget:self action:#selector(nextScreenButtonAction:) forControlEvents:UIControlEventTouchUpInside]
In your method definition you be passing and argument like
-(IBAction) nextScreenButtonAction:(id)sender
{
}
so in the addTarget you need to specify a colon(:) to indicate parameter
In response to below code,
[myButton addTarget:self action:#selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside];
Make sure that you have create method in the same class like,
- (void) nextScreenButtonAction
{
// write your logic here...
}

Error on adding action to UIButton with selector

I'm trying to add an event to a UIbutton:
UIButton *btn = [[UIButton alloc] init];
[btn setBackgroundImage:[UIImage imageNamed:#"aa.png"]
forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(Presed:)];
and I'm getting this warning :
"UIButtom may not respond to addTarget:action"
The interface I'm creating this button for inherits from CDVPlugin
what's wrong with my code?
you are calling a method that is not declared.
it should be.
– addTarget:action:forControlEvents:
[btn addTarget:self action:#selector(Presed:)
forControlEvents:UIControlEventTouchUpInside];

Function with variable blind to a button xcode

Hi I have a problem i can not send parameter for function
-(IBAction)ukazObrat:(NSInteger *)cis {
stvrtyViewController *screen = [[stvrtyViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
screen.obratLabel.font = [UIFont systemFontOfSize: 12];
screen.obratLabel.numberOfLines = 0;
screen.obratLabel.text = [array objectAtIndex:cis];
[screen release];
}
I set this function to a button klick but I m unable to compile it when I add parameter to it.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[button.titleLabel setTextAlignment:UITextAlignmentLeft];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setTitle:obratLable forState:UIControlStateNormal];
[button addTarget:self
action:#selector([self ukazObrat:minus]
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(0.0, 0.0, 250.0, 60.0);
It was working until I add there the variable.
please help
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventTouchDown];
Then define buttonHandler as:
- (void)buttonHandler {
[self ukazObrat:minus];
}
Manage minus however is necessary.
You can't set variable into selector definition. Definition can contain only function name. And number of colons will show compiler how much parameters this function required. Selector for button can have one argument - pointer to event-sender button. If you want to send some data from one controller to another you should create some shared storage for that data.
I use for that purpose singleton class with mutable dictionary inside it.

How to associate a method with this button?

I have a navigation bar button that displays both image and text. This is the code:
UIImage *saveImage = [UIImage imageNamed:#"star.png"];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:saveImage forState:UIControlStateNormal];
[saveButton setTitle:#"Save" forState:UIControlStateNormal];
saveButton.frame = (CGRect) {
.size.width = 100,
.size.height = 30,
};
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:saveButton];
[self.navigationItem setRightBarButtonItem:barButton animated:YES];
I tried this
[barButton setAction:#selector(saveArray)];
but it doesn't work.
Remember that you must specify target as well. You can set action/target to the UIButton object itself:
[saveButton addTarget:target action:#selector(saveArray) forControlEvents:UIControlEventTouchUpInside];
I've tried to set target/action to the UIBarButtonItem directly but it seems not to work in case of UIButton for custom view for some reason.
Did you remember to set the target? If the desired method belongs to the same class, you could do:
[barButton setTarget:self];
That said, action and target are both properties (new to Obj-C 2.0), consider using the dot notation:
barButton.action = #selector(saveArray:)
barButton.target = self;