我想从动态生成的按钮中将电影网址传递给MediaPlayer:
[button addTarget:self action:@selector(buttonPressed:) withObject:[speakers_mp4 objectAtIndex:[indexPath row]] forControlEvents:UIControlEventTouchUpInside];
但action:@selector() withObject:
无效?
还有其他解决方案吗?
谢谢帮助!
我想从动态生成的按钮中将电影网址传递给MediaPlayer:
[button addTarget:self action:@selector(buttonPressed:) withObject:[speakers_mp4 objectAtIndex:[indexPath row]] forControlEvents:UIControlEventTouchUpInside];
但action:@selector() withObject:
无效?
还有其他解决方案吗?
谢谢帮助!
编辑。发现了一种更简洁的方法!
按钮可以接收的一个参数是(id)sender
。这意味着你可以创建一个新的按钮,继承自UIButton,允许你存储其他预期的参数。希望这两个代码片段说明了如何操作。
myOwnbutton.argOne = someValue
[myOwnbutton addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
和
- (IBAction) buttonTouchUpInside:(id)sender {
MyOwnButton *buttonClicked = (MyOwnButton *)sender;
//do as you please with buttonClicked.argOne
}
这是我的原始建议。
有一种方法可以实现同样的结果,但它不太美观。假设您想向您的navigate
方法添加一个参数。下面的代码将不允许您将该参数传递给navigate
。
[button addTarget:self action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
为了解决这个问题,你可以将导航方法
移到一个独立的类中,并将"参数"设置为该类的属性...NavigationAid *navAid = [[NavigationAid alloc] init];
navAid.firstParam = someVariableOne
navAid.secondParam = someVariableTwo
[button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
当然,你可以将navigate方法保留在原始类中,并通过navAid调用它,只要navAid知道在哪里找到它。
NavigationAid *navAid = [[NavigationAid alloc] init];
navAid.whereToCallNavigate = self
navAid.firstParam = someVariableOne
navAid.secondParam = someVariableTwo
[button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
就像我之前所说的,这并不漂亮,但它对我起了作用,而且我没有找到其他人提出任何其他可行的解决方案。
-(void) someMethod{
UIButton * but;
but.tag = 1;//some id button that you choice
[but addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
并且这里调用了该方法:
-(void) buttonPressed : (id) sender{
UIButton *clicked = (UIButton *) sender;
NSLog(@"%d",clicked.tag);//Here you know which button has pressed
}
我根据上面的信息做出了一部分解决方案。
我只是设置了titleLabel.text为我想要传递的字符串,并将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;
使用这种方法,不需要继承或类别,也不会出现内存泄漏问题。
您可以通过继承一个名为MyButton的UIButton类,并在MyButton的属性中传递参数。
然后,从(id)sender中获取参数。
-(void)listVideos
{
for(index=0;index<[self.arrUrl count];index++{
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(20,50+60*index,50,50);
NSURL *dUrl = [self.arrUrl objectAtIndex:index];
[imageButton setImage:[[UIImage allow] initWithCGImage:*[self getFrameFromeVideo:dUrl]] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
imageButton.titleLabel.text = [NSString strinfWithFormat:@"%@",dUrl];
imageButton.titleLabel.hidden = YES;
[self.view addSubView:imageButton];
}
}
-(void)playMovie:(id) sender{
UIButton *btn = (UIButton *)sender;
NSURL *movUrl = [NSURL URLWithString:btn.titleLabel.text];
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movUrl];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
-(CGIImageRef *)getFrameFromVideo:(NSURL *)mUrl{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:mUrl option:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
NSError *error =nil;
CMTime = CMTimeMake(3,1);
CGImageRef imageRef = [generator copyCGImageAtTime:time actualTime:nil error:&error];
if(error !=nil) {
NSLog(@"%@",sekf,error);
}
return @imageRef;
}
[btnHome addTarget:self action:@selector(btnMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
btnHome.userInteractionEnabled = YES;
btnHome.tag = 123;
-(void)btnMenuClicked:(id)sender
{
[sender tag];
if ([sender tag] == 123) {
// Do Anything
}
}
我认为正确的方法应该是:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
你从哪里得到这个方法的?
我看到你的选择器有一个参数,这个参数将由运行时系统填充。它将通过那个参数把按钮发送回给你。
你的方法应该像这样:
- (void)buttonPressed:(id)BUTTON_HERE {
}
例如,对于Obj-C,有一个CocoaPod SHControlBlocks,其用法如下:
[self.btnFirst SH_addControlEvents:UIControlEventTouchDown withBlock:^(UIControl *sender) {
[weakSelf performSegueWithIdentifier:@"second" sender:nil];
NSLog(@"first");
}];
UIControl
添加块[1]。// UIControl
let button = UIButton()
button.add(event: .touchUpInside) {
print("Button tapped")
playMusic(from: speakers_mp4, withSongAtPosition: indexPath.row)
}
不是有人在看一个三年前的帖子。 ::蟋蟀::
[1] 和 UIView
、UITextField
、UIGestureRecognizer
、UIBarButtonItem
、Timer
(原名NSTimer)和 NotificationCenter
(原名NSNotificationCenter)。
补充Tristan的回答,除了(id)sender
,按钮还可以接收(id)event
:
- (IBAction) buttonTouchUpInside:(id)sender forEvent:(id)event { .... }
如果按钮位于UITableView
的单元格中,并且您想要找到被触摸的按钮的indexPath
,那么这将非常有用(尽管我认为这也可以通过sender
元素找到)。