[英]Tabbar delegate method is not getting called after changing storyboard
我在登錄情節提要中沒有登錄和注冊視圖控制器,而在主情節提要中沒有。
一旦注冊成功或登錄成功,然后將Login Storyboard更改為Main。 以下代碼有效,但是當我選擇任何選項卡時,它不會在AppDelegate中調用選項卡委托方法
但是,如果用戶已經成功注冊或登錄,則它將調用以下選項卡委托方法。
LoginViewController.m
if(isLoginSuccess)
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CustomTabBarViewController *tbc = [mainStoryboard instantiateViewControllerWithIdentifier:@"tabbar"];
tbc.selectedIndex = 2;
[self presentViewController:tbc animated:YES completion:nil];
}
AppDeletage.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CustomTabBarViewController *tabBarController = (CustomTabBarViewController *)self.window.rootViewController;
tabBarController.delegate = self;
return YES;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 2) {
if(![self isRegistered])
{
UIStoryboard *loginStoryboard = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];
UIViewController *vc = [loginStoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[ROOTVIEW presentViewController:vc animated:YES completion:nil];
}
else
{
tabBarController.selectedIndex = 2;
}
}
}
更新:
沒有調用AppDelegate,但我想知道為什么以下代碼在用戶注銷后無法在AppDelegate中打開Loginstoryboard。
#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[ROOTVIEW presentViewController:vc animated:YES completion:nil];
請找到下面的代碼。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:@"TabbarController"];
tabController.delegate = self;
return YES;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(@"Selected Index - %lu",(unsigned long)tabBarController.selectedIndex);
}
在ViewController的Button Click方法上,
- (IBAction)btnLoginTapped:(id)sender {
UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:@"TabbarController"];
tabController.selectedIndex = 1;
[self presentViewController:tabController animated:YES completion:nil];
}
然后Main.storyboard,從對象庫拖動對象 ,並把它下面急救員在標簽欄控制器的場景,設置對象類的AppDelegate,然后右鍵點擊標簽欄控制器,並設置委托給該對象類如下圖所示的圖像。
讓我知道它是否有效,我准備為解決方案提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.