Iphone 通知和解除模态视图控制器时出现问题

Iphone 通知和解除模态视图控制器时出现问题,iphone,objective-c,cocoa-touch,modalviewcontroller,nsnotifications,Iphone,Objective C,Cocoa Touch,Modalviewcontroller,Nsnotifications,因此,我有一个tabbarcontroller,当触摸特定的tabBarItem时,我会向dismissModalViewController传递一个通知 它工作正常,模态视图控制器被取消。但我想用一种特殊的方式来改变它,它并没有像我期望的那样工作 在发布通知之前,我已初始化了观察者。这些是tabBarItems- NSArray *viewControllerss = [[NSArray alloc] initWithObjects: myProfileDataViewController,

因此,我有一个tabbarcontroller,当触摸特定的tabBarItem时,我会向dismissModalViewController传递一个通知

它工作正常,模态视图控制器被取消。但我想用一种特殊的方式来改变它,它并没有像我期望的那样工作

在发布通知之前,我已初始化了观察者。这些是tabBarItems-

NSArray *viewControllerss = [[NSArray alloc] initWithObjects: myProfileDataViewController, 
sampleViewController,reminderInfoViewController, nil];


[self.tabBarContr setViewControllers:viewControllerss animated:YES];
self.tabBarContr.selectedIndex = 2;
我在sampleViewController的ViewWill上发送通知,当我选择该tabBarIcon时,它将取消TabBarController

但我希望sampleViewController位于UITabBar的最左侧

所以我加上它

 NSArray *viewControllerss = [[NSArray alloc] initWithObjects: sampleViewController,
 myProfileDataViewController, reminderInfoViewController, nil];
这不会关闭选项卡栏控制器

注意:请参阅NSArray的初始化顺序


通知发布在视图中,将在显示模态视图控制器的相应视图控制器中显示SampleViewController`和observer

在发布通知之前,是否可以放置NSLog

查看应用加载时是否有任何输出

编辑:根据您的回答添加到答案上

在sampleViewController中,您可以尝试以下操作:

使其符合UITABBARCONTROLLEDELEGATE。sampleViewController类接口应如下所示:

@interface SampleViewController : UIViewController <UITabBarControllerDelegate>
现在在sampleViewController.m文件中的某个地方实现委托方法

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    // I've included this to see if this method actually gets called or not.
    NSLog(@"Dismissing modal view controller");

    // check to make sure sampleViewController tab was pressed by checking 
    // the class type of the viewController parameter being passed in

    if ([viewController isKindOfClass:[SampleViewController class]]  
    {
        // I assume you have a pointer reference to that modal view controller 
        // you want to dismiss
        [self dismissModalViewController:theUnwantedViewController animated:YES];
    }


}

看看这是否管用。

我总是到处登录。通知已发送,观察员已准备就绪。当首先将sampleViewController添加到阵列中时,模态视图不会关闭。是否从该NSLog获得输出?我想知道在选项卡之间切换是否会触发ViewWillDisplay方法。是的,它没有触发ViewWillDisplay。可能是当应用程序第一次加载时,第一个选项卡栏控制器已经加载,ViewWillDisplay会触发一次,但什么也没有发生。然后,当你的应用程序完成加载时,你会看到tabbar选择索引2代码导致的tab bar控制器2,当你试图切换回sampleViewController时,它不算视图显示和视图显示从未被触发,因此为什么你的关闭视图控制器的通知从未被调用?我想我记得有一种协议委托方法,它允许您在选择选项卡栏时执行某些操作。这一点您完全正确。我该怎么做呢?
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    // I've included this to see if this method actually gets called or not.
    NSLog(@"Dismissing modal view controller");

    // check to make sure sampleViewController tab was pressed by checking 
    // the class type of the viewController parameter being passed in

    if ([viewController isKindOfClass:[SampleViewController class]]  
    {
        // I assume you have a pointer reference to that modal view controller 
        // you want to dismiss
        [self dismissModalViewController:theUnwantedViewController animated:YES];
    }


}