How to pass variable to new method I call when using @selector(methodname)

前端 未结 4 1181
自闭症患者
自闭症患者 2020-12-22 05:58

Ok very quick question. I am adding annotations to my iOS using MKMapAnnotation. I create a int and an annotation with a disclosure button Which calls the method loadPano l

4条回答
  •  庸人自扰
    2020-12-22 06:32

    If you just have to pass an integer associated to each disclosurebutton, you can set disclosurebutton.tag = integer value;.

    Sort of hacky to pass data around in tags but in simple cases it works.

    Also for this to work, declare loadpano this way:

    - (void)loadPano:(UIButton*)sender
    {
        NSInteger relevantInteger = sender.tag;
       // More code here
    }
    

    And set the target like this:

    [disclosureButton addTarget:self 
                         action:@selector(loadPano:) 
               forControlEvents:UIControlEventTouchUpInside];    
    

    Note that the method now takes a parameter so the selector includes a colon.

提交回复
热议问题