how can I know current viewcontroller name in iphone

我与影子孤独终老i 提交于 2020-01-04 05:32:08

问题


I have BaseView which implement UIViewController. Every view in project must implement this BaseView.

In BaseView, I have method:

-(void) checkLoginStatus
{
    defaults = [[NSUserDefaults alloc] init];

    if(![[defaults objectForKey:@"USERID"] length] > 0 )
    {
        Login *login=[[Login alloc] initWithNibName:@"Login" bundle:nil];
        [self.navigationController pushViewController:login animated:TRUE];
        [login release];
    }
    [defaults release];
}

The problem is my Login view also implement BaseView, checks for login, and again open LoginView i.e. stuck in to recursive calling.

Can I check in checkLoginStatus method if request is from LoginView then take no action else check login. Ex:

- (void) checkLoginStatus
{
    **if(SubView is NOT Login){** 
        defaults = [[NSUserDefaults alloc] init];

        if(![[defaults objectForKey:@"USERID"] length] > 0 )
        {
            Login *login=[[Login alloc] initWithNibName:@"Login" bundle:nil];
            [self.navigationController pushViewController:login animated:TRUE];
            [login release];
        }
        [defaults release];
    }
}

Please help..


回答1:


Use the following method:

if ([self isMemberOfClass:[Login class]])
{
    CFShow(@"Yep, it's the login controller");
}

isMemberOfClass tells you if the instance is an exact instance of that class. There's also isKindOfClass:

if ([self isKindOfClass:[BaseView class]])
{
    CFShow(@"This will log for all classes that extend BaseView");
}

isKind tests that the class is a extension of a certain class.

So given your example:

-(void) checkLoginStatus
{
    defaults = [[NSUserDefaults alloc] init];

    if (![self isMemberOfClass:[Login class]])
    {
        if (![[defaults objectForKey:@"USERID"] length] > 0 )
        {
            Login *login=[[Login alloc] initWithNibName:@"Login" bundle:nil];
            [self.navigationController pushViewController:login animated:TRUE];
            [login release];
        }
    }
    [defaults release];
}



回答2:


implement an empty checkLoginStatus in Login.

@implementation Login
  -(void) checkLoginStatus {}
@end



回答3:


Add one parameter in checkLoginStatus and set that parameter when you you call method from LoginView and in checkLoginStatus check that parameter if that parameter is set skip this block...i.e

if(![[defaults objectForKey:@"USERID"] length] > 0  && var1 != TRUE)
{
     Login *login=[[Login alloc] initWithNibName:@"Login" bundle:nil];
    [self.navigationController pushViewController:login animated:TRUE];
    [login release];

}


来源:https://stackoverflow.com/questions/2635469/how-can-i-know-current-viewcontroller-name-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!