NSNotification&关闭模态视图控制器

时间:2011-07-25 22:11:19

标签: iphone objective-c cocoa-touch modalviewcontroller nsnotifications

所以,我有一个tabbarcontroller,当触摸到特定的dismissModalViewController时,我会向tabBarItem发送通知。

运行良好,模态View Controller被解雇。但是我想以一种特殊的方式改变它,它并不像我期望的那样......

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

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


[self.tabBarContr setViewControllers:viewControllerss animated:YES];
self.tabBarContr.selectedIndex = 2;

我在viewWillAppear sampleViewController发送通知,当我选择tabBarIcon时,它会解除TabBarController。

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

所以我添加它像

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

这不会丢失TAB BAR控制器。

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

通知发布在相应视图控制器中的viewWillAppear of sampleViewController`和观察者中,该控制器显示模态视图控制器

1 个答案:

答案 0 :(得分:0)

您可以在发布通知之前立即放置NSLog吗?

查看应用加载时是否获得任何输出。

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

在你的sampleViewController中可以试试这个:

使其符合UITabBarControllerDelegate。您的sampleViewController类接口应该是这样的:

@interface SampleViewController : UIViewController <UITabBarControllerDelegate>

然后在你的sampleViewController的.m中,在viewDidLoad中,将委托设置为sampleViewController(在这种情况下为self)

-(void) viewDidLoad
{   
    [super viewDidLoad];

    // Assuming you have a reference to your tabBarController somewhere

    [self setDelegate:self]; // try this line or the line below
    // [[self tabBarController] setDelegate:self];

    // The rest of your drawing code here
}

现在在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];
    }


}

看看是否有效。