2017-08-06 64 views
0

我有一個標籤欄,第一個標籤有HomeViewController和第二個標籤有導航控制器有兩個ViewControllers - CheckOutViewControllerPaymentViewControllerTabBar之間的ViewControllers之間的委託沒有被調用

我想在PaymentViewController上添加一個代理,它允許我更新HomeViewController

但是,HomeViewController上的代理方法沒有被調用。

PaymentViewController

@protocol PaymentViewControllerDelegate <NSObject> 

@required 
-(void)paymentSuccessfull :(BOOL)isSuccessfull; 
@end 

@interface PaymentViewController 
@property (nonatomic, weak) id <PaymentViewControllerDelegate> paymentDelegate; 

-(void)orderProcessed 
{ 
    [paymentDelegate paymentSuccessfull : YES]; 
    [self.navigationController popToRootViewControllerAnimated :YES]; 
} 

HomeViewController.m

@interface HomeViewController : UIViewController<PaymentViewControllerDelegate> 

// I do not know how to assign delegate here in the tabbar 
-(void)paymentSuccessfull:(BOOL)isSuccessfull 
{ 
    NSLog(@"success"); 
} 
+0

你在HomeViewController中使用了PaymentViewController的實例嗎? –

+0

不,我沒有使用,因爲選項卡欄用於查看控制器轉換。 – hotspring

+0

您可以使用該委託方法,因爲您沒有在HomeViewController中使用PaymentViewController的實例,但是如果您想在HomeViewController中調用該方法,則可以使用NSNotificationCenter並在orderProcessed中發佈通知並將其捕獲到HomeViewController中。 –

回答

0

從我的理解是,你有一些結構是這樣的:

的TabBar控制器:

  • HomeViewController
  • NavigationController

    • PaymentViewController

    • CheckOutViewController

現在既然你已經添加協議PaymentViewController,根據您的結構,委託方法應該從正在實例化PaymentViewController控制器叫(我猜你的情況可能是NavigationController /的TabBar控制器)。 因此,在其中一個控制器中,您將從Storyboard實例化PaymentViewController,您還必須指定該類符合委託以及

這將是這樣的:

paymentViewControllerObj.delegate = self; 

我想這應該幫助你或給你方向。

+0

檢查此更清晰 https://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c –

相關問題