开发者

Problem with NSNotification & Dismissing modal View Controllers

So, I have a tabbarcontroller, and I pass a notification to dismissModalViewController when a particular tabBarItem is touched.

It is working well and the modal View Controller is dismissed. But I want to change it in a particular way, and it does not work as I expect it to...

I have the observer initialized before the notification is posted. These are the tabBarItems -

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


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

I send a notification on the viewWillAppear of sampleViewController and when I choose that tabBarIcon, it dismisses the TabBarController.

BUT I want the sampleViewController to be on the left most of the UITabBar.

And so I add it like

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

THIS DOES NOT DISMISS TAB BAR CONTROLLER.

Note: Please see the order in which NSArray is initialized.开发者_开发百科

The notification is posted in the viewWillAppear ofsampleViewController` and observer in the respective view controller which presents the modal view controller


Could you put a NSLog right before you post the notification?

See if you get any output when the app loads.

EDIT: Adding onto the answer based on your response

In your sampleViewController could you try this:

Make it conform to the UITabBarControllerDelegate. Your sampleViewController class interface should be something like this:

@interface SampleViewController : UIViewController <UITabBarControllerDelegate>

Then in the .m of your sampleViewController, in the viewDidLoad, set the delegate to be the sampleViewController (self in this case)

-(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
}

Now implement the delegate method somewhere inside the sampleViewController .m file.

-(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];
    }


}

See if that works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜