在我正在处理的应用中,我有一个UIViewController
sublcass和一个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
{
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
您正在initWithFrame:中创建一个视图控制器实例,将其委托指定为self,然后不保留对控制器的引用或将其视图添加到视图层次结构中。这当然不是你想要做的。通过将委托属性设置为IBOutlet并通过右键单击视图控制器并将属性名称旁边的圆圈拖到视图实例上来连接它们,从而在故事板中建立连接。
另外,我不相信以这种方式使用协议的效用。如果视图需要知道某些信息来完成它的工作,那么是应该公开一些可以由控制器设置的属性,还是声明一个dataSource协议并查询其dataSource,而不是依赖于定义所需接口的视图控制器。 / p>
答案 2 :(得分: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"];