2017-04-21 160 views
1

我有三個的UIViewController,即視圖控制器,BViewController和CViewController,視圖控制器是RootViewController的距離的UINavigationController,協議/委託不工作

我是從推的ViewController - > BViewController - > CViewController,通過按鈕操作。

我已經在CViewController中聲明瞭協議。這個協議方法對BViewController工作正常,但我不明白如何在ViewController中使其委託方法工作。

我已經在View Controller中創建了CViewController對象,然後爲該代理聲明瞭自己,儘管它不工作,下面是我的代碼。請幫幫我。我在這裏做錯了!

我的根視圖控制器,即視圖控制器

#import "ViewController.h" 
#import "BViewController.h" 
#import "CViewController.h" 

@interface ViewController()<testDelegates> 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"]; 
    cVC.mydelegate = self; 
} 

- (IBAction)nextAction:(id)sender { 

    BViewController *bViewC = [self.storyboard instantiateViewControllerWithIdentifier:@"BViewController"]; 

    [self.navigationController pushViewController:bViewC animated:YES]; 
} 

-(void)TestMethod{ 

    NSLog(@" Its NOT Working"); 
} 

我的第二個視圖控制器,即BViewController

#import "BViewController.h" 
#import "CViewController.h" 

@interface BViewController()<testDelegates> 

@end 

@implementation BViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (IBAction)bNextAction:(id)sender { 

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"]; 

// cVC.mydelegate = self; 
    [self.navigationController pushViewController:cVC animated:YES]; 
} 

-(void)TestMethod{ 

    NSLog(@" Its Working If I uncomment to cVC.mydelegate = self"); 
} 

我的第三個視圖控制器,在這裏我聲明在協議。 h文件,即CViewController

#import <UIKit/UIKit.h> 

@protocol testDelegates <NSObject> 

-(void)TestMethod; 

@end 

@interface CViewController : UIViewController 

@property (weak) id <testDelegates> mydelegate; 

@end 

而在.m文件

#import "CViewController.h" 

@interface CViewController() 

@end 

@implementation CViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (IBAction)cNextAction:(id)sender { 

    [self.mydelegate TestMethod]; 
} 

回答

1

創建和這個類摧毀你離開viewDidLoad方法之後。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"]; 
    cVC.mydelegate = self; 
} 

據我所知,你需要在CViewController中執行ViewController中的一些方法。

您可以將viewController作爲委託傳遞給BViewController,並將它們傳遞給CViewController。

或者你可以用這樣的代碼:

- (IBAction)cNextAction:(id)sender { 
    id<testDelegates> delegate = self.navigationController.viewControllers[0]; 
    if([delegate conformsToProtocol:@protocol(testDelegates)]) 
    { 
     [delegate TestMethod]; 
    } 
} 

這不是最好的方式,但它會工作。

+0

是的,你得到了,我必須從CViewController執行一些操作到ViewController,在你的代碼中,在這一行[delegate conformsToProtocol:testDelegates]中,「testDelegates」變成紅色標記並且使用未聲明的標識符。 :( –

+0

對不起,我已經修改回答 – Sergey

+0

完美,完美工作..謝謝:) –

1

試試這個

self.delegate = self.navigationController.viewControllers[0]; 
+0

它的工作,但@Sergey答案更魯棒。感謝您的幫助雖然.. :) –

1

您需要聲明另一個協議到BViewController類。

Please follow below steps. 

第1步:BViewController.h

@protocol testDelegates2 <NSObject> 

-(void)TestMethod2; 

@interface BViewController : UIViewController 

@property (weak) id <testDelegates> mydelegate; 


@end 
@implementation CViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (IBAction)cNextAction:(id)sender { 

    [self.mydelegate TestMethod2]; 
} 

Step 2. In ViewController class conform Protocol 
@interface ViewController()<testDelegates> 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"]; 
    cVC.mydelegate = self; 
} 

- (IBAction)nextAction:(id)sender { 

    BViewController *bViewC = [self.storyboard instantiateViewControllerWithIdentifier:@"BViewController"]; 
cVC.mydelegate = self; //important line 
    [self.navigationController pushViewController:bViewC animated:YES]; 
} 

-(void)TestMethod2{ 

    NSLog(@"Final Output"); 
} 
+0

它似乎不是好主意通過在兩個vc聲明協議。我認爲。我需要從第三個VC到第一個vc –

+0

O.w使用的通知中心。在ViewController上註冊並從CViewController類調用。 –

+0

雖然,它不是個好主意:/ –

1

如果你正在嘗試CViewController的代表設置的ViewController然後 嘗試把

self.mydelegate = self.navigationController.viewControllers[0] 

上CViewController的viewDidLoad方法。

否則您正試圖使ViewController和BViewController都成爲CViewController的委託,那麼這不可能像這樣,因爲對象的委託只能是唯一的。這意味着委託只是一個變量,用於保存對象的引用,因爲您知道變量的值在某個時刻可以是唯一的。

所以如果你願意在Viewcontroller和BViewController上做一些任務,那麼就使用通知模式。 See apple doc here