2017-08-06 24 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 –