<i id='TFeF7'><tr id='TFeF7'><dt id='TFeF7'><q id='TFeF7'><span id='TFeF7'><b id='TFeF7'><form id='TFeF7'><ins id='TFeF7'></ins><ul id='TFeF7'></ul><sub id='TFeF7'></sub></form><legend id='TFeF7'></legend><bdo id='TFeF7'><pre id='TFeF7'><center id='TFeF7'></center></pre></bdo></b><th id='TFeF7'></th></span></q></dt></tr></i><div id='TFeF7'><tfoot id='TFeF7'></tfoot><dl id='TFeF7'><fieldset id='TFeF7'></fieldset></dl></div>
  • <tfoot id='TFeF7'></tfoot>
    <legend id='TFeF7'><style id='TFeF7'><dir id='TFeF7'><q id='TFeF7'></q></dir></style></legend>

    <small id='TFeF7'></small><noframes id='TFeF7'>

        • <bdo id='TFeF7'></bdo><ul id='TFeF7'></ul>

        Objective-C 中的匿名委托实现?

        Anonymous delegate implementation in Objective-C?(Objective-C 中的匿名委托实现?)

        <i id='Hca9c'><tr id='Hca9c'><dt id='Hca9c'><q id='Hca9c'><span id='Hca9c'><b id='Hca9c'><form id='Hca9c'><ins id='Hca9c'></ins><ul id='Hca9c'></ul><sub id='Hca9c'></sub></form><legend id='Hca9c'></legend><bdo id='Hca9c'><pre id='Hca9c'><center id='Hca9c'></center></pre></bdo></b><th id='Hca9c'></th></span></q></dt></tr></i><div id='Hca9c'><tfoot id='Hca9c'></tfoot><dl id='Hca9c'><fieldset id='Hca9c'></fieldset></dl></div>

          <bdo id='Hca9c'></bdo><ul id='Hca9c'></ul>

              <tfoot id='Hca9c'></tfoot>
                <legend id='Hca9c'><style id='Hca9c'><dir id='Hca9c'><q id='Hca9c'></q></dir></style></legend>

                  <small id='Hca9c'></small><noframes id='Hca9c'>

                    <tbody id='Hca9c'></tbody>
                • 本文介绍了Objective-C 中的匿名委托实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以在 Objective-C 中声明诸如 Delegates 之类的匿名实现.我认为我的术语是正确的,但这是一个 java 示例:

                  Is it possible to declare anonymous implementations of things like Delegates in Objective-C. I think I have the terminology right, but here's a java example:

                  myClass.addListener(new FancyInterfaceListener({
                      void onListenerInterestingAction(Action a){
                          ....interesting stuff here
                      }
                  });
                  

                  例如,为了处理 UIActionSheet 调用,我必须在同一个类中声明另一个方法,如果我想向它传递数据,这似乎有点愚蠢,因为我必须将该数据存储为全局变量.这是一个删除某些内容的示例,其中包含一个询问您是否确定的确认对话框:

                  So for example to handle an UIActionSheet call I have to declare another method in the same class, which seems a bit silly if I want to pass it data, because I'd have to store that data as a global variable. Here's an example of deleting something with a confirmation dialog asking you if your sure:

                  -(void)deleteItem:(int)indexToDelete{
                      UIActionSheet *confirm = [[UIActionSheet alloc] initWithTitle:@"Delete Item?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:nil];
                      [confirm showInView:self.view];
                      [confirm release];
                  }
                  

                  和 UIActionSheetDelegate 在同一个类中:

                  and the UIActionSheetDelegate in the same class:

                  - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
                      if (buttonIndex == 0){
                          [[Settings sharedSettings] removeItemAtIndex:/*need index variable here*/];
                          [drinksTable reloadData];
                      }
                  }
                  

                  我想要做的是内联声明它,就像我在顶部的 java 示例中所做的那样.这可能吗?

                  What I want to be able to do is declare it inline, just like I did in the java example at the top. Is this possible?

                  推荐答案

                  目前在 Objective-C 中没有办法做到这一点.Apple 已经发布了一些关于向语言添加块(实际上更像 lambda 闭包而不是匿名类)的工作.您可能可以用这些来做类似于匿名委托的事情.

                  There is no way to do this in Objective-C currently. Apple has published some work on their efforts to add blocks (really more like lambda closures than anonymous classes) to the language. You would likely be able to do something similar to the anonymous delegate with those.

                  与此同时,大多数 Cocoa 程序员将委托方法添加到委托类的单独类别中.这有助于使代码更有条理.在您示例中的类的 .m 文件中,我会执行以下操作:

                  In the mean time, most Cocoa programmers add the delegate methods to a separate category on the delegate class. This helps to keep the code more organized. In the .m file for the class in your example, I would do something like this:

                  @interface MyClass (UIActionSheetDelegate)
                  - (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
                  @end
                  
                  @implementation MyClass
                  //... normal stuff here
                  @end
                  
                  @implementation MyClass (UIActionSheetDelegate)
                  - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
                      if (buttonIndex == 0){
                          [[Settings sharedSettings] removeItemAtIndex:/*need index variable here*/];
                          [drinksTable reloadData];
                      }
                  }
                  @end
                  

                  编辑器窗口中 Xcode 的方法弹出窗口会将类别的声明和实现与主类分开.

                  Xcode's method popup in the editor window will separate the category's declaration and implementation from the main class'.

                  这篇关于Objective-C 中的匿名委托实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Auto Layout still required after executing -layoutSubviewsquot; with UITableViewCell subclass(“执行 -layoutSubviews 后仍需要自动布局带有 UITableViewCell 子类)
                  How do I animate constraint changes?(如何为约束更改设置动画?)
                  How to change the preferred language of an iPhone app in iOS 4(如何在 iOS 4 中更改 iPhone 应用程序的首选语言)
                  NSLocale currentLocale always returns quot;en_USquot; not user#39;s current language(NSLocale currentLocale 总是返回“en_US;不是用户当前的语言)
                  set delegate and datasource of uitableview programmatically in Swift(在 Swift 中以编程方式设置 uitableview 的委托和数据源)
                  How can I wait for a NSURLConnection delegate to finish before executing the next statement?(在执行下一条语句之前,如何等待 NSURLConnection 委托完成?)
                  <i id='Xr7F3'><tr id='Xr7F3'><dt id='Xr7F3'><q id='Xr7F3'><span id='Xr7F3'><b id='Xr7F3'><form id='Xr7F3'><ins id='Xr7F3'></ins><ul id='Xr7F3'></ul><sub id='Xr7F3'></sub></form><legend id='Xr7F3'></legend><bdo id='Xr7F3'><pre id='Xr7F3'><center id='Xr7F3'></center></pre></bdo></b><th id='Xr7F3'></th></span></q></dt></tr></i><div id='Xr7F3'><tfoot id='Xr7F3'></tfoot><dl id='Xr7F3'><fieldset id='Xr7F3'></fieldset></dl></div>

                  1. <small id='Xr7F3'></small><noframes id='Xr7F3'>

                      • <bdo id='Xr7F3'></bdo><ul id='Xr7F3'></ul>

                          1. <tfoot id='Xr7F3'></tfoot>
                          2. <legend id='Xr7F3'><style id='Xr7F3'><dir id='Xr7F3'><q id='Xr7F3'></q></dir></style></legend>
                              <tbody id='Xr7F3'></tbody>