在MFSideMenuController内部viewwillappear方法无法正常工作

时间:2016-07-08 06:17:25

标签: objective-c iphone ios7

我想查看一个nsuserdefault值是否可用,如果值可用,它应该显示Logout,如果value是nill,它应该在sidevillappear方法中我在侧边菜单中显示Login但是它不起作用,请有人澄清了这一点。

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


}

2 个答案:

答案 0 :(得分:2)

假设MFSidemenu是一个用于抽屉的吊舱,用于显示菜单的功能。 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.

}

答案 1 :(得分:1)

我已经这样做了,现在它正在发挥作用。

- (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方法(您要调用的任何页面)示例 Loginviewcontroller.M

- (IBAction)Logout:(id)sender

{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginlogout:) name:@"login" object:nil];

}

最后是Delloc

- (void) dealloc
{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"login" object:nil];
}