【问题标题】:Call method of another class (for example via singleton class)?调用另一个类的方法(例如通过单例类)?
【发布时间】:2011-10-21 05:16:48
【问题描述】:

我使用this 简单教程来创建我的单例类。一切正常。教程中没有说明的一件事是,如何在该类中创建方法,以便我可以从其他类(例如其他 ViewController 或 AppDelegate)访问它们。

我该怎么做? 提前非常感谢!

【问题讨论】:

    标签: iphone ios methods uiviewcontroller


    【解决方案1】:

    您将像在任何其他 Objective-C 文件中一样定义方法。在公共方法的头部添加定义,然后在实现(.m)文件中实现它们。

    #import <foundation/Foundation.h>
    
    @interface MyManager : NSObject {
        NSString *someProperty;
    }
    
    @property (nonatomic, retain) NSString *someProperty;
    
    + (id)sharedManager;
    
    //Add instance methods for your singleton here
    - (void)someSingletonMethod;
    
    @end
    

    用法:

    [[MyManager sharedManager] someSingletonMethod];
    

    【讨论】:

      【解决方案2】:

      单例 .h 文件

      #import <Foundation/Foundation.h>
       @interface SingleTon : NSObject
      {
        NSString *sum;
      
      }
      
      +(SingleTon *) createSingleTon;
      -(NSString *) sumOfTwoNumbers:(NSString *) numOne :(NSString *)numTwo;
      @end
      

      单例 .m 文件

      #import "SingleTon.h"
      
      @implementation SingleTon
      
      +(SingleTon *) createSingleTon
      {
          static SingleTon *single= nil;
          if (single == nil) {
      
              single = [[SingleTon alloc] init];
          }
          return single;
      }
      
      -(NSString *) sumOfTwoNumbers:(NSString *) numOne :(NSString *)numTwo
      {
          sum =  [NSString stringWithFormat:@"%d",[numOne intValue] + [numTwo intValue]];
          return sum;
      }
      
      @end
      

      viecontroller.h 文件

      #import <UIKit/UIKit.h>
      #import "SingleTon.h"
      @interface ViewController : UIViewController
      {
          SingleTon *sing;
          IBOutlet UITextField *one,*two,*sum;
      }
      -(IBAction)sum:(id)sender;
      @end
      

      viecontroller.m 文件

      #import "ViewController.h"
      
      @interface ViewController ()
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          sing = [SingleTon createSingleTon];
          // Do any additional setup after loading the view, typically from a nib.
      }
      
      -(IBAction)sum:(id)sender
      {
          sum.text = [sing sumOfTwoNumbers:one.text :two.text];
      
      }
      @end
      

      o/p

      【讨论】:

        【解决方案3】:

        像这样为你的第一个类创建一个类方法。也在 .h 文件中声明它。

        +(FFMainVC *)sharedSingleton
        {
            static FFMainVC *instance = nil;
        
            if(instance == nil)
                instance = [[FFMainVC alloc]init];
        
            return instance;
        }
        // write your method that you wants to access from other class. also declare this in .h as well
        
        -(void)showCartView
        { 
               // Your Code
        }  
        // make call of your method from second class like this
        [[FFMainVC sharedSingleton]showCartView];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-08
          • 2015-06-10
          • 2018-02-27
          • 2011-12-30
          • 1970-01-01
          • 2014-01-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多