I have a trigger to catch the button click on my controller like this
[saveButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
and the relevant function is
- (void)save:(id)sender {
//implementation code
}
How do call this function in another function, I try to call this function like as follow but it showing error.
(void)checkStatus{
if(somecondition){
[self save];
}
}
Note that I am new to iOS and Objective-C and I am editing a code of another developer. Please give me a solution.
your button action has a parameter , so you need to append in the parameter as nil or self.do like
- (void)checkStatus{
if(somecondition){
[self save:nil];
}
}
Related
I am using UISlider in my project, but I want to fire event when user end dragging the Slider.
I have following code.
[progressBar addTarget:self
action:#selector(sideProgressBarValueChanged)
forControlEvents:UIControlEventValueChanged];
- (void) sideProgressBarValueChanged
{
NSLog(#"Changing values");
// Some action
}
I set this setContinuous to false.
[progressBar setContinuous:false];
but sometimes its not getting call.
I am confused that what else should I use to perform accurate results.
Can anybody suggest something?
I have webview and a Go-Back button for webview.
I need two functionality from Go-back button(as in standard browser)
Button should be disabled when it is not possible to go back
When pressed, should go back
Disabling the button is achieved by cocoa binding.
And go back action is done by binding following selector from Owner class
- (IBAction)buttonPressed:(id)sender {
[_webView goBack];
}
Since the buttonPressed: selector is only calling goBack selector, can I bind self.webView.goback to the button, eliminating the need of buttonPressed selector in owner class.
You can use following code Disabling your button
- (void)webViewDidFinishLoad:(UIWebView *)webView;{
if ([webView canGoBack]) {
_button.enabled = NO;
}else{
_button.enabled = YES;
}
}
I add UIButton programmatically (self.button is my property UIButton):
self.button = [[UIButton alloc] initWithFrame:CGRectMake(139, 366, 42, 34)];
[self.button addTarget:self action:#selector(buttonPressed:completion:) forControlEvents:UIControlEventTouchUpInside];
I call programmatically to the button target and also I want the framework to invoke the target when the user push the button.
The target selector is:
-(void)buttonPressed:(UIButton*)sender completion:(void (^)())completionBlock;
The second argument is a block.
When I try to introspection/invoke the block I get an exception EXC_BAD_ACCESS (code=2, address=0x0)
I know that I try to invoke UITouchesEvent because of the framework target action.
How can I make custom target with completion block?
You can't pass a completion block there, but you can make something like this:
self.button = [[UIButton alloc] initWithFrame:CGRectMake(139, 366, 42, 34)];
[self.button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(void)buttonPressed:(UIButton*)sender {
[self buttonPressed:sender completion:^{
//something
}];
}
-(void)buttonPressed:(UIButton*)sender completion:(void (^)())completionBlock {
//do something
//invoke block
completionBlock();
}
You can't. For the button action, system expect to have a single parameter and that too is the sender(Invoker of the action method) ie the UIButton instance itself. If you want to do anything with a second argument i would suggest you to have a wrapper some thing like
-(void)buttonPressed:(UIButton*)sender
{
[self customMethodWithcompletion:^{
}];
}
Inside the customMethodWithcompletion you can perform your operations.
A target-action listener for UIKit must have one of the following three signatures:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
If there is a first parameter, the "sender" control (the control that you are listening to actions for) will be passed to it. If there is furthermore a second parameter, the UIEvent object will be passed to it. You don't have any control over what is passed to the method.
There are many third-party libraries that implement a Block-based API for UIControls that you can find on the Internet. Essentially what they do is attach the completion block as an associative object to the control, and retrieve it in the handling method.
essentially I'm deleting pictures inside of an app.. everything is coded etc.. etc.. so when the user holds the UIButton the image is swapped and has the X over it. Click again and image is deleted from the Doc Directory and DB. So it only made sense to add an alertview before deletion.. problem is that the methods I'm using use (UIButton*)sender as a parameter. I need to pass that parameter to the next method to property delete from the screen.
Is there a relatively simple way to do this..
this is the function that calls the deletion.. the function that would initiate the alertview is also returns a void and takes the same UIButton.
-(void)action:(UIButton*)sender {
if (edit == true)
{
[sender removeFromSuperview];
[[scrollView viewWithTag:[sender tag]] removeFromSuperview];
[self deleteFromDoc:sender];
edit = false;
stop = false;
NSLog(#"remove");
}
}
Change your method to
-(void)action:(UIView *)sender
Assign the tag of the uibutton to the alertview, then in didClickButtonWithIndex: call your action: method passing in the alertView as sender.
I have a simple UIButton in a viewController.
- (IBAction)btnEnter:(id)sender {
..
..
}
Now I want to trigger the button press action programatically one of library functions (didFinishPickingmediaWithInfo) gets executed
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// Do Something
txtBoxAboveEnterButton = #"Data populated successfully";
[self btnEnter:nil];
[reader dismissModalViewControllerAnimated: YES];
}
The function is getting called and the text box above Enter button is getting populated with the dummy text but click is not getting triggered. Please help me out!
The method you are calling is btnEnter with no parameter. It doesn't look like you have that defined. Try passing a paramter like nil or self to it:
[self btnEnter:nil];