无法从模态ViewController访问UITabBarController

时间:2014-08-06 18:21:30

标签: ios objective-c uitabbarcontroller modalviewcontroller

我有一个基于UITabBarController的应用程序,其中我的登录页面由默认选项卡的VC(UITabBarController索引0)以模态方式呈现,并且由{以模态方式解除} {1}}。

从我的设置页,我有注销按钮,如果用户立即重新登录,我必须致电dismissViewControllerAnimated:,然后重置{{ 1}} selectedIndex 属性,以便摆脱设置页面,并返回到起始tabBar选项卡。所以我这样做:

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

在这种情况下,方法UITabBarController不起作用,因为这种方法实际上创建了tabBarController的新实例,当它是实际显示的原始实例时。我似乎无法访问在模态显示的登录页面之前的已存在的 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString* responseString = [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding]; /*Succesful Login*/ if([responseString isEqualToString:@"success"]) { UITabBarController *tabBarController = (UITabBarController *)self.presentingViewController; if (tabBarController){ NSLog(@"I have a tab bar~"); [self.tabBarController setSelectedIndex:0]; } else{ NSLog(@"I don't have a tab bar~"); } //dismisses from a second, immediate, re-login attempt [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil]; //dismisses from first login attempt [self dismissViewControllerAnimated:YES completion:nil]; NSLog(@"Succesful login."); }

修改

我已尝试使用此条件检查其存在:

setSeletedIndex:

我甚至将 self.tabBarController 更改为self.presentingViewController.tabBarController并获取nil和self.presentingViewController.presentingViewController.tabBarController并获取nil,我如何访问以前存在的{{ 1}}?

目标:访问在登录页面呈现之前存在的应用程序的UITabBarController。 (访问Appdelegate' UITabBarController)

3 个答案:

答案 0 :(得分:1)

您的使用案例非常适合授权。

在Login的头文件中,定义一个委托。如,

@class Login
@protocol LoginDelegate <NSObject>
@required
- (void) userDoneWithLoginController:(Login *) controller;
@end

@interface Login: UIViewController

@property (weak, nonatomic) id<LoginDelegate> viewDelegate;

@end

然后你的呈现视图控制器segue可以通过设置这个&#34; viewDelegate&#34;来设置自己作为登录视图控制器的委托。在控制传递到“登录”页面之前,showForSegue内的对象用于呈现视图控制器。 [如果你想澄清,请告诉我。]

此外,呈现视图控制器将实现所需的方法 - 在调用时可以简单地关闭登录页面

-(void)  userDoneWithLoginController:(Login *) controller
{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

完成此操作后,登录页面在完成后只调用其委托方法,因此:

[self.viewDelegate userDoneWithLoginController:self];

中提琴!

这是在iOS中解除模态视图的首选方法。呼叫控制器应该是解除视图的控制器。尽量避免让视图控制器解雇自己。相反,让他们的呼叫/呈现控制器(代表)解雇他们。

答案 1 :(得分:1)

以下内容应该有效。

// check that the vc presenting your current modal vc is a tab bar controller
if ([self.presentingViewController isKindOfClass:[UITabBarController class]]) {

    // get the pointer of type UITabBarController
    UITabBarController *tabBarController = (UITabBarController *)self.presentingViewController;

    // set to the desired tab
    tabBarController.selectedIndex = 2;
}

// dismiss the current modal vc
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

答案 2 :(得分:0)

我相信我找到了解决方案。

我可以从 Appdelegate 访问该应用的UITabBarController,如下所示:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate myTabBar] setSelectedIndex:0];