0

在我正在开发的应用程序中,我有一个子类UIViewController和一个UIView子类。在storyboard视图控制器中包含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
{
}
4

3 回答 3

0

阅读本文,这是描述的最佳示例

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);

}
于 2013-03-23T08:53:09.403 回答
0

您正在 initWithFrame: 内创建一个视图控制器实例,将其委托分配给 self,然后不保留对控制器的引用或将其视图添加到视图层次结构中。这当然不是你的本意。而是在情节提要中建立连接,方法是将委托属性设置为 IBOutlet 并通过右键单击视图控制器并从属性名称旁边的圆圈拖动到视图实例来连接它们。

顺便说一句,我不相信以这种方式使用协议的效用。如果视图需要知道一些信息来完成它的工作,如果应该暴露一些可以由控制器设置的属性,或者声明一个 dataSource 协议并查询它的数据源,而不是依赖于视图控制器定义它需要的接口。

于 2013-03-23T10:14:31.637 回答
0
// 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"];
于 2013-03-23T10:34:41.837 回答