简体   繁体   English

我想在按下按钮时更改视图控制器

[英]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.

相关问题 当我按下iPhone上的其他按钮时,更改按钮上的图像 - Change image on button when I pressed other button on iPhone 按下时如何更改iPhone上按钮的默认颜色? - How can I change the default color of the button on iPhone when pressed? iPhone-使用子视图中的按钮更改导航控制器的视图 - iPhone - change navigation controller's view, using a button in a subview 按下主页按钮时关闭视图控制器 - Dismiss view controller when the home button pressed 我需要在按下按钮时使用.xib加载视图(不为此xib使用视图控制器) - i need to load view using .xib when button pressed (without using view controller for this xib) 在导航控制器中按下“后退”按钮时,是否可能不取消分配视图控制器? - is it possible to not dealloc a view controller when the back button is pressed in a navigation controller? 按下iPhone时如何切换到下一个视图? - How to switch to next view when button is pressed iPhone? 按下后退按钮,当我将视图控制器从堆栈中弹出时,将丢失我的自定义UIToolBar - Back button pressed, losing my custom UIToolBar when i pop the view controller off the stack 当按下UITextField / UISearchBar的清除按钮时,iPhone会执行操作 - iPhone perform action when UITextField / UISearchBar's clear button is pressed 当我按下按钮时,我想在iPhone应用程序开发中创建一个标签栏控制器。 - When i press a button then i want to make a tabbar controller in iPhone application development.how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM