【问题标题】:Accessing Method from other Classes Objective-C从其他类Objective-C访问方法
【发布时间】:2010-12-12 02:53:36
【问题描述】:

正在寻找这个问题的答案,但我还没有找到合适的答案。我希望你们(和女孩)可以帮助我! (这是针对 iPhone 应用的)

好的,我有一个 Mutliview 应用程序。每个视图都有自己的类,一切都很愉快。但是,不同的类有时会调用相同的方法。到目前为止,我只是在两个类文件中编写了两次该方法。

这就是我想要做的:

我想在它自己的文件中创建一个新类,它包含所有“通用”方法。然后,每当另一个类需要调用该方法时,我只需从另一个文件中调用它。这样,当我想更改 Method 时,我只需要更改一个地方,而不是所有地方...

我不知道该怎么做,这就是我寻求帮助的原因。我对 Objective-C 有点生疏和新手,所以漂亮的例子会对我有很大帮助。请允许我给你一个。

文件:ViewController1.m

@implementation ViewController1

//Do Some awesome stuff....

CALL "CommonMethod" HERE

@end

文件:ViewController2.m

@implementation ViewController2

//Do Some awesome stuff....

CALL "CommonMethod" HERE

@end

文件:CommonClass

@implementation commonClass

- (void)CommonMethod:(id)sender
{

//So some awesome generic stuff...



    }
@end

我觉得我需要#import 另一个文件,从类中创建一个对象并从对象中调用方法...我该怎么做?

再次感谢!

【问题讨论】:

    标签: iphone objective-c class object methods


    【解决方案1】:

    在分配和初始化视图时传入对 commonClass 的引用...

    CommonClass *cc = [[CommonClass alloc] init];
    
    ViewController1 *vc1 = [[ViewController1 alloc] ... initWith:cc];
    ViewController2 *vc2 = [[ViewController2 alloc] ... initWith:cc];
    

    但是制作一个经典的 c 包含可能就足够了。

    【讨论】:

      【解决方案2】:

      您可以使用的另一种方法

      @interface ServerManager : NSObject
      
      +(ServerManager *)getInstance;
      
      @implementation ServerManager
      
      +(ServerManager *)getInstance
      {
      static ServerManager *objServerManager = nil;
      
      if(objServerManager==NULL){
      objServerManager=[[self alloc] init];
      }
      // Return the servermanager object.
      return objServerManager;
      }
      

      调用是否要使用

      ServerManager *SMObject =   [ServerManager getInstance];
      

      别忘了导入 servermanager.h 文件。

      【讨论】:

        【解决方案3】:

        作为具有 Java 背景和小 C 的 iPhone 新手,我遇到了类似的问题,希望同时引用 RootController 和 ViewController 中的方法。在我看来,该方法的正确位置是 AppDelegate 类,一个实例可以通过以下方式在其他类中获得:

        MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
        

        然后,如果方法是“doSomething”,则可以通过以下方式访问它:

        [delegate doSomething];
        

        但也许这太明显了,或者不是所需要的。

        【讨论】:

          【解决方案4】:

          这里有一些答案告诉您创建一个通用的“父”类。但是我认为你可以做得更好。而是为 UIViewController 创建一个category。您不了解 UIViewController 发生的所有内部情况,因此我认为不值得创建自己的 View Controller 层次结构。事实上,这可能很危险。当我尝试创建一个“基础”UITableViewController 然后创建继承自它的类时,我遇到了许多问题。我通过使用类别来避免这些问题。

          您的第一要务不应该是无缘无故地继承东西,而应该是让人们想要下载的应用进入应用商店。

          【讨论】:

            【解决方案5】:

            选项 1:

            @implementation commonClass
            + (void)CommonMethod:(id)sender  /* note the + sign */
            {
            //So some awesome generic stuff...
                }
            @end
            
            @implementation ViewController2
            
            - (void)do_something... {
                [commonClass CommonMethod];
            }
            
            
            @end
            

            选项 2:

            @implementation commonClass
            - (void)CommonMethod:(id)sender
            {
            //So some awesome generic stuff...
                }
            @end
            
            @implementation ViewController2
            
            - (void)do_something... {
                commonClass *c=[[commonClass alloc] init];
                [c CommonMethod];
                [c release];
            }
            
            @end
            

            选项 3:使用继承(参见 Totland 先生在此线程中的描述)

            @implementation commonClass
            - (void)CommonMethod:(id)sender
            {
            //So some awesome generic stuff...
                }
            @end
            
            /* in your .h file */
            @interface ViewController2: commonClass
            
            @end
            

            当然你总是需要在你的视图控制器中#import commonClass.h..

            【讨论】:

            • 你好,这里的commonClass的超类是什么?我的意思是我有一种方法需要从许多不同的类中调用,那么最好的做法是什么?
            • 在 ARC 模式下是否允许使用 [property release]?编译器告诉我这是不允许的。
            • @tymac 这篇文章来自 2009 年,在 ARC 之前。
            【解决方案6】:

            你要做的是让两个控制器共享一个共同的超类:

            UIViewController : MyAwesomeViewController : ViewController1
                                                       : ViewController2
            

            commonMethod: 然后将驻留在 MyAwesomeViewController 中。另外,不要以大写字母开头的方法名称。 :)

            详细说明:

            +@interface MyAwesomeController : UIViewController {
            
            -@interface ViewController1 : UIViewController { // and ditto for ViewController2
            +@interface ViewController1 : MyAwesomeController {
            

            【讨论】:

              【解决方案7】:

              请记住,Objective-C 只是 C 的超集,虽然 #include 指令主要用于头文件,但没有什么能阻止您使用 #include 将一个实现的内容嵌入另一个实现中。如果代码确实相同,您可以轻松地将其粘贴到自己的文件中,然后将其#include 到 .m 文件中。

              话虽如此,也许将这种技术与类别结合使用会更好,特别是如果相同的实现具有相似的行为。

              【讨论】:

                【解决方案8】:

                在我看来,公共代码根本不需要在一个类中。您是否有理由不能只使用 C 风格的函数来做您想做的事情?

                您可以将公共代码放在一个类中,然后将其他两个类作为该类的子类;这种方法也避免了代码重复。

                另一种选择可能是为此公共代码编写类方法而不是实例方法。我认为大多数人都认为最好避免将单例作为设计选择。

                如果我们对您真正想要完成的工作有更多了解,那么给出一个好的答案会更容易。

                【讨论】:

                • 这似乎是没有利用继承的问题。
                • 谢谢。我在 Class1 中有一个方法(method1)。我在 Class1.h 中声明它并在 Class1.m 中实现。我有 Class2,它有一个选择器:@selector(method1)。在这种情况下,我该怎么做?
                • @Williham,听起来就是这样。 @srikanth,也许你需要问一个问题而不是在这里发表评论?
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-07-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-10-10
                • 1970-01-01
                • 2023-04-11
                相关资源
                最近更新 更多