2013-03-23 85 views
0

在我正在处理的应用中,我有一个UIViewController sublcass和一个UIView子类。在storyboard视图控制器包含UIview。在uiview中,我绘制了一些东西,但我需要它来了解它应该从视图控制器获得的一些值。因此,我在视图控制器创建的自定义协议h文件:自定义协议不起作用

@protocol SSGraphViewControllerProtocol <NSObject> 

- (void)numberOfSemesters:(int)number; 

@end 

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

并在UIView I类确认它作为具有上述的协议和我实现它的方法。然而。当我从视图控制器传递一个数字时,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

请张贴更多的代码,以便我们为您提供帮助。你在哪里实现了自定义协议,你是否使用view.delegate = self等... – Scar

+0

是的,视图控制器具有自定义类作为我创建的自定义类,它的视图也有我创建的自定义类。顺便说一句,我一直在与协议和代表团合作,并且我意识到我无法通过导航将代表从父母发送给孩子。真的吗? – HusseinB

回答

0

阅读这篇文章,它是描述

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

Also read for create Protocol

最好的例子,下面笔者描述了如何创建协议

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

} 
+0

中调用它,这正是我如何做的,但不能将委托工作从主视图转移到详细视图?我的意思是我不能在masterView的.h文件中创建协议并使用它的详细视图?实际上在我的情况下,我有一个UIViewController和UIView,我想从视图控制器传递数据到视图..有没有办法做到这一点?即使没有代表团?我正在考虑使用单身。 – HusseinB

+0

@ user2176995-遵循我的代码,它为我工作得很好:) – iPatel

+0

好吧,我添加了完整的代码。 – HusseinB

0

你创建一个视图简单的例子initWithFrame中的控制器实例:,将它的委托分配给自己,然后不保留对控制器的引用或添加将其视图放入视图层次结构中。这当然不是你要做的。改为在故事板中建立连接,方法是将委托属性设置为IBOutlet,并通过右键单击视图控制器并将其从属性名称旁边的圆圈拖到视图实例上进行连接。

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

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"];