开发者

How can i Pass the arguments in the button selector method in iPhone?

开发者 https://www.devze.com 2023-02-05 22:40 出处:网络
Now i want to pass the arguments in the button selector method, so how can i pass that arguments in the button selector method?

Now i want to pass the arguments in the button selector method, so how can i pass that arguments in the button selector method?

    //Button creation

    -(void) viewDidLoad
    {
         UIButton *imageOverlayBtn = [[UIButton 开发者_JS百科buttonWithType:UIButtonTypeRoundedRect] retain];

     imageOverlayBtn.frame = CGRectMake(xOrigin, yOrigin, theSize.width, theSize.height);

            //Pass this string into selector method 
     NSString *passString = [feedArray objectForKey:@"URLString"];

     [imageOverlayBtn addTarget:self action:@selector(overlayBtnAction:) forControlEvents:UIControlEventTouchUpInside];

       }

    // Button Action
    -(void) overlayBtnAction : (NSString *) getString
    {
          NSLog(@"The get string is %@", getString)
    }

So how can i pass the string to the selector method?. Please Help me out.

Thanks.


First of all your action method should be implemented with this signature:

-(void) overlayBtnAction : (id) sender
{
...
}

Where sender is the pointer to the UIButton (in general case to a UIControl) which have invoked this handler.

Your target is self, so you can declare a NSString variable being member of this class.

So in viewDidLoad you will have:

  self.passString = [feedArray objectForKey:@"URLString"];

And in handler just use its value:

-(void) overlayBtnAction : (id) sender
{
    NSString *localVar = self.passString;
}


If you need a string to persist between methods like this, I'd suggest putting it in your header file. That way, you can set it when you need to and access it from anywhere in the implementation. I don't think passing an argument to a UI action is good design practice.

0

精彩评论

暂无评论...
验证码 换一张
取 消