Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 595 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing parameters to addTarget:action:forControlEvents

#1
I am using addTarget:action:forControlEvents like this:

<pre><code>[newsButton addTarget:self
action:@selector(switchToNewsDetails)
forControlEvents:UIControlEventTouchUpInside];
</pre></code>

and I would like to pass parameters to my selector "switchToNewsDetails".
The only thing I succeed in doing is to pass the (id)sender by writing:
<pre><code>action:@selector(switchToNewsDetails:)</pre></code>

But I am trying to pass variables like integer values. Writing it this way doesn't work :

<pre><code>int i = 0;
[newsButton addTarget:self
action:@selector(switchToNewsDetails:i)
forControlEvents:UIControlEventTouchUpInside];
</pre></code>

Writing it this way does not work either:

<pre><code>int i = 0;
[newsButton addTarget:self
action:@selector(switchToNewsDetails:i:)
forControlEvents:UIControlEventTouchUpInside];
</pre></code>

Any help would be appreciated :)
Reply

#2
See my comment above, and I believe you have to use NSInvocation when there is more than one parameter

more information on NSInvocation here

[To see links please register here]

Reply

#3
action:@selector(switchToNewsDetails:)

You do not pass parameters to `switchToNewsDetails:` method here. You just create a selector to make button able to call it when certain action occurs (touch up in your case). Controls can use 3 types of selectors to respond to actions, all of them have predefined meaning of their parameters:

1. with no parameters

action:@selector(switchToNewsDetails)

2. with 1 parameter indicating the control that sends the message

action:@selector(switchToNewsDetails:)

3. With 2 parameters indicating the control that sends the message and the event that triggered the message:

action:@selector(switchToNewsDetails:event:)

It is not clear what exactly you try to do, but considering you want to assign a specific details index to each button you can do the following:

1. set a tag property to each button equal to required index
2. in `switchToNewsDetails:` method you can obtain that index and open appropriate deatails:

- (void)switchToNewsDetails:(UIButton*)sender{
[self openDetails:sender.tag];
// Or place opening logic right here
}

Reply

#4
Target-Action allows three different forms of action selector:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
Reply

#5
To pass custom params along with the button click you just need to **SUBCLASS UIButton**.

*(ASR is on, so there's no releases in the code.)*

This is **myButton.h**

#import <UIKit/UIKit.h>

@interface myButton : UIButton {
id userData;
}

@property (nonatomic, readwrite, retain) id userData;

@end

This is **myButton.m**

#import "myButton.h"
@implementation myButton
@synthesize userData;
@end

Usage:


myButton *bt = [myButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0,0, 100, 100)];
[bt setExclusiveTouch:NO];
[bt setUserData:**(insert user data here)**];

[bt addTarget:self action:@selector(touchUpHandler:) forControlEvents:UIControlEventTouchUpInside];

[view addSubview:bt];


Recieving function:

- (void) touchUpHandler:(myButton *)sender {
id userData = sender.userData;
}

If you need me to be more specific on any part of the above code — feel free to ask about it in comments.
Reply

#6
I made a solution based in part by the information above. I just set the titlelabel.text to the string I want to pass, and set the titlelabel.hidden = YES

Like this :

UIButton *imageclick = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
imageclick.frame = photoframe;
imageclick.titleLabel.text = [NSString stringWithFormat:@"%@.%@", ti.mediaImage, ti.mediaExtension];
imageclick.titleLabel.hidden = YES;
This way, there is no need for a inheritance or category and there is no memory leak
Reply

#7
I was creating several buttons for each phone number in an array so each button needed a different phone number to call. I used the setTag function as I was creating several buttons within a for loop:

for (NSInteger i = 0; i < _phoneNumbers.count; i++) {

UIButton *phoneButton = [[UIButton alloc] initWithFrame:someFrame];
[phoneButton setTitle:_phoneNumbers[i] forState:UIControlStateNormal];

[phoneButton setTag:i];

[phoneButton addTarget:self
action:@selector(call:)
forControlEvents:UIControlEventTouchUpInside];
}

Then in my call: method I used the same for loop and an if statement to pick the correct phone number:

- (void)call:(UIButton *)sender
{
for (NSInteger i = 0; i < _phoneNumbers.count; i++) {
if (sender.tag == i) {
NSString *callString = [NSString stringWithFormat:@"telprompt://%@", _phoneNumbers[i]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callString]];
}
}
}

Reply

#8
As there are many ways mentioned here for the solution, Except category feature .

> Use the category feature to extend defined(built-in) element into your
> customisable element.

For instance(ex) :




@interface UIButton (myData)

@property (strong, nonatomic) id btnData;

@end

in the your view Controller.m


#import "UIButton+myAppLists.h"

UIButton *myButton = // btn intialisation....
[myButton set btnData:@"my own Data"];
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

Event handler:

-(void)buttonClicked : (UIButton*)sender{
NSLog(@"my Data %@", sender. btnData);
}


Reply

#9
This fixed my problem but it crashed unless I changed

action:@selector(switchToNewsDetails:event:)

to

action:@selector(switchToNewsDetails: forEvent:)

Reply

#10
I subclassed UIButton in CustomButton and I add a property where I store my data. So I call method: (CustomButton*) sender and in the method I only read my data sender.myproperty.

Example CustomButton:

@interface CustomButton : UIButton
@property(nonatomic, retain) NSString *textShare;
@end

Method action:

+ (void) share: (CustomButton*) sender
{
NSString *text = sender.textShare;
//your work…
}

Assign action

CustomButton *btn = [[CustomButton alloc] initWithFrame: CGRectMake(margin, margin, 60, 60)];
// other setup…

btnWa.textShare = @"my text";
[btn addTarget: self action: @selector(shareWhatsapp:) forControlEvents: UIControlEventTouchUpInside];

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through