【问题标题】:Inside MFSideMenuController viewwillappear method is not working内部 MFSideMenuController viewwillappear 方法不起作用
【发布时间】:2016-07-08 06:17:25
【问题描述】:

我想检查一个 nsuserdefault 值是否可用,如果值可用,它应该显示 Logout,如果值为 nill,它应该在侧面菜单中显示 Login 我在 viewwillappear 方法中这样做,但它不起作用,请有人对此进行了澄清。

-(void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *id1 = [defaults objectForKey:@"ID"];


    if (id1.length>0)
    {
        recipes = [NSArray arrayWithObjects:@"Account Details", **@"Logout",** @"Change Password", @"Contacts", @"Ham and Cheese Panini", nil];
    }

    else
    {
        recipes = [NSArray arrayWithObjects:@"Account Details", @"**Login**", @"Change Password", @"Contacts", @"Ham and Cheese Panini", nil];
    }


}

【问题讨论】:

  • side menu 的 viewWillAppear 将仅在您呈现 MFSideMenuController 时调用,而不是每次打开侧视图时调用。您可以设置断点并调试以查找问题,您是得到空字符串 id1 还是根本没有调用 viewWillAppear?这么多信息很难弄清楚实际问题是什么

标签: objective-c iphone ios7


【解决方案1】:

假设 MFSidemenu 是一个用于显示菜单的抽屉式功能的 pod。 MFSidemenu 有以下方法来显示侧边菜单。

[self.menuContainerViewController setMenuState:MFSideMenuStateLeftMenuOpen completion:^{}];

您可以在调用此方法之前或在完成块中添加对 NSUserDefaults 的检查,以适合您的方式。 每次打开侧边菜单时都不会调用 ViewWillAppear。

它还有一个通知,您可以观察到在 SideMenu 打开时触发(即菜单将打开,菜单已打开等)

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(menuStateEventOccurred:)
name:MFSideMenuStateNotificationEvent
object:nil];

- (void)menuStateEventOccurred:(NSNotification *)notification {
    MFSideMenuStateEvent event = [[[notification userInfo] objectForKey:@"eventType"] intValue];
    MFSideMenuContainerViewController *containerViewController = notification.object;
// Check whether menu opens here and then add your code.

}

【讨论】:

    【解决方案2】:

    我已经这样做了现在它正在工作。

    - (void)viewDidLoad {
        [super viewDidLoad];
    {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginlogout:) name:@"login" object:nil];
    
    }
    
    - (void)loginlogout:(NSNotification *) notification
    {
    
    
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *id1 = [defaults objectForKey:@"ID"];
    
    
        if (id1.length>0)
        {
            recipes = [NSArray arrayWithObjects:@"Account Details", @"Logout", @"Change Password", @"Contacts", @"Ham and Cheese Panini", nil];
        }
    
        else
        {
            recipes = [NSArray arrayWithObjects:@"Account Details", @"Login", @"Change Password", @"Contacts", @"Ham and Cheese Panini", nil];
        }
        [tableview1 reloadData];
    }
    

    在其他页面调用这个loginlogout方法(你想调用的任何页面)例子 登录viewcontroller.M

    - (IBAction)Logout:(id)sender
    
    {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginlogout:) name:@"login" object:nil];
    
    }
    

    终于戴尔洛克

    - (void) dealloc
    {
    
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"login" object:nil];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      相关资源
      最近更新 更多