Pas*_*yer 49 iphone parameters action selector media-player
我想将动态生成按钮中的电影网址传递给MediaPlayer:
[button addTarget:self action:@selector(buttonPressed:) withObject:[speakers_mp4 objectAtIndex:[indexPath row]] forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
但action:@selector() withObject:不起作用?
还有其他解决方案吗?
感谢帮助!
Woo*_*tty 61
编辑.找到一个更整洁的方式!
按钮可以接收的一个参数是(id)sender.这意味着您可以创建一个继承自UIButton的新按钮,该按钮允许您存储其他预期参数.希望这两个片段说明了该做什么.
myOwnbutton.argOne = someValue
[myOwnbutton addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
和
- (IBAction) buttonTouchUpInside:(id)sender {
MyOwnButton *buttonClicked = (MyOwnButton *)sender;
//do as you please with buttonClicked.argOne
}
Run Code Online (Sandbox Code Playgroud)
这是我最初的建议.
有一种方法可以达到相同的效果,但它并不漂亮.假设您要为方法添加参数navigate.下面的代码不允许您传递该参数navigate.
[button addTarget:self action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,你可以将导航移动method到它自己的类,并将"参数"设置为该类的属性......
NavigationAid *navAid = [[NavigationAid alloc] init];
navAid.firstParam = someVariableOne
navAid.secondParam = someVariableTwo
[button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
当然,您可以将导航方法保留在原始类中,并让navAid调用它,只要它知道在哪里找到它.
NavigationAid *navAid = [[NavigationAid alloc] init];
navAid.whereToCallNavigate = self
navAid.firstParam = someVariableOne
navAid.secondParam = someVariableTwo
[button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
就像我说的,它不漂亮,但它对我有用,我没有发现任何人建议任何其他工作的解决方案.
小智 32
我找到了解决方案 电话:
-(void) someMethod{
UIButton * but;
but.tag = 1;//some id button that you choice
[but addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
Run Code Online (Sandbox Code Playgroud)
这里的方法称为:
-(void) buttonPressed : (id) sender{
UIButton *clicked = (UIButton *) sender;
NSLog(@"%d",clicked.tag);//Here you know which button has pressed
}
Run Code Online (Sandbox Code Playgroud)
Tra*_*hor 12
我部分地根据上述信息制定了解决方案.我只是设置了标题标签.文本到我要传递的字符串,并设置titleLabel.hidden = YES
像这样 :
UIButton *imageclick = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
imageclick.frame = photoframe;
imageclick.titleLabel.text = [NSString stringWithFormat:@"%@.%@", ti.mediaImage, ti.mediaExtension];
imageclick.titleLabel.hidden = YES;
Run Code Online (Sandbox Code Playgroud)
这样,就不需要继承或类别,也没有内存泄漏
| 归档时间: |
|
| 查看次数: |
109349 次 |
| 最近记录: |