NSNotification 的问题关闭模态视图控制器

发布于 2024-11-26 03:12:56 字数 1073 浏览 1 评论 0原文

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

它运行良好,模态视图控制器已被关闭。但我想以特定的方式更改它,但它并不像我期望的那样工作...

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

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


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

我在 sampleViewControllerviewWillAppear 上发送通知,当我选择该 tabBarIcon 时,它会关闭 TabBarController。

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

所以我添加它就像

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

这样不会关闭选项卡栏控制器。

注意: 请查看 NSArray 初始化的顺序。

通知发布在 viewWillAppearsampleViewController` 和观察者在各自的视图控制器中呈现模式视图控制器

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-12-03 03:12:56

你能在发布通知之前先放一个 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];
    }


}

看看是否有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文