簡體   English   中英

自定義協議不起作用

[英]custom protocol doesn't work

在我正在使用的應用程序中,我有一個UIViewController子類和一個UIView子類。 storyboard UIview ,視圖控制器包含UIview 在uiview中,我正在繪制一些東西,但我需要它知道它應該從視圖控制器獲取的一些值。 所以我在視圖控制器.h文件中創建了一個自定義協議:

@protocol SSGraphViewControllerProtocol <NSObject>

- (void)numberOfSemesters:(int)number;

@end

@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;

UIView類中,我確認它具有上述協議,並實現了其方法。 然而。 當我從視圖控制器傳遞一個數字時, UIView不會收到它。 使用NSLog,我發現UIView沒有輸入- (void)numberOfS:(int)number; 我做錯什么了嗎? 我該如何解決? 還有另一種方法可以將數據從UIViewController類發送到UIView控制器嗎?

這是完整的代碼:UIViewController.h

@protocol SSGraphViewControllerProtocol <NSObject>

- (void)numberOfSemesters:(int)number;

@end

@interface SSGraphViewController : UIViewController
@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;
@end

UIViewController.m

@implementation SSGraphViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.delegate numberOfSemesters:2];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

UIView.h

@interface SSGraph : UIView <SSGraphViewControllerProtocol>
@end

UIView.m

static int numberOfS = 0;

@implementation SSGraph

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }

    SSGraphViewController *graph = [[SSGraphViewController alloc] init];
    graph.delegate = self;
    return self;
}

- (void) numberOfSemesters:(int)number{NSLog(@"YES");
    numberOfSemesters= number;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
}

閱讀本文,這是帶有說明的最佳示例

http://css.dzone.com/articles/do-not-publishcreating-your

另請參閱創建協議

以下我描述了如何創建協議的簡單示例

#DetailViewController.h

#import <UIKit/UIKit.h>

@protocol MasterDelegate <NSObject>
-(void) getButtonTitile:(NSString *)btnTitle;
@end


@interface DetailViewController : MasterViewController

@property (nonatomic, assign) id<MasterDelegate> customDelegate; 

#DetailViewController.m

if([self.customDelegate respondsToSelector:@selector(getButtonTitile:)])
{
          [self.customDelegate getButtonTitile:button.currentTitle];    
}

#MasterViewController.m

create obj of DetailViewController

DetailViewController *obj = [[DetailViewController alloc] init];
obj.customDelegate = self;
[self.navigationController pushViewController:reportTypeVC animated:YES];

and add delegate method in MasterViewController.m for get button title.

#pragma mark -
#pragma mark - Custom Delegate  Method

-(void) getButtonTitile:(NSString *)btnTitle;
{
    NSLog(@"%@", btnTitle);

}

您正在initWithFrame:中創建視圖控制器實例,將其委托分配為self,然后不保留對控制器的引用或將其視圖添加到視圖層次結構中。 這當然不是您的本意。 相反,通過將委托屬性設置為IBOutlet,然后在視圖控制器上單擊鼠標右鍵,然后從屬性名稱旁邊的圓圈拖動到您的視圖實例上,即可在故事板上建立連接。

順便說一句,我不相信以這種方式使用協議的實用性。 如果視圖需要知道一些信息來完成其工作,則應該公開一些可由控制器設置的屬性,或者聲明一個dataSource協議並查詢其dataSource,而不是依賴於視圖控制器定義所需的接口。

// Add an observer to your ViewController for some action in uiview
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveActionNotification:)
                                             name:@"someActionNotification"
                                           object:nil];

// Post Notification and method in your Viewcontroller will be called
[[NSNotificationCenter defaultCenter] postNotificationName:@"someActionNotification" object:self];

// at the end Dont forget to remove Observer.
[[NSNotificationCenter defaultCenter] removeObserver:@"someActionNotification"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM