[英]I want to change the view controller when a button it's pressed iphone
I have a table view controller with custom cells. 我有一个带有自定义单元格的表视图控制器。 In those cells i added a button for each one of the cells.
在这些单元格中,我为每个单元格添加了一个按钮。 What i would like it's that when I press that button it display a new view with more information about that cell, different of the view that i get from didSelectRowAtIndexPath.
我想要的是,当我按下该按钮时,它将显示一个新视图,其中包含有关该单元格的更多信息,这与我从didSelectRowAtIndexPath获得的视图不同。
I know it's now a hard question. 我知道这是一个难题。 I've seen that the button has some action on interfacebuilder (touch down maybe) but how do i link it to what code.
我已经看到该按钮在interfacebuilder上有一些动作(也许按下),但是我如何将其链接到什么代码。 where should i declare the code that handle that event?
我应该在哪里声明处理该事件的代码?
I've posted in other forums with no answer, hope this would work. 我已经在其他论坛上张贴了没有答案,希望这可以工作。
Thanks. 谢谢。 Gian
ian
I would probably subclass UIButton to have an instance of NSIndexPath. 我可能会子类化UIButton以具有NSIndexPath的实例。 That way, each individual UIButton in a UITableViewCell can "know" where it is in the table view, so when you press the button, you can call some method that takes an NSIndexPath and pushes a new view, similar to what you do in
-didSelectRowAtIndexPath:
but with your other view instead (maybe give it a descriptive method name like -didPressButtonAtIndexPath:
). 这样,UITableViewCell中的每个单独的UIButton都可以“知道”它在表视图中的位置,因此,当您按下按钮时,您可以调用某些方法,该方法采用NSIndexPath并推送一个新视图,类似于您在
-didSelectRowAtIndexPath:
但使用其他视图代替(也许给它一个描述性的方法名称,如-didPressButtonAtIndexPath:
。
Instead of using the Interface Builder to do this, you should add a method to the UIButton subclass itself that in turn calls a method on your view controller. 而不是使用Interface Builder来执行此操作,您应该向UIButton子类本身添加一个方法,该方法又会在视图控制器上调用一个方法。 Then, for each UIButton, you can use the UIControl method
-addTarget:action:forControlEvents:
. 然后,对于每个UIButton,可以使用UIControl方法
-addTarget:action:forControlEvents:
Have the UIButton call its own method, which calls the controller's method. 让UIButton调用自己的方法,该方法调用控制器的方法。 Your solution might look something like:
您的解决方案可能类似于:
// MyButton.h
@interface MyButton : UIButton {
NSIndexPath *myIndexPath;
MyViewController *viewController;
}
- (void)didPressButton;
@end
// MyViewController.h
@interface MyViewController { }
- (void)didPressButtonAtIndexPath:(NSIndexPath *)indexPath;
@end
Then, when you build your cells, for each button you add call: 然后,在构建单元格时,为每个按钮添加呼叫:
[button addTarget:button
action:@selector(didPressButton)
forControlEvents:UIControlEventTouchDown];
And finally, implement -didPressButton
to look like: 最后,实现
-didPressButton
如下所示:
- (void)didPressButton {
[controller didPressButtonAtIndexPath:myIndexPath];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.