2013-03-23 107 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"];