各位,
我在某些导航逻辑方面遇到问题。目前我有一个简单的两个选项卡式选项卡栏应用程序。但我想在前面显示一个登录屏幕。所以这将是一个 UIView。
目前代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[roosterViewController alloc] initWithNibName:@"roosterViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
所以这插入了一个简单的选项卡 Controller 。好吧,现在我想要一个登录屏幕。所以这将是一个简单的 UIView
,它插入标签栏 Controller 。但我似乎看不到如何做到这一点的逻辑。
我一直在尝试呈现一个模态视图 Controller ,但问题是:选项卡栏将加载在背景上。由于我需要用户名/密码信息才能在选项卡 View 上工作,因此这不起作用。
我的逻辑是:
委托(delegate)>加载loginViewController>加载标签栏 Controller
但是,然后我需要能够“注销”。所以我需要销毁标签栏 Controller 并显示登录屏幕。
对此有什么想法吗?
最佳答案
你可以这样做:
您可以创建您的 AppDelegate 遵循的协议(protocol)。
@protocol Authenticator <NSObject>
@required
- (void)authenticateWithUsername:(NSString *)username andPassword:(NSString *)password;
- (bool)authenticated;
@optional
- (void)authenticationSuccess;
- (void)authenticationFailure;
...
@interface AppDelegate : UIResponder <UIApplication, Authenticator>
@property (readonly, nonatomic, assign) bool loggedIn;
...
@implementation AppDelegate
@synthesize loggedIn = _loggedIn;
- (void)authenticateWithUsername:(NSString *)username andPassword:(NSString *)password
{
//if success
_loggedIn = YES;
//check if app responds to the optional authenticateSuccess method
//call it if it does
//else fail
//do stuff
}
- (bool)authenticated
{
if (_loggedIn != NULL) {
return _loggedIn;
}
//do other stuff
}
...
我对正确的 Objective-C 约定和语法有点模糊,所以如果我有一些错误,请原谅我,但无论如何,这是一些需要处理的逻辑和伪代码。根据您的需要进行调整。
我希望这会有所帮助。
编辑:
我想我的回答有点未完成。在我看来,你的问题的答案与某种身份验证结构密切相关。如果您的应用程序与身份验证密切相关,那么为什么不通过身份验证结构来控制其流程。我想这就是我的观点,并且由于所有这些都可以在您的应用程序委托(delegate)中方便且轻松地使用,因此您可以在任何您想要的地方调用这些方法,因此让您的身份验证逻辑决定要显示哪个 View Controller 。
关于objective-c - UIView 登录屏幕到选项卡栏逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13918551/