我有一个名为CenterViewController的superViewcontroller,其中我实现了UITabBarController * tabVC,tabVC的viewcontroller是featureViewController,scheduleViewController,feedbackViewController,contactUsViewController。
它们被添加为:
tabVC.viewControllers = [NSArray arrayWithObjects:featureViewController,scheduleViewController,feedbackViewController,contactUsViewController, nil];
self addChildViewController:tabVC];
[self.view addSubview:tabVC.view];
当成功提交反馈时,我必须从feedbackviewcontroller访问名为loadHome的函数,该函数位于CenterViewcontroller中:
-(void) loadHome
{
[tabVC setSelectedIndex:0];
}
搜索了很多实现协议委托的解决方案代码如下:
centerviewcontroller.h
@protocol CenterViewControllerDelegate <NSObject>
@required
- (void)loadHome;
@end
@interface CenterViewController : UIViewController <LeftPanelViewControllerDelegate, UITabBarControllerDelegate>
@property (nonatomic, assign) id<CenterViewControllerDelegate> delegate;
@end
feedbackviewcontroller.h
#import "CenterViewController.h"
@interface FeedbackViewController : UIViewController<UIAlertViewDelegate>
{
id<CenterViewControllerDelegate> centerdelegate;
}
@property (nonatomic,assign) id<CenterViewControllerDelegate> centerdelegate;
当单击成功提交反馈时alertview上的按钮时,我可以调用centerviewcontroller的loadHome函数:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"alert view button index %d", buttonIndex);
switch(buttonIndex)
{
case 0:
NSLog(@"alert view button index inside switch %d", buttonIndex);
[self.centerdelegate loadHome];
break;
case 1:
break;
default:
break;
}
}
[self.centerdelegate loadHome];正在执行,但它没有带我到centerviewcontroller中的函数。
没有错误没有发生。