Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用';授权';:目标-C_Ios_Objective C_Design Patterns_Callback_Delegates - Fatal编程技术网

Ios 使用';授权';:目标-C

Ios 使用';授权';:目标-C,ios,objective-c,design-patterns,callback,delegates,Ios,Objective C,Design Patterns,Callback,Delegates,我正在实现一个库(.a),我想将通知计数从库发送到应用程序,以便它们可以在UI中显示通知计数。我想让他们实现唯一的方法,比如 -(void)updateCount:(int)count{ NSLog(@"count *d", count); } 如何从我的库中连续发送计数,以便他们可以在updateCount方法中使用它来显示。 我搜索并了解了回调函数。我不知道如何实施它们。还有其他方法吗。您有3种选择 委派 通知 块,也称为回调 我想你需要的是授权 假设这个文件是lib TestLi

我正在实现一个库(.a),我想将通知计数从库发送到应用程序,以便它们可以在UI中显示通知计数。我想让他们实现唯一的方法,比如

-(void)updateCount:(int)count{
    NSLog(@"count *d", count);
}
如何从我的库中连续发送计数,以便他们可以在updateCount方法中使用它来显示。 我搜索并了解了回调函数。我不知道如何实施它们。还有其他方法吗。

您有3种选择

  • 委派
  • 通知
  • 块,也称为回调
  • 我想你需要的是授权

    假设这个文件是lib

    TestLib.h

    #import <Foundation/Foundation.h>
    @protocol TestLibDelegate<NSObject>
    -(void)updateCount:(int)count;
    @end
    
    @interface TestLib : NSObject
    @property(weak,nonatomic)id<TestLibDelegate> delegate;
    -(void)startUpdatingCount;
    @end
    
    然后在你想用的课堂上

    #import "ViewController.h"
    #import "TestLib.h"
    @interface ViewController ()<TestLibDelegate>
    @property (strong,nonatomic)TestLib * lib;
    @end
    
    @implementation ViewController
    -(void)viewDidLoad{
    self.lib = [[TestLib alloc] init];
    self.lib.delegate = self;
    [self.lib startUpdatingCount];
    }
    -(void)updateCount:(int)count{
        NSLog(@"%d",count);
    }
    
    @end
    
    #导入“ViewController.h”
    #导入“TestLib.h”
    @界面视图控制器()
    @属性(强,非原子)TestLib*lib;
    @结束
    @实现视图控制器
    -(无效)viewDidLoad{
    self.lib=[[TestLib alloc]init];
    self.lib.delegate=self;
    [self.lib startUpdatingCount];
    }
    -(void)updateCount:(int)计数{
    NSLog(@“%d”,计数);
    }
    @结束
    
    您读过或吗?
    #import "ViewController.h"
    #import "TestLib.h"
    @interface ViewController ()<TestLibDelegate>
    @property (strong,nonatomic)TestLib * lib;
    @end
    
    @implementation ViewController
    -(void)viewDidLoad{
    self.lib = [[TestLib alloc] init];
    self.lib.delegate = self;
    [self.lib startUpdatingCount];
    }
    -(void)updateCount:(int)count{
        NSLog(@"%d",count);
    }
    
    @end