我怎么知道iPhone中的当前ViewController名称 [英] how can I know current viewcontroller name in iphone
问题描述
我有实现UIViewController的BaseView.项目中的每个视图都必须实现此BaseView.
I have BaseView which implement UIViewController. Every view in project must implement this BaseView.
在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];
}
问题是我的登录视图也实现了BaseView,检查登录,然后再次打开LoginView,即卡在递归调用中.
The problem is my Login view also implement BaseView, checks for login, and again open LoginView i.e. stuck in to recursive calling.
如果请求来自LoginView,是否可以签入checkLoginStatus方法,然后不执行其他任何操作,否则请检查登录名.例如:
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];
}
}
请帮助.
推荐答案
使用以下方法:
if ([self isMemberOfClass:[Login class]])
{
CFShow(@"Yep, it's the login controller");
}
isMemberOfClass
告诉您该实例是否是该类的确切实例.还有 isKindOfClass
:
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测试该类是某个类的扩展.
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];
}
这篇关于我怎么知道iPhone中的当前ViewController名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!